123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package dao
- import (
- "context"
- "fmt"
- "go-common/library/cache/redis"
- "go-common/library/log"
- )
- const (
- _prefixLimitDay = "ld_%d_%d_%s"
- _prefixLimitBiz = "lb_%d_%d_%s_%d"
- _prefixLimitNotLive = "lnl_%d_%s"
- )
- func limitDayKey(day string, app, mid int64) string {
- return fmt.Sprintf(_prefixLimitDay, app, mid, day)
- }
- func limitBizKey(day string, app, mid, biz int64) string {
- return fmt.Sprintf(_prefixLimitBiz, app, mid, day, biz)
- }
- func limitNotLiveKey(day string, mid int64) string {
- return fmt.Sprintf(_prefixLimitNotLive, mid, day)
- }
- // pingRedis ping redis.
- func (d *Dao) pingRedis(ctx context.Context) (err error) {
- conn := d.redis.Get(ctx)
- defer conn.Close()
- if _, err = conn.Do("SET", "PING", "PONG"); err != nil {
- PromError("redis: ping remote")
- log.Error("remote redis: conn.Do(SET,PING,PONG) error(%v)", err)
- }
- return
- }
- // LimitDayCache gets limit cache by day & mid.
- // 测试用,业务用不着
- func (d *Dao) LimitDayCache(ctx context.Context, day string, app, mid int64) (count int, err error) {
- var (
- key = limitDayKey(day, app, mid)
- conn = d.redis.Get(ctx)
- )
- defer conn.Close()
- if count, err = redis.Int(conn.Do("GET", key)); err != nil {
- PromError("redis:LimitDayCache")
- log.Error("LimitDayCache(%s,%d,%d) error(%v)", day, app, mid, err)
- }
- return
- }
- // IncrLimitDayCache increases and gets limit cache by day & mid.
- func (d *Dao) IncrLimitDayCache(ctx context.Context, day string, app, mid int64) (count int, err error) {
- var (
- key = limitDayKey(day, app, mid)
- conn = d.redis.Get(ctx)
- )
- defer conn.Close()
- if err = conn.Send("INCR", key); err != nil {
- PromError("redis:IncrLimitDayCache")
- log.Error("IncrLimitDayCache(%s,%d,%d) error(%v)", day, app, mid, err)
- return
- }
- if err = conn.Send("EXPIRE", key, d.redisLimitDayExpire); err != nil {
- PromError("redis:IncrLimitDayCache:expire")
- log.Error("IncrLimitDayCache(%s,%d,%d) expire error(%v)", day, app, mid, err)
- return
- }
- if err = conn.Flush(); err != nil {
- PromError("redis:IncrLimitDayCache:flush")
- log.Error("IncrLimitDayCache(%s,%d,%d) flush error(%v)", day, app, mid, err)
- return
- }
- if count, err = redis.Int(conn.Receive()); err != nil {
- PromError("redis:IncrLimitDayCache:receive:incr")
- log.Error("IncrLimitDayCache(%s,%d,%d) receive incr error(%+v)", day, app, mid, err)
- return
- }
- if _, err = conn.Receive(); err != nil {
- PromError("redis:IncrLimitDayCache:receive:expire")
- log.Error("IncrLimitDayCache(%s,%d,%d) receive expire error(%+v)", day, app, mid, err)
- }
- return
- }
- // IncrLimitBizCache increases and gets limit cache by day & mid & bisiness.
- func (d *Dao) IncrLimitBizCache(ctx context.Context, day string, app, mid, biz int64) (count int, err error) {
- var (
- key = limitBizKey(day, app, mid, biz)
- conn = d.redis.Get(ctx)
- )
- defer conn.Close()
- if err = conn.Send("INCR", key); err != nil {
- PromError("redis:IncrLimitBizCache")
- log.Error("IncrLimitBizCache(%s,%d,%d,%d) error(%v)", day, app, mid, biz, err)
- return
- }
- if err = conn.Send("EXPIRE", key, d.redisLimitDayExpire); err != nil {
- PromError("redis:IncrLimitBizCache:expire")
- log.Error("IncrLimitBizCache(%s,%d,%d,%d) expire error(%v)", day, app, mid, biz, err)
- return
- }
- if err = conn.Flush(); err != nil {
- PromError("redis:IncrLimitBizCache:flush")
- log.Error("IncrLimitBizCache(%s,%d,%d,%d) flush error(%v)", day, app, mid, biz, err)
- return
- }
- if count, err = redis.Int(conn.Receive()); err != nil {
- PromError("redis:IncrLimitBizCache:receive:incr")
- log.Error("IncrLimitBizCache(%s,%d,%d,%d) receive incr error(%+v)", day, app, mid, biz, err)
- return
- }
- if _, err = conn.Receive(); err != nil {
- PromError("redis:IncrLimitBizCache:receive:expire")
- log.Error("IncrLimitBizCache(%s,%d,%d,%d) receive expire error(%+v)", day, app, mid, biz, err)
- }
- return
- }
- // IncrLimitNotLiveCache increases and gets not live limit cache by day & mid.
- func (d *Dao) IncrLimitNotLiveCache(ctx context.Context, day string, mid int64) (count int, err error) {
- var (
- key = limitNotLiveKey(day, mid)
- conn = d.redis.Get(ctx)
- )
- defer conn.Close()
- if err = conn.Send("INCR", key); err != nil {
- PromError("redis:IncrLimitNotLiveCache")
- log.Error("IncrLimitNotLiveCache(%s,%d) error(%v)", day, mid, err)
- return
- }
- if err = conn.Send("EXPIRE", key, d.redisLimitDayExpire); err != nil {
- PromError("redis:IncrLimitNotLiveCache:expire")
- log.Error("IncrLimitNotLiveCache(%s,%d) expire error(%v)", day, mid, err)
- return
- }
- if err = conn.Flush(); err != nil {
- PromError("redis:IncrLimitNotLiveCache:flush")
- log.Error("IncrLimitNotLiveCache(%s,%d) flush error(%v)", day, mid, err)
- return
- }
- if count, err = redis.Int(conn.Receive()); err != nil {
- PromError("redis:IncrLimitNotLiveCache:receive:incr")
- log.Error("IncrLimitNotLiveCache(%s,%d) receive incr error(%+v)", day, mid, err)
- return
- }
- if _, err = conn.Receive(); err != nil {
- PromError("redis:IncrLimitNotLiveCache:receive:expire")
- log.Error("IncrLimitNotLiveCache(%s,%d) receive expire error(%+v)", day, mid, err)
- }
- return
- }
|