diff --git a/pkg/infra/remotecache/database_storage.go b/pkg/infra/remotecache/database_storage.go index af834da3b61..f57977d69ce 100644 --- a/pkg/infra/remotecache/database_storage.go +++ b/pkg/infra/remotecache/database_storage.go @@ -122,22 +122,6 @@ func (dc *databaseCache) Delete(ctx context.Context, key string) error { }) } -func (dc *databaseCache) Count(ctx context.Context, prefix string) (int64, error) { - res := int64(0) - err := dc.SQLStore.WithDbSession(ctx, func(session *db.Session) error { - sql := "SELECT COUNT(*) FROM cache_data WHERE cache_key LIKE ?" - - _, err := session.SQL(sql, prefix+"%").Get(&res) - if err != nil { - return err - } - - return nil - }) - - return res, err -} - // CacheData is the struct representing the table in the database type CacheData struct { CacheKey string diff --git a/pkg/infra/remotecache/database_storage_test.go b/pkg/infra/remotecache/database_storage_test.go index b3a64e79bb0..6c44aca3777 100644 --- a/pkg/infra/remotecache/database_storage_test.go +++ b/pkg/infra/remotecache/database_storage_test.go @@ -6,7 +6,6 @@ import ( "time" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/log" @@ -76,42 +75,3 @@ func TestSecondSet(t *testing.T) { err = db.Set(context.Background(), "killa-gorilla", obj, 0) assert.Equal(t, err, nil) } - -func TestDatabaseStorageCount(t *testing.T) { - sqlstore := db.InitTestDB(t) - - db := &databaseCache{ - SQLStore: sqlstore, - log: log.New("remotecache.database"), - } - - obj := []byte("foolbar") - - // set time.now to 2 weeks ago - var err error - getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) } - err = db.Set(context.Background(), "pref-key1", obj, 1000*time.Second) - require.NoError(t, err) - - err = db.Set(context.Background(), "pref-key2", obj, 1000*time.Second) - require.NoError(t, err) - - err = db.Set(context.Background(), "pref-key3", obj, 1000*time.Second) - require.NoError(t, err) - - // insert object that should never expire - err = db.Set(context.Background(), "pref-key4", obj, 0) - require.NoError(t, err) - - getTime = time.Now - err = db.Set(context.Background(), "pref-key5", obj, 1000*time.Second) - require.NoError(t, err) - - // run GC - db.internalRunGC() - - // try to read values - n, errC := db.Count(context.Background(), "pref-") - require.NoError(t, errC) - assert.Equal(t, int64(2), n) -} diff --git a/pkg/infra/remotecache/memcached_storage.go b/pkg/infra/remotecache/memcached_storage.go index 6149c82245d..e39dd971c2f 100644 --- a/pkg/infra/remotecache/memcached_storage.go +++ b/pkg/infra/remotecache/memcached_storage.go @@ -57,10 +57,6 @@ func (s *memcachedStorage) Get(ctx context.Context, key string) ([]byte, error) return memcachedItem.Value, nil } -func (s *memcachedStorage) Count(ctx context.Context, prefix string) (int64, error) { - return 0, ErrNotImplemented -} - // Delete delete a key from the cache func (s *memcachedStorage) Delete(ctx context.Context, key string) error { return s.c.Delete(key) diff --git a/pkg/infra/remotecache/memcached_storage_integration_test.go b/pkg/infra/remotecache/memcached_storage_integration_test.go index 38bf6fc12ea..e19b0b2e02c 100644 --- a/pkg/infra/remotecache/memcached_storage_integration_test.go +++ b/pkg/infra/remotecache/memcached_storage_integration_test.go @@ -20,5 +20,4 @@ func TestIntegrationMemcachedCacheStorage(t *testing.T) { opts := &setting.RemoteCacheOptions{Name: memcachedCacheType, ConnStr: u} client := createTestClient(t, opts, nil) runTestsForClient(t, client) - runCountTestsForClient(t, opts, nil) } diff --git a/pkg/infra/remotecache/redis_storage.go b/pkg/infra/remotecache/redis_storage.go index e97679c04f6..6f51754396d 100644 --- a/pkg/infra/remotecache/redis_storage.go +++ b/pkg/infra/remotecache/redis_storage.go @@ -110,12 +110,3 @@ func (s *redisStorage) Delete(ctx context.Context, key string) error { cmd := s.c.Del(ctx, key) return cmd.Err() } - -func (s *redisStorage) Count(ctx context.Context, prefix string) (int64, error) { - cmd := s.c.Keys(ctx, prefix+"*") - if cmd.Err() != nil { - return 0, cmd.Err() - } - - return int64(len(cmd.Val())), nil -} diff --git a/pkg/infra/remotecache/redis_storage_integration_test.go b/pkg/infra/remotecache/redis_storage_integration_test.go index 392d3bb8b44..c6dd9ae5133 100644 --- a/pkg/infra/remotecache/redis_storage_integration_test.go +++ b/pkg/infra/remotecache/redis_storage_integration_test.go @@ -37,5 +37,4 @@ func TestIntegrationRedisCacheStorage(t *testing.T) { opts := &setting.RemoteCacheOptions{Name: redisCacheType, ConnStr: b.String()} client := createTestClient(t, opts, nil) runTestsForClient(t, client) - runCountTestsForClient(t, opts, nil) } diff --git a/pkg/infra/remotecache/remotecache.go b/pkg/infra/remotecache/remotecache.go index a24b0d20308..aa7ba2073f7 100644 --- a/pkg/infra/remotecache/remotecache.go +++ b/pkg/infra/remotecache/remotecache.go @@ -69,11 +69,6 @@ type CacheStorage interface { // Delete object from cache Delete(ctx context.Context, key string) error - - // Count returns the number of items in the cache. - // Optionaly a prefix can be provided to only count items with that prefix - // DO NOT USE. Not available for memcached. - Count(ctx context.Context, prefix string) (int64, error) } // RemoteCache allows Grafana to cache data outside its own process @@ -102,11 +97,6 @@ func (ds *RemoteCache) Delete(ctx context.Context, key string) error { return ds.client.Delete(ctx, key) } -// Count returns the number of items in the cache. -func (ds *RemoteCache) Count(ctx context.Context, prefix string) (int64, error) { - return ds.client.Count(ctx, prefix) -} - // Run starts the backend processes for cache clients. func (ds *RemoteCache) Run(ctx context.Context) error { // create new interface if more clients need GC jobs @@ -173,10 +163,6 @@ func (pcs *encryptedCacheStorage) Delete(ctx context.Context, key string) error return pcs.cache.Delete(ctx, key) } -func (pcs *encryptedCacheStorage) Count(ctx context.Context, prefix string) (int64, error) { - return pcs.cache.Count(ctx, prefix) -} - type prefixCacheStorage struct { cache CacheStorage prefix string @@ -191,7 +177,3 @@ func (pcs *prefixCacheStorage) Set(ctx context.Context, key string, value []byte func (pcs *prefixCacheStorage) Delete(ctx context.Context, key string) error { return pcs.cache.Delete(ctx, pcs.prefix+key) } - -func (pcs *prefixCacheStorage) Count(ctx context.Context, prefix string) (int64, error) { - return pcs.cache.Count(ctx, pcs.prefix+prefix) -} diff --git a/pkg/infra/remotecache/remotecache_test.go b/pkg/infra/remotecache/remotecache_test.go index b05d47fb3d7..7b7a64e8d38 100644 --- a/pkg/infra/remotecache/remotecache_test.go +++ b/pkg/infra/remotecache/remotecache_test.go @@ -42,7 +42,6 @@ func TestCachedBasedOnConfig(t *testing.T) { client := createTestClient(t, cfg.RemoteCacheOptions, db) runTestsForClient(t, client) - runCountTestsForClient(t, cfg.RemoteCacheOptions, db) } func TestInvalidCacheTypeReturnsError(t *testing.T) { @@ -55,37 +54,6 @@ func runTestsForClient(t *testing.T, client CacheStorage) { canNotFetchExpiredItems(t, client) } -func runCountTestsForClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore db.DB) { - client := createTestClient(t, opts, sqlstore) - expectError := false - if opts.Name == memcachedCacheType { - expectError = true - } - - t.Run("can count items", func(t *testing.T) { - cacheableValue := []byte("hej hej") - - err := client.Set(context.Background(), "pref-key1", cacheableValue, 0) - require.NoError(t, err) - - err = client.Set(context.Background(), "pref-key2", cacheableValue, 0) - require.NoError(t, err) - - err = client.Set(context.Background(), "key3-not-pref", cacheableValue, 0) - require.NoError(t, err) - - n, errC := client.Count(context.Background(), "pref-") - if expectError { - require.ErrorIs(t, ErrNotImplemented, errC) - assert.Equal(t, int64(0), n) - return - } - - require.NoError(t, errC) - assert.Equal(t, int64(2), n) - }) -} - func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) { dataToCache := []byte("some bytes") diff --git a/pkg/infra/remotecache/test_utils.go b/pkg/infra/remotecache/test_utils.go index 64640a95f4f..96751e09b77 100644 --- a/pkg/infra/remotecache/test_utils.go +++ b/pkg/infra/remotecache/test_utils.go @@ -28,10 +28,6 @@ func (fcs FakeCacheStorage) Delete(_ context.Context, key string) error { return nil } -func (fcs FakeCacheStorage) Count(_ context.Context, prefix string) (int64, error) { - return int64(len(fcs.Storage)), nil -} - func NewFakeCacheStorage() FakeCacheStorage { return FakeCacheStorage{ Storage: map[string][]byte{},