|
| 1 | +package rcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/gomodule/redigo/redis" |
| 8 | + "github.com/pkg/errors" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + keyPattern = "%s:%s" |
| 13 | + datePattern = "2006_01_02" |
| 14 | + secondsInDay = 60 * 60 * 24 |
| 15 | +) |
| 16 | + |
| 17 | +var hasTask = redis.NewScript(3, |
| 18 | + `-- KEYS: [TodayKey, YesterdayKey, Key] |
| 19 | + local value = redis.call("hget", KEYS[1], KEYS[3]) |
| 20 | + if (value ~= nil) then |
| 21 | + return value |
| 22 | + end |
| 23 | + return redis.call("hget", KEYS[2], KEYS[3]) |
| 24 | +`) |
| 25 | + |
| 26 | +// Get returns the cached value for key in the passed in group or nil |
| 27 | +func Get(rc redis.Conn, group string, key string) (string, error) { |
| 28 | + todayKey := fmt.Sprintf(keyPattern, group, time.Now().UTC().Format(datePattern)) |
| 29 | + yesterdayKey := fmt.Sprintf(keyPattern, group, time.Now().Add(time.Hour*-24).UTC().Format(datePattern)) |
| 30 | + value, err := redis.String(hasTask.Do(rc, todayKey, yesterdayKey, key)) |
| 31 | + if err != nil && err != redis.ErrNil { |
| 32 | + return "", errors.Wrapf(err, "error getting value for group: %s and key: %s", group, key) |
| 33 | + } |
| 34 | + return value, nil |
| 35 | +} |
| 36 | + |
| 37 | +// Set sets the cached value for key for the passed in group. It will be cached for at least 24 hours but no longer than 48 hours |
| 38 | +func Set(rc redis.Conn, group string, key string, value string) error { |
| 39 | + dateKey := fmt.Sprintf(keyPattern, group, time.Now().UTC().Format(datePattern)) |
| 40 | + rc.Send("hset", dateKey, key, value) |
| 41 | + rc.Send("expire", dateKey, secondsInDay) |
| 42 | + _, err := rc.Do("") |
| 43 | + if err != nil { |
| 44 | + return errors.Wrapf(err, "error setting value for group: %s, key: %s, value: %s", group, key, value) |
| 45 | + } |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// Delete removes the value with the passed in key |
| 50 | +func Delete(rc redis.Conn, group string, key string) error { |
| 51 | + todayKey := fmt.Sprintf(keyPattern, group, time.Now().UTC().Format(datePattern)) |
| 52 | + yesterdayKey := fmt.Sprintf(keyPattern, group, time.Now().Add(time.Hour*-24).UTC().Format(datePattern)) |
| 53 | + rc.Send("hdel", todayKey, key) |
| 54 | + rc.Send("hdel", yesterdayKey, key) |
| 55 | + _, err := rc.Do("") |
| 56 | + if err != nil { |
| 57 | + return errors.Wrapf(err, "error deleting value for group: %s and key: %s", group, key) |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// Clear removes all values for the passed in group |
| 63 | +func Clear(rc redis.Conn, group string) error { |
| 64 | + todayKey := fmt.Sprintf(keyPattern, group, time.Now().UTC().Format(datePattern)) |
| 65 | + yesterdayKey := fmt.Sprintf(keyPattern, group, time.Now().Add(time.Hour*-24).UTC().Format(datePattern)) |
| 66 | + rc.Send("del", todayKey) |
| 67 | + rc.Send("del", yesterdayKey) |
| 68 | + _, err := rc.Do("") |
| 69 | + return err |
| 70 | +} |
0 commit comments