Commit c001cfe1 by bergquist

dont allow inifinite expiration

parent 51862737
......@@ -92,18 +92,18 @@ func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration
return err
}
var expiresAtEpoch int64
var expiresInSeconds int64
if expire != 0 {
expiresAtEpoch = int64(expire) / int64(time.Second)
expiresInSeconds = int64(expire) / int64(time.Second)
}
// insert or update depending on if item already exist
if has {
sql := `UPDATE cache_data SET data=?, created=?, expire=? WHERE cache_key='?'`
_, err = session.Exec(sql, data, getTime().Unix(), expiresAtEpoch, key)
_, err = session.Exec(sql, data, getTime().Unix(), expiresInSeconds, key)
} else {
sql := `INSERT INTO cache_data (cache_key,data,created_at,expires) VALUES(?,?,?,?)`
_, err = session.Exec(sql, key, data, getTime().Unix(), expiresAtEpoch)
_, err = session.Exec(sql, key, data, getTime().Unix(), expiresInSeconds)
}
return err
......
......@@ -33,7 +33,12 @@ func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duratio
return err
}
memcachedItem := newItem(key, bytes, int32(expires))
var expiresInSeconds int64
if expires != 0 {
expiresInSeconds = int64(expires) / int64(time.Second)
}
memcachedItem := newItem(key, bytes, int32(expiresInSeconds))
return s.c.Set(memcachedItem)
}
......
......@@ -27,13 +27,7 @@ func (s *redisStorage) Set(key string, val interface{}, expires time.Duration) e
return err
}
var status *redis.StatusCmd
if expires == 0 {
status = s.c.Set(key, string(value))
} else {
status = s.c.SetEx(key, expires, string(value))
}
status := s.c.SetEx(key, expires, string(value))
return status.Err()
}
......
......@@ -21,6 +21,8 @@ var (
// ErrInvalidCacheType is returned if the type is invalid
ErrInvalidCacheType = errors.New("invalid remote cache name")
defaultMaxCacheExpiration = time.Hour * 24
)
func init() {
......@@ -35,7 +37,7 @@ type CacheStorage interface {
// Get reads object from Cache
Get(key string) (interface{}, error)
// Set sets an object into the cache. if `expire` is set to zero it never expires.
// Set sets an object into the cache. if `expire` is set to zero it will default to 24h
Set(key string, value interface{}, expire time.Duration) error
// Delete object from cache
......@@ -50,14 +52,21 @@ type RemoteCache struct {
Cfg *setting.Cfg `inject:""`
}
// Get reads object from Cache
func (ds *RemoteCache) Get(key string) (interface{}, error) {
return ds.client.Get(key)
}
// Set sets an object into the cache. if `expire` is set to zero it will default to 24h
func (ds *RemoteCache) Set(key string, value interface{}, expire time.Duration) error {
if expire == 0 {
expire = defaultMaxCacheExpiration
}
return ds.client.Set(key, value, expire)
}
// Delete object from cache
func (ds *RemoteCache) Delete(key string) error {
return ds.client.Delete(key)
}
......
......@@ -34,7 +34,7 @@ func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore *
t.Fatalf("failed to init client for test. error: %v", err)
}
return dc.client
return dc
}
func TestCachedBasedOnConfig(t *testing.T) {
......@@ -56,7 +56,6 @@ func TestInvalidCacheTypeReturnsError(t *testing.T) {
func runTestsForClient(t *testing.T, client CacheStorage) {
canPutGetAndDeleteCachedObjects(t, client)
canNotFetchExpiredItems(t, client)
canSetInfiniteCacheExpiration(t, client)
}
func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
......@@ -92,21 +91,3 @@ func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
_, err = client.Get("key1")
assert.Equal(t, err, ErrCacheItemNotFound)
}
func canSetInfiniteCacheExpiration(t *testing.T, client CacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
// insert cache item one day back
getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
err := client.Set("key1", cacheableStruct, 0)
assert.Equal(t, err, nil)
// should not be able to read that value since its expired
getTime = time.Now
data, err := client.Get("key1")
s, ok := data.(CacheableStruct)
assert.Equal(t, ok, true)
assert.Equal(t, s.String, "hej")
assert.Equal(t, s.Int64, int64(2000))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment