-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b576e44
commit 172ca52
Showing
15 changed files
with
359 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,10 @@ jobs: | |
uses: docker/setup-buildx-action@v3 | ||
- name: Run Unittest | ||
run: make test | ||
- name: Upload coverage reports to Codecov | ||
uses: codecov/[email protected] | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
|
||
# build_docker_image: | ||
# name: Build And Push Docker Image | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Package dcache implements a distributed cache Interface | ||
// redis.go implements a distributed cache with redis | ||
package dcache | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
|
||
"github.com/beiai0xff/turl/pkg/cache" | ||
redis2 "github.com/beiai0xff/turl/pkg/db/redis" | ||
) | ||
|
||
var _ cache.Interface = (*redisCache)(nil) | ||
|
||
type redisCache struct { | ||
rdb redis.UniversalClient | ||
// bucketSize int | ||
} | ||
|
||
// NewRedis returns a new redis cache | ||
func NewRedis(addr []string) cache.Interface { | ||
return newRedisCache(addr) | ||
} | ||
|
||
func newRedisCache(addr []string) *redisCache { | ||
return &redisCache{ | ||
rdb: redis2.Client(addr), | ||
} | ||
} | ||
|
||
// Set the k v pair to the cache | ||
func (c *redisCache) Set(ctx context.Context, k string, v []byte, ttl time.Duration) error { | ||
return c.rdb.SetEx(ctx, k, v, ttl).Err() | ||
} | ||
|
||
// Get the value by key | ||
func (c *redisCache) Get(ctx context.Context, k string) ([]byte, error) { | ||
value, err := c.rdb.Get(ctx, k).Bytes() | ||
if err != nil && errors.Is(err, redis.Nil) { | ||
return nil, cache.ErrCacheMiss | ||
} | ||
|
||
return value, err | ||
} | ||
|
||
// Close the cache | ||
func (c *redisCache) Close() error { | ||
return c.rdb.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package dcache | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/beiai0xff/turl/pkg/cache" | ||
"github.com/beiai0xff/turl/test" | ||
) | ||
|
||
func TestNewRedisCache(t *testing.T) { | ||
got := NewRedis(test.RedisAddr) | ||
require.NotNil(t, got) | ||
} | ||
|
||
func Test_newRedisCache(t *testing.T) { | ||
got := newRedisCache(test.RedisAddr) | ||
require.NotNil(t, got) | ||
} | ||
|
||
func Test_redisCache_Set(t *testing.T) { | ||
c := newRedisCache(test.RedisAddr) | ||
t.Cleanup( | ||
func() { | ||
c.Close() | ||
}) | ||
|
||
ctx := context.Background() | ||
k, v, ttl := "key", []byte("value"), time.Minute | ||
require.NoError(t, c.Set(ctx, k, v, ttl)) | ||
} | ||
|
||
func Test_redisCache_Get(t *testing.T) { | ||
c := newRedisCache(test.RedisAddr) | ||
t.Cleanup( | ||
func() { | ||
c.Close() | ||
}) | ||
|
||
ctx := context.Background() | ||
k, v, ttl := "key_get", []byte("value"), time.Minute | ||
require.NoError(t, c.Set(ctx, k, v, ttl)) | ||
got, err := c.Get(ctx, k) | ||
require.NoError(t, err) | ||
require.Equal(t, v, got) | ||
|
||
// test cache miss | ||
got, err = c.Get(ctx, "empty") | ||
require.ErrorIs(t, err, cache.ErrCacheMiss) | ||
require.Nil(t, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Package cache provides the cache management interface define | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
) | ||
|
||
// ErrCacheMiss cache miss error | ||
// if Get() method return this error, means key is not exist | ||
var ErrCacheMiss = errors.New("cache: key is missing") | ||
|
||
// Interface cache interface | ||
type Interface interface { | ||
// Set the key value to cache | ||
Set(ctx context.Context, k string, v []byte, ttl time.Duration) error | ||
// Get the key value from cache | ||
Get(ctx context.Context, k string) ([]byte, error) | ||
// Close the cache | ||
Close() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package lcahce | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/allegro/bigcache/v3" | ||
) | ||
|
||
func Benchmark_bigCache_Set(b *testing.B) { | ||
config := bigcache.DefaultConfig(10 * time.Minute) | ||
config.MaxEntriesInWindow = 1e6 | ||
|
||
c, _ := bigcache.New(context.Background(), config) | ||
|
||
v := []byte("https://www.abc.com/images/100040.jpg\n") | ||
|
||
keys := make([]string, 0, b.N) | ||
for i := range b.N { | ||
keys = append(keys, strconv.Itoa(i+10000)) | ||
} | ||
|
||
b.ResetTimer() | ||
for i := range b.N { | ||
_ = c.Set(keys[i], v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Package lcahce provides the local cache | ||
package lcahce | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/allegro/bigcache/v3" | ||
|
||
"github.com/beiai0xff/turl/pkg/cache" | ||
) | ||
|
||
var ( | ||
_ cache.Interface = (*localCache)(nil) | ||
errInvalidCap = errors.New("cache: invalid capacity") | ||
) | ||
|
||
type localCache struct { | ||
cache *bigcache.BigCache | ||
} | ||
|
||
// New create a local cache | ||
// capacity is the cache capacity | ||
// ttl is the time to live | ||
func New(capacity int, ttl time.Duration) (cache.Interface, error) { | ||
return newLocalCache(capacity, ttl) | ||
} | ||
|
||
func newLocalCache(capacity int, ttl time.Duration) (*localCache, error) { | ||
if capacity <= 0 { | ||
return nil, errInvalidCap | ||
} | ||
|
||
config := bigcache.DefaultConfig(ttl) | ||
config.MaxEntriesInWindow = capacity | ||
|
||
c, err := bigcache.New(context.Background(), config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &localCache{cache: c}, err | ||
} | ||
|
||
// Set the k v pair to the cache | ||
// Note that the duration is not used | ||
func (l *localCache) Set(_ context.Context, k string, v []byte, _ time.Duration) error { | ||
return l.cache.Set(k, v) | ||
} | ||
|
||
// Get the value by key | ||
func (l *localCache) Get(_ context.Context, k string) ([]byte, error) { | ||
v, err := l.cache.Get(k) | ||
if err != nil && errors.Is(err, bigcache.ErrEntryNotFound) { | ||
return nil, cache.ErrCacheMiss | ||
} | ||
|
||
return v, err | ||
} | ||
|
||
// Close the cache | ||
func (l *localCache) Close() error { | ||
return l.cache.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package lcahce | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/beiai0xff/turl/pkg/cache" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
c, err := New(1e6, 10*time.Minute) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
c.Close() | ||
}) | ||
require.NotNil(t, c) | ||
} | ||
|
||
func Test_newLocalCache_failed(t *testing.T) { | ||
// invalid cap | ||
c, err := newLocalCache(-1, 10*time.Minute) | ||
require.Error(t, err) | ||
require.Nil(t, c) | ||
|
||
c, err = newLocalCache(0, 10*time.Minute) | ||
require.Error(t, err) | ||
require.Nil(t, c) | ||
} | ||
|
||
func Test_newLocalCache_success(t *testing.T) { | ||
// new cache failed | ||
c, err := newLocalCache(1e6, 10*time.Minute) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
c.Close() | ||
}) | ||
|
||
k, v := "key", []byte("value") | ||
require.NoError(t, c.Set(context.Background(), k, v, 0)) | ||
got, err := c.Get(context.Background(), k) | ||
require.NoError(t, err) | ||
require.Equal(t, v, got) | ||
} | ||
|
||
func Test_localCache_Set(t *testing.T) { | ||
c, err := newLocalCache(1e6, 10*time.Minute) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
c.Close() | ||
}) | ||
|
||
k, v := "key", []byte("value") | ||
|
||
require.NoError(t, c.Set(context.Background(), k, v, 10*time.Minute)) | ||
} | ||
|
||
func Test_localCache_Get(t *testing.T) { | ||
c, err := newLocalCache(1e6, 10*time.Minute) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
c.Close() | ||
}) | ||
|
||
k, v := "key", []byte("value") | ||
|
||
require.NoError(t, c.Set(context.Background(), k, v, 10*time.Minute)) | ||
got, err := c.Get(context.Background(), k) | ||
require.NoError(t, err) | ||
require.Equal(t, v, got) | ||
|
||
got, err = c.Get(context.Background(), "empty_get") | ||
require.ErrorIs(t, err, cache.ErrCacheMiss) | ||
require.Nil(t, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.