adds memory as dist storage alt
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user