Skip to content

Commit

Permalink
extend redis after get
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Apr 7, 2017
1 parent 53cc86a commit bf815df
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (

// Cache implement Cache interface
type Cache struct {
Pool *redis.Pool
Prefix string
TTL time.Duration
Skip func(*datastore.Key) bool
Pool *redis.Pool
Prefix string
TTL time.Duration
ExtendTTL bool
Skip func(*datastore.Key) bool
}

func encode(v interface{}) ([]byte, error) {
Expand All @@ -40,13 +41,17 @@ func (cache *Cache) Get(key *datastore.Key, dst interface{}) error {

db := cache.Pool.Get()
defer db.Close()
b, err := redis.Bytes(db.Do("GET", cache.Prefix+key.String()))
k := cache.Prefix + key.String()
b, err := redis.Bytes(db.Do("GET", k))
if err != nil {
return err
}
if len(b) == 0 {
return ds.ErrCacheNotFound
}
if cache.ExtendTTL {
db.Do("EXPIRE", k, int(cache.TTL/time.Second))
}
return decode(b, dst)
}

Expand All @@ -70,6 +75,13 @@ func (cache *Cache) GetMulti(keys []*datastore.Key, dst interface{}) error {
decode(b, reflect.Indirect(reflect.ValueOf(dst)).Index(i).Interface())
}
}
if cache.ExtendTTL {
ttl := int(cache.TTL / time.Second)
for _, key := range keys {
db.Send("EXPIRE", cache.Prefix+key.String(), ttl)
}
db.Flush()
}
return nil
}

Expand Down Expand Up @@ -100,6 +112,7 @@ func (cache *Cache) Set(key *datastore.Key, src interface{}) error {
func (cache *Cache) SetMulti(keys []*datastore.Key, src interface{}) error {
db := cache.Pool.Get()
defer db.Close()
ttl := int(cache.TTL / time.Second)
db.Send("MULTI")
for i, key := range keys {
if key == nil {
Expand All @@ -113,7 +126,7 @@ func (cache *Cache) SetMulti(keys []*datastore.Key, src interface{}) error {
return err
}
if cache.TTL > 0 {
db.Send("SETEX", cache.Prefix+key.String(), int(cache.TTL/time.Second), b)
db.Send("SETEX", cache.Prefix+key.String(), ttl, b)
}
db.Send("SET", cache.Prefix+key.String(), b)
}
Expand Down

0 comments on commit bf815df

Please sign in to comment.