当前位置:网站首页>【Golang】golang实现简单memcache
【Golang】golang实现简单memcache
2022-07-24 11:06:00 【陶士涵】
如果有使用内存存一些简单数据的功能,可以实现下面这样的带有过期时间的内存类
内存中存储key-value,并且可以设置过期时间
package tools
import (
"sync"
"time"
)
// Memory struct contains *memcache.Client
type Memory struct {
sync.Mutex
data map[string]*data
}
type data struct {
Data interface{}
Expired time.Time
}
// NewMemory create new memcache
func NewMemory() *Memory {
return &Memory{
data: map[string]*data{},
}
}
// Get return cached value
func (mem *Memory) Get(key string) interface{} {
if ret, ok := mem.data[key]; ok {
if ret.Expired.Before(time.Now()) {
mem.deleteKey(key)
return nil
}
return ret.Data
}
return nil
}
// IsExist check value exists in memcache.
func (mem *Memory) IsExist(key string) bool {
if ret, ok := mem.data[key]; ok {
if ret.Expired.Before(time.Now()) {
mem.deleteKey(key)
return false
}
return true
}
return false
}
// Set cached value with key and expire time.
func (mem *Memory) Set(key string, val interface{}, timeout time.Duration) (err error) {
mem.Lock()
defer mem.Unlock()
mem.data[key] = &data{
Data: val,
Expired: time.Now().Add(timeout),
}
return nil
}
// Delete delete value in memcache.
func (mem *Memory) Delete(key string) error {
mem.deleteKey(key)
return nil
}
// deleteKey
func (mem *Memory) deleteKey(key string) {
mem.Lock()
defer mem.Unlock()
delete(mem.data, key)
}
// clean all data
func (mem *Memory) Clean() {
mem.Lock()
defer mem.Unlock()
mem.data = map[string]*data{}
}使用方法
mem := NewMemory()
mem.Set("name", "livechat", time.Duration(10)*time.Second)
time.Sleep(time.Duration(2) * time.Second)
name := mem.Get("name")边栏推荐
- Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]
- 【Golang】golang中time类型的before方法
- The difference between Lora wireless technology and lorawan gateway module
- Research on parameter setting of MATLAB FFT
- 在idea中System.getProperty(“user.dir“)识别到模块(module)路径的方法:Working directory的设置
- read_csv 报错:‘gbk‘ codec can‘t decode byte 0xb4 in position 274: illegal multibyte sequence
- Zero basic learning canoe panel (4) -- button
- Princeton chendanqi: how to make the "big model" smaller
- Data visualization - White Snake 2: black snake robbery (1)
- CSDN blog removes the uploaded image watermark
猜你喜欢

聊聊软件测试-自动化测试框架

Zero basic learning canoe panel (7) -- input/output box

Zero basic learning canoe panel (8) -- hex/text editor

关于【软件测试-自动化测试之面试技巧和注意事项】——侃侃而谈

Docker installs 3 master and 3 slave redis clusters

变频器的四大组成部分和工作原理

read_csv 报错:‘gbk‘ codec can‘t decode byte 0xb4 in position 274: illegal multibyte sequence

性能测试总结(一)---基础理论篇

变频器的工作原理和功能应用

Altium one key automatic BOM
随机推荐
【白帽子讲Web安全】第一章 我的安全世界观
Publish local images to Alibaba cloud
[FPGA]: use of MicroBlaze
[FPGA]: IP core - multiplier
MySQL queries tables and fields according to comments
Nodejs installation tutorial
Self taught software testing talent -- not covered
js树形结构,根据里层id找出它所属的每层父级集合
数据可视化-《白蛇2:青蛇劫起》(1)
read_csv 报错:‘gbk‘ codec can‘t decode byte 0xb4 in position 274: illegal multibyte sequence
自动推理的逻辑06--谓词演算
性能测试总结(一)---基础理论篇
Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment
[FPGA]: IP core ibert
The bean injected through @autowired can still be injected even if the class is not annotated with annotations such as @comment
【Golang】golang实现urlencode urldecode函数
SQL optimization skills and precautions
JS tree structure, find out the parent set of each layer it belongs to according to the ID of the inner layer
Mockito3.8 how to mock static methods (how to mock PageHelper)
Yum installation prompt to protect multi library version