2

go语言实现的基于内存的key/value的缓存库

 1 year ago
source link: https://studygolang.com/articles/36054
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

go-cache

一个基于内存的key/value的go语言存储库,支持string和hash

go get -u github.com/itmisx/go-cache

  • 支持过期回调函数
  • 支持哈希字段的过期时间设置

🏗️ 使用场景

主要用在单机或本地缓存场景

✅ 开始使用

  • 设置键值,不带过期回调
cache.Set("key1", 1, time.Second*4, nil)
  • 设置键值,带过期回调
cache.Set("key1", 1, time.Second*4, func(key string, value interface{}) {
    log.Println("callback1", key, value)
})
  • 设置键的过期时间
cache.Expire("key1",time.Second*4)
// found指示是否存在
value,found := cache.Get("key1")
cache.Del("key1")
  • 设置hash,带过期回调
cache.HSet("hkey1", "hfield1", 1, time.Second*8, func(key string, field string, value interface{}) {
    log.Println("callback2", key, field, value)
})
  • 设置hash字段的过期时间
cache.Expire("hkey1","hfield1",time.Second*3)
  • 获取hash值
// found指示是否存在
value,found := cache.HGet("hkey1","hfield1")
  • 删除hash字段
cache.HDel("hkey1","hfield1")

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK