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 ...@@ -92,18 +92,18 @@ func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration
return err return err
} }
var expiresAtEpoch int64 var expiresInSeconds int64
if expire != 0 { if expire != 0 {
expiresAtEpoch = int64(expire) / int64(time.Second) expiresInSeconds = int64(expire) / int64(time.Second)
} }
// insert or update depending on if item already exist // insert or update depending on if item already exist
if has { if has {
sql := `UPDATE cache_data SET data=?, created=?, expire=? WHERE cache_key='?'` 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 { } else {
sql := `INSERT INTO cache_data (cache_key,data,created_at,expires) VALUES(?,?,?,?)` 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 return err
......
...@@ -33,7 +33,12 @@ func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duratio ...@@ -33,7 +33,12 @@ func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duratio
return err 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) return s.c.Set(memcachedItem)
} }
......
...@@ -27,13 +27,7 @@ func (s *redisStorage) Set(key string, val interface{}, expires time.Duration) e ...@@ -27,13 +27,7 @@ func (s *redisStorage) Set(key string, val interface{}, expires time.Duration) e
return err return err
} }
var status *redis.StatusCmd status := s.c.SetEx(key, expires, string(value))
if expires == 0 {
status = s.c.Set(key, string(value))
} else {
status = s.c.SetEx(key, expires, string(value))
}
return status.Err() return status.Err()
} }
......
...@@ -21,6 +21,8 @@ var ( ...@@ -21,6 +21,8 @@ var (
// ErrInvalidCacheType is returned if the type is invalid // ErrInvalidCacheType is returned if the type is invalid
ErrInvalidCacheType = errors.New("invalid remote cache name") ErrInvalidCacheType = errors.New("invalid remote cache name")
defaultMaxCacheExpiration = time.Hour * 24
) )
func init() { func init() {
...@@ -35,7 +37,7 @@ type CacheStorage interface { ...@@ -35,7 +37,7 @@ type CacheStorage interface {
// Get reads object from Cache // Get reads object from Cache
Get(key string) (interface{}, error) 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 Set(key string, value interface{}, expire time.Duration) error
// Delete object from cache // Delete object from cache
...@@ -50,14 +52,21 @@ type RemoteCache struct { ...@@ -50,14 +52,21 @@ type RemoteCache struct {
Cfg *setting.Cfg `inject:""` Cfg *setting.Cfg `inject:""`
} }
// Get reads object from Cache
func (ds *RemoteCache) Get(key string) (interface{}, error) { func (ds *RemoteCache) Get(key string) (interface{}, error) {
return ds.client.Get(key) 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 { func (ds *RemoteCache) Set(key string, value interface{}, expire time.Duration) error {
if expire == 0 {
expire = defaultMaxCacheExpiration
}
return ds.client.Set(key, value, expire) return ds.client.Set(key, value, expire)
} }
// Delete object from cache
func (ds *RemoteCache) Delete(key string) error { func (ds *RemoteCache) Delete(key string) error {
return ds.client.Delete(key) return ds.client.Delete(key)
} }
......
...@@ -34,7 +34,7 @@ func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore * ...@@ -34,7 +34,7 @@ func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore *
t.Fatalf("failed to init client for test. error: %v", err) t.Fatalf("failed to init client for test. error: %v", err)
} }
return dc.client return dc
} }
func TestCachedBasedOnConfig(t *testing.T) { func TestCachedBasedOnConfig(t *testing.T) {
...@@ -56,7 +56,6 @@ func TestInvalidCacheTypeReturnsError(t *testing.T) { ...@@ -56,7 +56,6 @@ func TestInvalidCacheTypeReturnsError(t *testing.T) {
func runTestsForClient(t *testing.T, client CacheStorage) { func runTestsForClient(t *testing.T, client CacheStorage) {
canPutGetAndDeleteCachedObjects(t, client) canPutGetAndDeleteCachedObjects(t, client)
canNotFetchExpiredItems(t, client) canNotFetchExpiredItems(t, client)
canSetInfiniteCacheExpiration(t, client)
} }
func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) { func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
...@@ -92,21 +91,3 @@ func canNotFetchExpiredItems(t *testing.T, client CacheStorage) { ...@@ -92,21 +91,3 @@ func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
_, err = client.Get("key1") _, err = client.Get("key1")
assert.Equal(t, err, ErrCacheItemNotFound) 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