123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package anchorReward
- import (
- "context"
- "go-common/library/cache/redis"
- "go-common/library/log"
- )
- const (
- _preLock = "lk_"
- _preExpireCount = "ec_"
- )
- func lockKey(key string) string {
- return _preLock + key
- }
- func expireCountKey(key string) string {
- return _preExpireCount + key
- }
- //DelLockCache del lock cache.
- func (d *Dao) DelLockCache(c context.Context, k string) (err error) {
- var (
- key = lockKey(k)
- conn = d.redis.Get(c)
- )
- defer conn.Close()
- if _, err = conn.Do("DEL", key); err != nil {
- log.Error("DelLockCache.conn.Do(del,%v) err(%v)", key, err)
- } else {
- log.Info("DelLockCache.conn.Do(del,%v)", key)
- }
- return
- }
- // GetExpireCountCache .
- func (d *Dao) GetExpireCountCache(c context.Context, k string) (count int64, err error) {
- var (
- key = expireCountKey(k)
- conn = d.redis.Get(c)
- )
- //spew.Dump(k)
- defer conn.Close()
- item, err := redis.Int(conn.Do("GET", key))
- if err != nil {
- if err == redis.ErrNil {
- count = 0
- err = nil
- } else {
- log.Error("conn.Do(GET %s) error(%v)", key, err)
- return 0, err
- }
- }
- count = int64(item)
- return
- }
- // AddExpireCountCache .
- func (d *Dao) AddExpireCountCache(c context.Context, k string, times int64) (err error) {
- var (
- key = expireCountKey(k)
- conn = d.redis.Get(c)
- )
- //spew.Dump(k)
- defer conn.Close()
- if _, err = conn.Do("INCR", key); err != nil {
- log.Error("conn.Do(incr,%v) err(%v)", key, err)
- }
- if _, err = redis.Bool(conn.Do("EXPIRE", key, times)); err != nil {
- log.Error("conn.Do(EXPIRE, %s, %d) error(%v)", key, times, err)
- return
- }
- return
- }
- // ClearExpireCountCache .
- func (d *Dao) ClearExpireCountCache(c context.Context, k string) (err error) {
- var (
- key = expireCountKey(k)
- conn = d.redis.Get(c)
- )
- //spew.Dump(k)
- defer conn.Close()
- if _, err = conn.Do("DEL", key); err != nil {
- log.Error("conn.Do(del,%v) err(%v)", key, err)
- return
- }
- return
- }
- //SetNxLock redis lock.
- func (d *Dao) SetNxLock(c context.Context, k string, times int64) (res bool, err error) {
- var (
- key = lockKey(k)
- conn = d.redis.Get(c)
- )
- defer conn.Close()
- if res, err = redis.Bool(conn.Do("SETNX", key, "1")); err != nil {
- log.Error("conn.Do(SETNX(%d)) error(%v)", key, err)
- return
- }
- //spew.Dump(res, err )
- if res {
- if _, err = redis.Bool(conn.Do("EXPIRE", key, times)); err != nil {
- log.Error("conn.Do(EXPIRE, %s, %d) error(%v)", key, times, err)
- return
- }
- log.Info("conn.Do(EXPIRE, %s, %d) ", key, times)
- }
- return
- }
|