gcache增加内存锁

This commit is contained in:
John
2018-03-29 14:31:45 +08:00
parent 66a71c1a44
commit 545fc68481

View File

@ -96,6 +96,20 @@ func BatchRemove(keys []string) {
cache.BatchRemove(keys)
}
// 基于内存缓存的锁锁成功返回true失败返回false当失败时表示有其他的锁存在
func Lock(key string, expire int64) bool {
if v := cache.Get(key); v != nil {
return false
}
cache.Set(key, struct {}{}, expire)
return true
}
// 解除基于内存缓存的锁
func Unlock(key string) {
cache.Remove(key)
}
// 获得所有的键名,组成字符串数组返回
func Keys() []string {
return cache.Keys()
@ -175,6 +189,20 @@ func (c *Cache) BatchSet(data map[string]interface{}, expire int64) {
}
}
// 基于内存缓存的锁锁成功返回true失败返回false当失败时表示有其他的锁存在
func (c *Cache) Lock(key string, expire int64) bool {
if v := c.Get(key); v != nil {
return false
}
c.Set(key, struct {}{}, expire)
return true
}
// 解除基于内存缓存的锁
func (c *Cache) Unlock(key string) {
c.Remove(key)
}
// 获取指定键名的值
func (c *Cache) Get(key string) interface{} {
c.dmu.RLock()