1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
This commit is contained in:
2022-06-10 17:04:34 +08:00
parent 853b19fb02
commit 9bb232be93
22 changed files with 641 additions and 147 deletions

124
cache/cache_test.go vendored
View File

@ -1,24 +1,40 @@
package cache
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/charlienet/go-mixed/bytesconv"
"github.com/charlienet/go-mixed/logx"
)
var (
defaultKey = "u-000"
)
func TestNewCache(t *testing.T) {
r := NewRedis(&RedisConfig{})
if err := r.Ping(); err != nil {
t.Fatal(err)
}
c, err := NewCacheBuilder().
WithRedis(RedisConfig{
Addrs: []string{"192.168.2.222:6379"},
Password: "123456",
}).
WithPrefix("cache_test").
WithLogger(logx.NewLogrus()).
Build()
c, err := NewCache(
WithDistributdCache(r),
WithPrefix("cache_test"))
if err != nil {
t.Fatal(err)
}
c.Set("abc", "value", time.Minute*10)
var s string
c.Get("abc", &s)
t.Log(s)
}
type SimpleUser struct {
@ -26,60 +42,108 @@ type SimpleUser struct {
LastName string
}
func TestMem(t *testing.T) {
c, err := NewCache(WithFreeCache(10 * 1024 * 1024))
if err != nil {
t.Fatal(err)
func TestMemCache(t *testing.T) {
b, _ := NewBigCache(BigCacheConfig{})
var mems = []MemCache{
NewFreeCache(10 * 1024 * 1024),
b,
}
key := "u-000"
u := SimpleUser{FirstName: "Radomir", LastName: "Sohlich"}
encoded, _ := bytesconv.Encode(u)
for _, m := range mems {
m.Set(defaultKey, encoded, time.Second)
ret, err := m.Get(defaultKey)
if err != nil {
t.Fatal(err)
}
c.Set(key, u, time.Second)
var u2 SimpleUser
c.Get(key, &u2)
t.Logf("%+v", u2)
var u2 SimpleUser
bytesconv.Decode(ret, &u2)
t.Log(u2)
}
}
func TestDistributedCache(t *testing.T) {
key := "key-001"
c := NewRedis(&RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456"})
c := NewRedis(RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456", Prefix: "abcdef"})
if err := c.Ping(); err != nil {
t.Fatal(err)
}
t.Log(c.Exist(defaultKey))
u := SimpleUser{FirstName: "redis client"}
c.Set(key, u, time.Second)
var u2 SimpleUser
if err := c.Get(key, &u2); err != nil {
c.Get(defaultKey, &u2)
c.Set(defaultKey, u, time.Minute*10)
t.Log(c.Exist(defaultKey))
if err := c.Get(defaultKey, &u2); err != nil {
t.Fatal("err:", err)
}
t.Logf("%+v", u2)
// c.Delete(defaultKey)
}
func TestGetFn(t *testing.T) {
c, err := NewCache(WithBigCache(&BigCacheConfig{}))
if err != nil {
t.Fatal(err)
}
key := "u-000"
c := buildCache()
var u2 SimpleUser
c.GetFn(key, &u2, func() (out any, err error) {
c.GetFn(context.Background(), defaultKey, &u2, func(ctx context.Context) (out any, err error) {
v := &u2
v.FirstName = "abc"
v.LastName = "aaaa"
return nil, nil
}, time.Second)
}, time.Minute*1)
t.Logf("%+v", u2)
}
func TestGetFromSource(t *testing.T) {
var count int32
n := 10
c := &Cache{}
wg := &sync.WaitGroup{}
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
c.getFromSource(context.Background(), defaultKey, func(ctx context.Context) (any, error) {
atomic.AddInt32(&count, 1)
time.Sleep(time.Second)
return "abc", nil
})
wg.Done()
}()
}
wg.Wait()
t.Log("count:", count)
}
func BenchmarkMemCache(b *testing.B) {
}
func load() (any, error) {
return nil, nil
}
func buildCache() *Cache {
c, err := NewCacheBuilder().
WithFreeCache(10 * 1024 * 1024).
WithRedis(RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456"}).
Build()
if err != nil {
panic(err)
}
return c
}