RemoteCache: remove count method (#91581)

* remove count method

* remove count from remote cache

---------

Co-authored-by: jguer <me@jguer.space>
stars-as-collection
Karl Persson 11 months ago committed by GitHub
parent d84fd94936
commit d93f5bab83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 16
      pkg/infra/remotecache/database_storage.go
  2. 40
      pkg/infra/remotecache/database_storage_test.go
  3. 4
      pkg/infra/remotecache/memcached_storage.go
  4. 1
      pkg/infra/remotecache/memcached_storage_integration_test.go
  5. 9
      pkg/infra/remotecache/redis_storage.go
  6. 1
      pkg/infra/remotecache/redis_storage_integration_test.go
  7. 18
      pkg/infra/remotecache/remotecache.go
  8. 32
      pkg/infra/remotecache/remotecache_test.go
  9. 4
      pkg/infra/remotecache/test_utils.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 // CacheData is the struct representing the table in the database
type CacheData struct { type CacheData struct {
CacheKey string CacheKey string

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log" "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) err = db.Set(context.Background(), "killa-gorilla", obj, 0)
assert.Equal(t, err, nil) 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)
}

@ -57,10 +57,6 @@ func (s *memcachedStorage) Get(ctx context.Context, key string) ([]byte, error)
return memcachedItem.Value, nil 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 // Delete delete a key from the cache
func (s *memcachedStorage) Delete(ctx context.Context, key string) error { func (s *memcachedStorage) Delete(ctx context.Context, key string) error {
return s.c.Delete(key) return s.c.Delete(key)

@ -20,5 +20,4 @@ func TestIntegrationMemcachedCacheStorage(t *testing.T) {
opts := &setting.RemoteCacheOptions{Name: memcachedCacheType, ConnStr: u} opts := &setting.RemoteCacheOptions{Name: memcachedCacheType, ConnStr: u}
client := createTestClient(t, opts, nil) client := createTestClient(t, opts, nil)
runTestsForClient(t, client) runTestsForClient(t, client)
runCountTestsForClient(t, opts, nil)
} }

@ -110,12 +110,3 @@ func (s *redisStorage) Delete(ctx context.Context, key string) error {
cmd := s.c.Del(ctx, key) cmd := s.c.Del(ctx, key)
return cmd.Err() 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
}

@ -37,5 +37,4 @@ func TestIntegrationRedisCacheStorage(t *testing.T) {
opts := &setting.RemoteCacheOptions{Name: redisCacheType, ConnStr: b.String()} opts := &setting.RemoteCacheOptions{Name: redisCacheType, ConnStr: b.String()}
client := createTestClient(t, opts, nil) client := createTestClient(t, opts, nil)
runTestsForClient(t, client) runTestsForClient(t, client)
runCountTestsForClient(t, opts, nil)
} }

@ -69,11 +69,6 @@ type CacheStorage interface {
// Delete object from cache // Delete object from cache
Delete(ctx context.Context, key string) error 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 // 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) 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. // Run starts the backend processes for cache clients.
func (ds *RemoteCache) Run(ctx context.Context) error { func (ds *RemoteCache) Run(ctx context.Context) error {
// create new interface if more clients need GC jobs // 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) 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 { type prefixCacheStorage struct {
cache CacheStorage cache CacheStorage
prefix string 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 { func (pcs *prefixCacheStorage) Delete(ctx context.Context, key string) error {
return pcs.cache.Delete(ctx, pcs.prefix+key) 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)
}

@ -42,7 +42,6 @@ func TestCachedBasedOnConfig(t *testing.T) {
client := createTestClient(t, cfg.RemoteCacheOptions, db) client := createTestClient(t, cfg.RemoteCacheOptions, db)
runTestsForClient(t, client) runTestsForClient(t, client)
runCountTestsForClient(t, cfg.RemoteCacheOptions, db)
} }
func TestInvalidCacheTypeReturnsError(t *testing.T) { func TestInvalidCacheTypeReturnsError(t *testing.T) {
@ -55,37 +54,6 @@ func runTestsForClient(t *testing.T, client CacheStorage) {
canNotFetchExpiredItems(t, client) 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) { func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
dataToCache := []byte("some bytes") dataToCache := []byte("some bytes")

@ -28,10 +28,6 @@ func (fcs FakeCacheStorage) Delete(_ context.Context, key string) error {
return nil return nil
} }
func (fcs FakeCacheStorage) Count(_ context.Context, prefix string) (int64, error) {
return int64(len(fcs.Storage)), nil
}
func NewFakeCacheStorage() FakeCacheStorage { func NewFakeCacheStorage() FakeCacheStorage {
return FakeCacheStorage{ return FakeCacheStorage{
Storage: map[string][]byte{}, Storage: map[string][]byte{},

Loading…
Cancel
Save