adds memory as dist storage alt

This commit is contained in:
bergquist
2019-03-11 10:49:09 +01:00
parent a60bb83a70
commit 8db2864fee
5 changed files with 63 additions and 5 deletions
@@ -0,0 +1,12 @@
package distcache
import "testing"
func TestIntegrationDatabaseCacheStorage(t *testing.T) {
client := createTestClient(t, "database")
CanPutGetAndDeleteCachedObjects(t, client)
CanNotFetchExpiredItems(t, client)
CanSetInfiniteCacheExpiration(t, client)
}
+3 -3
View File
@@ -48,9 +48,9 @@ func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
return newMemcacheStorage("localhost:11211")
}
// if opts.name == "memory" {
// return nil
// }
if opts.name == "memory" {
return newMemoryStorage()
}
return newDatabaseCache(sqlstore)
}
+1 -2
View File
@@ -27,8 +27,7 @@ func createTestClient(t *testing.T, name string) cacheStorage {
}
func TestAllCacheClients(t *testing.T) {
//clients := []string{"database", "redis", "memcache"} // add redis, memcache, memory
clients := []string{} // add redis, memcache, memory
clients := []string{"memory"} // add redis, memcache, memory
for _, v := range clients {
client := createTestClient(t, v)
@@ -0,0 +1,12 @@
package distcache
import "testing"
func TestMemcachedCacheStorage(t *testing.T) {
client := createTestClient(t, "memcache")
CanPutGetAndDeleteCachedObjects(t, client)
CanNotFetchExpiredItems(t, client)
CanSetInfiniteCacheExpiration(t, client)
}
+35
View File
@@ -0,0 +1,35 @@
package distcache
import (
"time"
gocache "github.com/patrickmn/go-cache"
)
type memoryStorage struct {
c *gocache.Cache
}
func newMemoryStorage() *memoryStorage {
return &memoryStorage{
c: gocache.New(time.Minute*30, time.Minute*30),
}
}
func (s *memoryStorage) Put(key string, val interface{}, expires time.Duration) error {
return s.c.Add(key, val, expires)
}
func (s *memoryStorage) Get(key string) (interface{}, error) {
val, exist := s.c.Get(key)
if !exist {
return nil, ErrCacheItemNotFound
}
return val, nil
}
func (s *memoryStorage) Delete(key string) error {
s.c.Delete(key)
return nil
}