Skip to content
/ sugar Public

this is library of cache and support for distributed locks

Notifications You must be signed in to change notification settings

zlx2019/sugar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sugar

this is A library of caching components and support for distributed locks


目前支持的缓存组件有:

  • Redis

分布式锁组件

  • Redis
  • Etcd

Redis分布式锁

1. 非阻塞模式

// 创建一个Redis分布式锁-非阻塞模式
lock := NewRedisDistributedLock("lock-key", caches.NewDefaultRedisTemplate())
// 加锁
err := lock.Lock(context.Background())
if err == nil {
    //加锁成功
    err = lock.Unlock(context.Background())
    if err == nil {
        //解锁成功
    }
}

2. 阻塞模式

lock := NewRedisDistributedLock("lock-key", caches.NewDefaultRedisTemplate(),WithBlocking())
lock.Lock(ctx)
defer lock.Unlock(ctx)

3. 看门狗模式

lock := NewRedisDistributedLock("dog-lock-key", caches.NewDefaultRedisTemplate(),WithExpire(time.Second * 5),WithWatchDog())

About

this is library of cache and support for distributed locks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages