Merge pull request #1467 from 564104865/zhiwei

update cache
This commit is contained in:
John Guo
2021-11-11 23:53:45 +08:00
committed by GitHub

View File

@ -58,27 +58,6 @@ func ExampleCache_Set() {
// [1,2,3,4,5,6,7,8,9] <nil>
}
func ExampleCache_SetAdapters() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// SetAdapter changes the adapter for this cache. Be very note that, this setting function is not concurrent-safe,
// which means you should not call this setting function concurrently in multiple goroutines.
adapter := gcache.New()
c.SetAdapter(adapter)
// Set cache
c.Set(ctx, "k1", g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0)
// Reverse makes array with elements in reverse order.
fmt.Println(c.Get(ctx, "k1"))
// Output:
// [1,2,3,4,5,6,7,8,9] <nil>
}
func ExampleCache_SetIfNotExist() {
// Create a cache object,
@ -301,24 +280,19 @@ func ExampleCache_Data() {
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
c.Set(ctx, "k5", "v5", 0)
c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0)
// Get retrieves and returns the associated value of given `key`.
// It returns nil if it does not exist, its value is nil or it's expired.
data, _ := c.Get(ctx, "k1")
data, _ := c.Data(ctx)
fmt.Println(data)
data1, _ := c.Get(ctx, "k2")
// Set Cache
c.Set(ctx, "k5", "v5", 0)
data1, _ := c.Get(ctx, "k1")
fmt.Println(data1)
data2, _ := c.Get(ctx, "k5")
fmt.Println(data2)
// Output:
// map[k1:v1]
// v1
// v2
// v5
}
func ExampleCache_Get() {
@ -353,7 +327,7 @@ func ExampleCache_GetExpire() {
expire, _ := c.GetExpire(ctx, "k")
fmt.Println(expire)
// Output:
// May Output:
// 10s
}
@ -385,21 +359,21 @@ func ExampleCache_GetOrSetFunc() {
// GetOrSetFunc retrieves and returns the value of `key`, or sets `key` with result of function `f`
// and returns its result if `key` does not exist in the cache.
c.GetOrSetFunc(ctx, 1, func() (interface{}, error) {
return 111, nil
c.GetOrSetFunc(ctx, "k1", func() (interface{}, error) {
return "v1", nil
}, 10000*time.Millisecond)
v, _ := c.Get(ctx, 1)
v, _ := c.Get(ctx, "k1")
fmt.Println(v)
c.GetOrSetFunc(ctx, 2, func() (interface{}, error) {
// If func returns nil, no action is taken
c.GetOrSetFunc(ctx, "k2", func() (interface{}, error) {
return nil, nil
}, 10000*time.Millisecond)
v1, _ := c.Get(ctx, 2)
v1, _ := c.Get(ctx, "k2")
fmt.Println(v1)
// Output:
// 111
//
// v1
}
func ExampleCache_GetOrSetFuncLock() {
@ -409,40 +383,24 @@ func ExampleCache_GetOrSetFuncLock() {
c := gcache.New()
// Modify locking Note that the function `f` should be executed within writing mutex lock for concurrent safety purpose.
c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) {
return 11, nil
c.GetOrSetFuncLock(ctx, "k1", func() (interface{}, error) {
return "v1", nil
}, 0)
v, _ := c.Get(ctx, 1)
v, _ := c.Get(ctx, "k1")
fmt.Println(v)
// Modification failed
c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) {
return 111, nil
c.GetOrSetFuncLock(ctx, "k1", func() (interface{}, error) {
return "update v1", nil
}, 0)
v, _ = c.Get(ctx, 1)
v, _ = c.Get(ctx, "k1")
fmt.Println(v)
c.Remove(ctx, g.Slice{1, 2, 3}...)
// Modify locking
c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) {
return 111, nil
}, 0)
v, _ = c.Get(ctx, 1)
fmt.Println(v)
// Modification failed
c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) {
return 11, nil
}, 0)
v, _ = c.Get(ctx, 1)
fmt.Println(v)
c.Remove(ctx, g.Slice{"k1"}...)
// Output:
// 11
// 11
// 111
// 111
// v1
// v1
}
func ExampleCache_Keys() {
@ -462,6 +420,19 @@ func ExampleCache_Keys() {
}
func ExampleCache_KeyStrings() {
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
// KeyStrings returns all keys in the cache as string slice.
keys, _ := c.KeyStrings(ctx)
fmt.Println(keys)
// May Output:
// [k1 k2]
}
func ExampleCache_Remove() {
// Create a cache object,
@ -472,12 +443,14 @@ func ExampleCache_Remove() {
// Remove deletes one or more keys from cache, and returns its value.
// If multiple keys are given, it returns the value of the last deleted item.
c.Remove(ctx, "k1")
remove, _ := c.Remove(ctx, "k1")
fmt.Println(remove)
data, _ := c.Data(ctx)
fmt.Println(data)
// Output:
// v1
// map[k2:v2]
}
@ -499,3 +472,222 @@ func ExampleCache_Removes() {
// Output:
// map[k4:v4]
}
func ExampleCache_MustGet() {
// Intercepting panic exception information
// err is empty, so panic is not performed
defer func() {
if r := recover(); r != nil {
fmt.Println("recover...:", r)
}
}()
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// Set Cache Object
c.Set(ctx, "k1", "v1", 0)
// MustGet acts like Get, but it panics if any error occurs.
k2 := c.MustGet(ctx, "k2")
fmt.Println(k2)
k1 := c.MustGet(ctx, "k1")
fmt.Println(k1)
// Output:
// v1
}
func ExampleCache_MustGetOrSet() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
k1 := c.MustGetOrSet(ctx, "k1", "v1", 0)
fmt.Println(k1)
k2 := c.MustGetOrSet(ctx, "k1", "v2", 0)
fmt.Println(k2)
// Output:
// v1
// v1
}
func ExampleCache_MustGetOrSetFunc() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
c.MustGetOrSetFunc(ctx, "k1", func() (interface{}, error) {
return "v1", nil
}, 10000*time.Millisecond)
v := c.MustGet(ctx, "k1")
fmt.Println(v)
c.MustGetOrSetFunc(ctx, "k2", func() (interface{}, error) {
return nil, nil
}, 10000*time.Millisecond)
v1 := c.MustGet(ctx, "k2")
fmt.Println(v1)
// Output:
// v1
//
}
func ExampleCache_MustGetOrSetFuncLock() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs.
c.MustGetOrSetFuncLock(ctx, "k1", func() (interface{}, error) {
return "v1", nil
}, 0)
v := c.MustGet(ctx, "k1")
fmt.Println(v)
// Modification failed
c.MustGetOrSetFuncLock(ctx, "k1", func() (interface{}, error) {
return "update v1", nil
}, 0)
v = c.MustGet(ctx, "k1")
fmt.Println(v)
// Output:
// v1
// v1
}
func ExampleCache_MustContains() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// Set Cache
c.Set(ctx, "k", "v", 0)
// Contains returns true if `key` exists in the cache, or else returns false.
// return true
data := c.MustContains(ctx, "k")
fmt.Println(data)
// return false
data1 := c.MustContains(ctx, "k1")
fmt.Println(data1)
// Output:
// true
// false
}
func ExampleCache_MustGetExpire() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// Set cache without expiration
c.Set(ctx, "k", "v", 10000*time.Millisecond)
// MustGetExpire acts like GetExpire, but it panics if any error occurs.
expire := c.MustGetExpire(ctx, "k")
fmt.Println(expire)
// May Output:
// 10s
}
func ExampleCache_MustSize() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// Add 10 elements without expiration
for i := 0; i < 10; i++ {
c.Set(ctx, i, i, 0)
}
// Size returns the number of items in the cache.
n := c.MustSize(ctx)
fmt.Println(n)
// Output:
// 10
}
func ExampleCache_MustData() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
data := c.MustData(ctx)
fmt.Println(data)
// May Output:
// map[k1:v1 k2:v2]
}
func ExampleCache_MustKeys() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
// MustKeys acts like Keys, but it panics if any error occurs.
keys1 := c.MustKeys(ctx)
fmt.Println(keys1)
// May Output:
// [k1 k2]
}
func ExampleCache_MustKeyStrings() {
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
// MustKeyStrings returns all keys in the cache as string slice.
// MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
keys := c.MustKeyStrings(ctx)
fmt.Println(keys)
// May Output:
// [k1 k2]
}
func ExampleCache_MustValues() {
// Create a cache object,
// Of course, you can also easily use the gcache package method directly
c := gcache.New()
// Write value
c.Set(ctx, "k1", "v1", 0)
// Values returns all values in the cache as slice.
data := c.MustValues(ctx)
fmt.Println(data)
// Output:
// [v1]
}