Sfoglia il codice sorgente

adds memory as dist storage alt

bergquist 6 anni fa
parent
commit
8db2864fee

+ 12 - 0
pkg/infra/distcache/database_storage_integration_test.go

@@ -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
pkg/infra/distcache/distcache.go

@@ -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
pkg/infra/distcache/distcache_test.go

@@ -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)

+ 12 - 0
pkg/infra/distcache/memcached_storage_test.go

@@ -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 - 0
pkg/infra/distcache/memory_storage.go

@@ -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
+}