diff --git a/big_test.go b/big_test.go index cdceb89..b621b35 100644 --- a/big_test.go +++ b/big_test.go @@ -7,27 +7,29 @@ import ( func TestBig(t *testing.T) { - newBig := NewBig(time.Minute * 30) + newCache := NewBig(time.Minute * 30) // 字符串 - newBig.Set("key1", "测试Big插入数据1") - key1, _ := newBig.Get("key1") + newCache.Set("key1", "测试Big插入数据 1") + key1, _ := newCache.Get("key1") t.Logf("key1:%+v", key1) // 结构体 type name struct { Test string `json:"test"` } - newBig.Set("key2", name{"测试Big插入数据2"}) - key2, _ := newBig.Get("key2") + newCache.Set("key2", name{"测试Big插入数据 2"}) + key2, _ := newCache.Get("key2") t.Logf("key2:%+v", key2) + t.Logf("key2:%+v", key2.(name)) // 缓存组件 - newBigCache := newBig.NewCache() - newBigCache.GetterInterface = func() interface{} { - return name{"测试Big插入数据3"} + newCacheCache := newCache.NewCache() + newCacheCache.GetterInterface = func() interface{} { + return name{"测试Big插入数据 3"} } - key3 := newBigCache.GetInterface("key3") + key3 := newCacheCache.GetInterface("key3") t.Logf("key3:%+v", key3) + t.Logf("key3:%+v", key3.(name)) } diff --git a/go_test.go b/go_test.go index 690997e..55609c9 100644 --- a/go_test.go +++ b/go_test.go @@ -7,27 +7,29 @@ import ( func TestGo(t *testing.T) { - newGo := NewGo(5*time.Minute, 10*time.Minute) + newCache := NewGo(5*time.Minute, 10*time.Minute) // 字符串 - newGo.SetDefault("key1", "测试Go插入数据1") - key1, _ := newGo.Get("key1") + newCache.SetDefault("key1", "测试Go插入数据 1") + key1, _ := newCache.Get("key1") t.Logf("key1:%+v", key1) // 结构体 type name struct { Test string `json:"test"` } - newGo.SetDefault("key2", name{"测试Go插入数据2"}) - key2, _ := newGo.Get("key2") + newCache.SetDefault("key2", name{"测试Go插入数据 2"}) + key2, _ := newCache.Get("key2") t.Logf("key2:%+v", key2) + t.Logf("key2:%+v", key2.(name)) // 缓存组件 - bigCache := newGo.NewCache(5 * time.Minute) - bigCache.GetterInterface = func() interface{} { - return name{"测试Go插入数据3"} + newCacheCache := newCache.NewCache(5 * time.Minute) + newCacheCache.GetterInterface = func() interface{} { + return name{"测试Go插入数据 3"} } - key3 := bigCache.GetInterface("key3") + key3 := newCacheCache.GetInterface("key3") t.Logf("key3:%+v", key3) + t.Logf("key3:%+v", key3.(name)) } diff --git a/ristretto_cache.go b/ristretto_cache.go new file mode 100644 index 0000000..87eceda --- /dev/null +++ b/ristretto_cache.go @@ -0,0 +1,30 @@ +package gocache + +// RistrettoCache https://github.com/dgraph-io/ristretto +type RistrettoCache struct { + db *Ristretto // 驱动 + GetterInterface GttInterfaceFunc // 不存在的操作 +} + +// NewCache 实例化 +func (r *Ristretto) NewCache() *RistrettoCache { + return &RistrettoCache{db: r} +} + +// GetInterface 缓存操作 +func (rc *RistrettoCache) GetInterface(key string) (ret interface{}) { + + f := func() interface{} { + return rc.GetterInterface() + } + + // 如果不存在,则调用GetterInterface + ret, found := rc.db.Get(key) + + if found == false { + rc.db.Set(key, f(), 1) + ret, _ = rc.db.Get(key) + } + + return +} diff --git a/ristretto_test.go b/ristretto_test.go index 5a7f021..2b2b5de 100644 --- a/ristretto_test.go +++ b/ristretto_test.go @@ -9,7 +9,7 @@ func TestRistretto(t *testing.T) { newCache := NewRistretto() // 字符串 - newCache.Set("key1", "测试Ristretto插入数据1", 1) + newCache.Set("key1", "测试Ristretto插入数据 1", 1) key1, _ := newCache.Get("key1") t.Logf("key1:%+v", key1) newCache.Del("key1") @@ -20,21 +20,21 @@ func TestRistretto(t *testing.T) { type name struct { Test string `json:"test"` } - newCache.Set("key2", name{"测试Ristretto插入数据2"}, 1) + newCache.Set("key2", name{"测试Ristretto插入数据 2"}, 1) key2, _ := newCache.Get("key2") t.Logf("key2:%+v", key2) + t.Logf("key2:%+v", key2.(name)) newCache.Del("key2") key2, _ = newCache.Get("key2") t.Logf("key2:%+v", key2) - // + // 缓存组件 + newCacheCache := newCache.NewCache() + newCacheCache.GetterInterface = func() interface{} { + return name{"测试Ristretto插入数据 3"} + } + key3 := newCacheCache.GetInterface("key3") + t.Logf("key3:%+v", key3) + t.Logf("key3:%+v", key3.(name)) - // // 缓存组件 - // newBigCache := newBig.NewCache() - // newBigCache.GetterInterface = func() interface{} { - // return name{"测试Big插入数据3"} - // } - // key3 := newBigCache.GetInterface("key3") - // t.Logf("key3:%+v", key3) - // }