2022-01-13 18:13:41 -08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCacheClient(t *testing.T) {
|
|
|
|
type cacheTest struct {
|
|
|
|
Value string
|
|
|
|
}
|
2024-06-22 07:34:26 -07:00
|
|
|
|
2022-01-13 18:13:41 -08:00
|
|
|
// Cache some data
|
|
|
|
data := cacheTest{Value: "abcdef"}
|
|
|
|
group := "testgroup"
|
|
|
|
key := "testkey"
|
|
|
|
err := c.Cache.
|
|
|
|
Set().
|
|
|
|
Group(group).
|
|
|
|
Key(key).
|
|
|
|
Data(data).
|
2024-06-22 07:34:26 -07:00
|
|
|
Expiration(500 * time.Millisecond).
|
2022-01-13 18:13:41 -08:00
|
|
|
Save(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Get the data
|
|
|
|
fromCache, err := c.Cache.
|
|
|
|
Get().
|
|
|
|
Group(group).
|
|
|
|
Key(key).
|
|
|
|
Fetch(context.Background())
|
|
|
|
require.NoError(t, err)
|
2024-06-22 07:34:26 -07:00
|
|
|
cast, ok := fromCache.(cacheTest)
|
2022-01-13 18:13:41 -08:00
|
|
|
require.True(t, ok)
|
2024-06-22 07:34:26 -07:00
|
|
|
assert.Equal(t, data, cast)
|
2022-01-13 18:13:41 -08:00
|
|
|
|
|
|
|
// The same key with the wrong group should fail
|
|
|
|
_, err = c.Cache.
|
|
|
|
Get().
|
|
|
|
Key(key).
|
|
|
|
Fetch(context.Background())
|
2024-06-22 07:34:26 -07:00
|
|
|
assert.Equal(t, ErrCacheMiss, err)
|
2022-01-13 18:13:41 -08:00
|
|
|
|
|
|
|
// Flush the data
|
|
|
|
err = c.Cache.
|
|
|
|
Flush().
|
|
|
|
Group(group).
|
|
|
|
Key(key).
|
2022-01-19 06:14:18 -08:00
|
|
|
Execute(context.Background())
|
2022-01-13 18:13:41 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// The data should be gone
|
2024-06-22 07:34:26 -07:00
|
|
|
assertFlushed := func(key string) {
|
2022-01-13 18:13:41 -08:00
|
|
|
// The data should be gone
|
|
|
|
_, err = c.Cache.
|
|
|
|
Get().
|
|
|
|
Group(group).
|
|
|
|
Key(key).
|
|
|
|
Fetch(context.Background())
|
2024-06-22 07:34:26 -07:00
|
|
|
assert.Equal(t, ErrCacheMiss, err)
|
2022-01-13 18:13:41 -08:00
|
|
|
}
|
2024-06-22 07:34:26 -07:00
|
|
|
assertFlushed(key)
|
2022-01-13 18:13:41 -08:00
|
|
|
|
|
|
|
// Set with tags
|
2024-06-22 07:34:26 -07:00
|
|
|
key = "testkey2"
|
2022-01-13 18:13:41 -08:00
|
|
|
err = c.Cache.
|
|
|
|
Set().
|
|
|
|
Group(group).
|
|
|
|
Key(key).
|
|
|
|
Data(data).
|
2024-06-22 07:34:26 -07:00
|
|
|
Tags("tag1", "tag2").
|
|
|
|
Expiration(time.Hour).
|
2022-01-13 18:13:41 -08:00
|
|
|
Save(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-06-22 07:34:26 -07:00
|
|
|
// Check the tag index
|
|
|
|
index := c.Cache.store.(*inMemoryCacheStore).tagIndex
|
|
|
|
gk := c.Cache.cacheKey(group, key)
|
|
|
|
_, exists := index.tags["tag1"][gk]
|
|
|
|
assert.True(t, exists)
|
|
|
|
_, exists = index.tags["tag2"][gk]
|
|
|
|
assert.True(t, exists)
|
|
|
|
_, exists = index.keys[gk]["tag1"]
|
|
|
|
assert.True(t, exists)
|
|
|
|
_, exists = index.keys[gk]["tag2"]
|
|
|
|
assert.True(t, exists)
|
|
|
|
|
|
|
|
// Flush one of tags
|
2022-01-13 18:13:41 -08:00
|
|
|
err = c.Cache.
|
|
|
|
Flush().
|
2022-01-14 10:07:19 -08:00
|
|
|
Tags("tag1").
|
2022-01-19 06:14:18 -08:00
|
|
|
Execute(context.Background())
|
2022-01-13 18:13:41 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// The data should be gone
|
2024-06-22 07:34:26 -07:00
|
|
|
assertFlushed(key)
|
2022-01-13 18:13:41 -08:00
|
|
|
|
2024-06-22 07:34:26 -07:00
|
|
|
// The index should be empty
|
|
|
|
assert.Empty(t, index.tags)
|
|
|
|
assert.Empty(t, index.keys)
|
2022-01-13 18:13:41 -08:00
|
|
|
}
|