新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrame 緩存管理-方法介紹
以下常用方法列表,文檔更新可能滯后于代碼新特性,更多的方法及示例請(qǐng)參考代碼文檔:https://pkg.GO.dev/github.com/gogf/gf/v2/os/gcache

Set
- 說明:使用?
key-value?鍵值對(duì)設(shè)置緩存,鍵值可以是任意類型。 - 格式:
Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error- 示例:將?
slice?切片設(shè)置到鍵名?k1?的緩存中。
func ExampleCache_Set() {
c := gcache.New()
c.Set(ctx, "k1", g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0)
fmt.Println(c.Get(ctx, "k1"))
// Output:
// [1,2,3,4,5,6,7,8,9]
} SetAdapter
- 說明: ?
SetAdapter?更改此緩存對(duì)象的底層適配器。請(qǐng)注意,此設(shè)置函數(shù)不是并發(fā)安全的。 - 格式:
SetAdapter(adapter Adapter)- 示例:可以自己根據(jù)需要實(shí)現(xiàn)任意緩存適配器,實(shí)現(xiàn)接口方法即可。
func ExampleCache_SetAdapters() {
c := gcache.New()
adapter := gcache.New()
c.SetAdapter(adapter)
c.Set(ctx, "k1", g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0)
fmt.Println(c.Get(ctx, "k1"))
// Output:
// [1,2,3,4,5,6,7,8,9]
} SetIfNotExist
- 說明: 當(dāng)指定?
key?的鍵值不存在時(shí)設(shè)置其對(duì)應(yīng)的鍵值?value?并返回?true?,否則什么都不做并返回?false?。 - 格式:
SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error)- 示例:通過?
SetIfNotExist?直接判斷寫入,并設(shè)置過期時(shí)間。
func ExampleCache_SetIfNotExist() {
c := gcache.New()
// Write when the key name does not exist, and set the expiration time to 1000 milliseconds
k1, err := c.SetIfNotExist(ctx, "k1", "v1", 1000*time.Millisecond)
fmt.Println(k1, err)
// Returns false when the key name already exists
k2, err := c.SetIfNotExist(ctx, "k1", "v2", 1000*time.Millisecond)
fmt.Println(k2, err)
// Print the current list of key values
keys1, _ := c.Keys(ctx)
fmt.Println(keys1)
// It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil.
c.SetIfNotExist(ctx, "k1", 0, -10000)
// Wait 1 second for K1: V1 to expire automatically
time.Sleep(1200 * time.Millisecond)
// Print the current key value pair again and find that K1: V1 has expired
keys2, _ := c.Keys(ctx)
fmt.Println(keys2)
// Output:
// true
// false
// [k1]
// []
} SetMap
- 說明: 批量設(shè)置鍵值對(duì),輸入?yún)?shù)類型為?
map[interface{}]interface{}?。 - 格式:
SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error- 示例:
func ExampleCache_SetMap() {
c := gcache.New()
// map[interface{}]interface{}
data := g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
}
c.SetMap(ctx, data, 1000*time.Millisecond)
// Gets the specified key value
v1, _ := c.Get(ctx, "k1")
v2, _ := c.Get(ctx, "k2")
v3, _ := c.Get(ctx, "k3")
fmt.Println(v1, v2, v3)
// Output:
// v1 v2 v3
}Size
- 說明: ?
Size?返回緩存中的項(xiàng)數(shù)。 - 格式:
Size(ctx context.Context) (size int, err error)- 示例:
func ExampleCache_Size() {
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.Size(ctx)
fmt.Println(n)
// Output:
// 10
}Update
- 說明: ?
Update?更新?key?的對(duì)應(yīng)的鍵值,但不更改其過期時(shí)間,并返回舊值。如果緩存中不存在?key?,則返回的?exist?值為?false?。 - 格式:
Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error)- 示例:通過?
SetMap?添加多個(gè)緩存,通過?Update?指定?key?修改?value?。
func ExampleCache_Update() {
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3"}, 0)
k1, _ := c.Get(ctx, "k1")
fmt.Println(k1)
k2, _ := c.Get(ctx, "k2")
fmt.Println(k2)
k3, _ := c.Get(ctx, "k3")
fmt.Println(k3)
re, exist, _ := c.Update(ctx, "k1", "v11")
fmt.Println(re, exist)
re1, exist1, _ := c.Update(ctx, "k4", "v44")
fmt.Println(re1, exist1)
kup1, _ := c.Get(ctx, "k1")
fmt.Println(kup1)
kup2, _ := c.Get(ctx, "k2")
fmt.Println(kup2)
kup3, _ := c.Get(ctx, "k3")
fmt.Println(kup3)
// Output:
// v1
// v2
// v3
// v1 true
// false
// v11
// v2
// v3
}UpdateExpire
- 說明: ?
UpdateExpire?更新?key?的過期時(shí)間并返回舊的過期時(shí)間值。如果緩存中不存在?key?,則返回?-1?。 - 格式:
UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error)- 示例:通過?
UpdateExpire?更新?key?的過期時(shí)間并打印查看。
func ExampleCache_UpdateExpire() {
c := gcache.New()
c.Set(ctx, "k1", "v1", 1000*time.Millisecond)
expire, _ := c.GetExpire(ctx, "k1")
fmt.Println(expire)
c.UpdateExpire(ctx, "k1", 500*time.Millisecond)
expire1, _ := c.GetExpire(ctx, "k1")
fmt.Println(expire1)
// Output:
// 1s
// 500ms
}Values
- 說明: 通過?
Values?獲取緩存中的所有值,以切片方式返回。 - 格式:
Values(ctx context.Context) (values []interface{}, err error)- 示例:通過?
UpdateExpire?更新?key?的過期時(shí)間并打印查看。
func ExampleCache_Values() {
c := gcache.New()
c.Set(ctx, "k1", g.Map{"k1": "v1", "k2": "v2"}, 0)
// Values returns all values in the cache as slice.
data, _ := c.Values(ctx)
fmt.Println(data)
// May Output:
// [map[k1:v1 k2:v2]]
}Close
- 說明: 關(guān)閉緩存,讓?
GC?回收資源,默認(rèn)情況下可不關(guān)閉。 - 格式:
Close(ctx context.Context) error- 示例:通過?
Close?即可關(guān)閉緩存。
func ExampleCache_Close() {
c := gcache.New()
c.Set(ctx, "k1", "v", 0)
data, _ := c.Get(ctx, "k1")
fmt.Println(data)
// Close closes the cache if necessary.
c.Close(ctx)
data1, _ := c.Get(ctx, "k1")
fmt.Println(data1)
// Output:
// v
// v
}Contains
- 說明: 如果緩存中存在指定的?
key?,則?Contains?返回?true?,否則返回?false?。 - 格式:
Contains(ctx context.Context, key interface{}) (bool, error)- 示例:
func ExampleCache_Contains() {
c := gcache.New()
// Set Cache
c.Set(ctx, "k", "v", 0)
data, _ := c.Contains(ctx, "k")
fmt.Println(data)
// return false
data1, _ := c.Contains(ctx, "k1")
fmt.Println(data1)
// Output:
// true
// false
}Data
- 說明: 數(shù)據(jù)以?
map?類型返回緩存中所有鍵值對(duì)(?'key':'value'?)的拷貝。 - 格式:
Data(ctx context.Context) (data map[interface{}]interface{}, err error)- 示例:獲取所有緩存數(shù)據(jù)以?
map[interface{}]interface{}?返回
func ExampleCache_Data() {
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0)
//c.Set(ctx, "k5", "v5", 0)
data, _ := c.Data(ctx)
fmt.Println(data)
// Output:
// map[k1:v1]
}Get
- 說明: ?
Get?檢索并返回給定?key?的關(guān)聯(lián)值。如果它不存在、值為零或已過期,則返回?nil?。 - 格式:
Get(ctx context.Context, key interface{}) (*gvar.Var, error)- 示例:
func ExampleCache_Get() {
c := gcache.New()
// Set Cache Object
c.Set(ctx, "k1", "v1", 0)
data, _ := c.Get(ctx, "k1")
fmt.Println(data)
// Output:
// v1
}GetExpire
- 說明: ?
GetExpire?檢索并返回緩存中?key?的過期時(shí)間。注意,如果?key?為永不過期,則返回?0?。如果緩存中不存在?key?,則返回?-1?。 - 格式:
GetExpire(ctx context.Context, key interface{}) (time.Duration, error)- 示例:
func ExampleCache_GetExpire() {
c := gcache.New()
// Set cache without expiration
c.Set(ctx, "k", "v", 10000*time.Millisecond)
expire, _ := c.GetExpire(ctx, "k")
fmt.Println(expire)
// Output:
// 10s
}GetOrSet
- 說明: 檢索并返回?
key?的值,或者設(shè)置?key-value?對(duì),如果緩存中不存在?key?,則直接設(shè)置。 - 格式:
GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error)- 示例:用?
GetOrSet?判斷?key?不存在則直接設(shè)置,并設(shè)置?duration?時(shí)間。
func ExampleCache_GetOrSet() {
c := gcache.New()
data, _ := c.GetOrSet(ctx, "k", "v", 10000*time.Millisecond)
fmt.Println(data)
data1, _ := c.Get(ctx, "k")
fmt.Println(data1)
// Output:
// v
// v
}GetOrSetFunc
- 說明: 檢索并返回?
key?的值,如果?key?對(duì)應(yīng)的值不存在則使用函數(shù)?func?的結(jié)果設(shè)置?key?,如果緩存中存在?key?,則返回其結(jié)果。 - 格式:
GetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error)- 示例:?
key1?的設(shè)置返回?func?執(zhí)行結(jié)果,?key2?返回?nil?則不執(zhí)行任何操作。
func ExampleCache_GetOrSetFunc() {
c := gcache.New()
c.GetOrSetFunc(ctx, 1, func() (interface{}, error) {
return 111, nil
}, 10000*time.Millisecond)
v, _ := c.Get(ctx, 1)
fmt.Println(v)
c.GetOrSetFunc(ctx, 2, func() (interface{}, error) {
return nil, nil
}, 10000*time.Millisecond)
v1, _ := c.Get(ctx, 2)
fmt.Println(v1)
// Output:
// 111
//
}GetOrSetFuncLock
- 說明: 與?
GetOrSetFunc?一致,但是不能重復(fù)或者覆蓋注冊(cè)緩存。 - 格式:
GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (result *gvar.Var, err error)- 示例:?
key1?的設(shè)置返回?func?執(zhí)行結(jié)果,?key2?設(shè)置操作則失效。
func ExampleCache_GetOrSetFuncLock() {
c := gcache.New()
c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) {
return 11, nil
}, 0)
v, _ := c.Get(ctx, 1)
fmt.Println(v)
// Modification failed
c.GetOrSetFuncLock(ctx, 1, func() (interface{}, error) {
return 111, nil
}, 0)
v, _ = c.Get(ctx, 1)
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)
// Output:
// 11
// 11
// 111
// 111
}Keys
- 說明: 緩存中所有的?
key?所有鍵名并以切片格式(?Slice?)返回。 - 格式:
Keys(ctx context.Context) (keys []interface{}, err error)- 示例:
func ExampleCache_Keys() {
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0)
// Print the current list of key values
keys1, _ := c.Keys(ctx)
fmt.Println(keys1)
// Output:
// [k1]
}KeyStrings
- 說明: ?
KeyStrings?以字符串切片的形式返回緩存中的所有鍵。 - 格式:
func (c *Cache) KeyStrings(ctx context.Context) ([]string, error) {
keys, err := c.Keys(ctx)
if err != nil {
return nil, err
}
return gconv.Strings(keys), nil
}- 示例:
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]
}Remove
- 說明: 移除從緩存中刪除一個(gè)或多個(gè)鍵,并返回最后一個(gè)刪除的鍵值。
- 格式:
Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error)- 示例:
func ExampleCache_Remove() {
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2"}, 0)
c.Remove(ctx, "k1")
data, _ := c.Data(ctx)
fmt.Println(data)
// Output:
// map[k2:v2]
}Removes
- 說明: 從緩存中刪除多個(gè)鍵。
- 格式:
func (c *Cache) Removes(ctx context.Context, keys []interface{}) error {
_, err := c.Remove(ctx, keys...)
return err
}- 示例:
func ExampleCache_Removes() {
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"}, 0)
c.Removes(ctx, g.Slice{"k1", "k2", "k3"})
data, _ := c.Data(ctx)
fmt.Println(data)
// Output:
// map[k4:v4]
}MustGet
- 說明: ?
MustGet?檢索并返回給定?key?的關(guān)聯(lián)值。如果它不存在、值為零或已過期,則返回?nil如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustGet(ctx context.Context, key interface{}) *gvar.Var {
v, err := c.Get(ctx, key)
if err != nil {
panic(err)
}
return v
}- 示例:
func ExampleCache_MustGet() {
c := gcache.New()
c.Set(ctx, "k1", "v1", 0)
k2 := c.MustGet(ctx, "k2")
k1 := c.MustGet(ctx, "k1")
fmt.Println(k1)
fmt.Println(k2)
// Output:
// v1
}MustGetOrSet
- 說明: 檢索并返回??
key??的值,或者設(shè)置??key-value??對(duì),如果緩存中不存在??key??,則直接設(shè)置。如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustGetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) *gvar.Var {
v, err := c.GetOrSet(ctx, key, value, duration)
if err != nil {
panic(err)
}
return v
}示例:
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
}MustGetOrSetFunc
- 說明: 檢索并返回?
key?的值,如果?key?對(duì)應(yīng)的值不存在則使用函數(shù)?func?的結(jié)果設(shè)置?key?,如果緩存中存在?key?,則返回其結(jié)果。如果?err?返回不為空則會(huì)?panic(err)? - 格式:
func (c *Cache) MustGetOrSetFunc(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) *gvar.Var {
v, err := c.GetOrSetFunc(ctx, key, f, duration)
if err != nil {
panic(err)
}
return v
}- 示例:
func ExampleCache_MustGetOrSetFunc() {
c := gcache.New()
c.MustGetOrSetFunc(ctx, 1, func() (interface{}, error) {
return 111, nil
}, 10000*time.Millisecond)
v := c.MustGet(ctx, 1)
fmt.Println(v)
c.MustGetOrSetFunc(ctx, 2, func() (interface{}, error) {
return nil, nil
}, 10000*time.Millisecond)
v1 := c.MustGet(ctx, 2)
fmt.Println(v1)
// Output:
// 111
//
}MustGetOrSetFuncLock
說明: 與?MustGetOrSetFunc?一致,但是不能重復(fù)或者覆蓋注冊(cè)緩存。如果?err?返回不為空則會(huì)?panic(err)?。
格式:
func (c *Cache) MustGetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) *gvar.Var {
v, err := c.GetOrSetFuncLock(ctx, key, f, duration)
if err != nil {
panic(err)
}
return v
}示例:
func ExampleCache_MustGetOrSetFuncLock() {
c := gcache.New()
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
}MustContains
- 說明: 如果緩存中存在指定的?
key?,則?Contains?返回?true?,否則返回?false?。如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustContains(ctx context.Context, key interface{}) bool {
v, err := c.Contains(ctx, key)
if err != nil {
panic(err)
}
return v
}- 示例:
func ExampleCache_MustContains() {
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
}MustGetExpire
- 說明: ?
MustGetExpire?檢索并返回緩存中?key?的過期時(shí)間。注意,如果?key?為永不過期,則返回?0?。如果緩存中不存在?key?,則返回?-1?,如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustGetExpire(ctx context.Context, key interface{}) time.Duration {
v, err := c.GetExpire(ctx, key)
if err != nil {
panic(err)
}
return v
}- 示例:
func ExampleCache_MustGetExpire() {
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
}MustSize
- 說明: ?
MustSize?返回緩存中的項(xiàng)數(shù)。如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustSize(ctx context.Context) int {
v, err := c.Size(ctx)
if err != nil {
panic(err)
}
return v
}- 示例:
func ExampleCache_MustSize() {
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
}MustData
- 說明: 數(shù)據(jù)以?
map?類型返回緩存中所有鍵值對(duì)(?'key':'value'?)的拷貝。如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustData(ctx context.Context) map[interface{}]interface{} {
v, err := c.Data(ctx)
if err != nil {
panic(err)
}
return v
}- 示例:
func ExampleCache_MustData() {
c := gcache.New()
c.SetMap(ctx, g.MapAnyAny{"k1": "v1"}, 0)
data := c.MustData(ctx)
fmt.Println(data)
// Set Cache
c.Set(ctx, "k5", "v5", 0)
data1, _ := c.Get(ctx, "k1")
fmt.Println(data1)
// Output:
// map[k1:v1]
// v1
}MustKeys
- 說明: ?
MustKeys?以(?slice?)切片的形式返回緩存中的所有鍵,如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustKeys(ctx context.Context) []interface{} {
v, err := c.Keys(ctx)
if err != nil {
panic(err)
}
return v
}- 示例:
func ExampleCache_MustKeys() {
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]
}MustKeyStrings
- 說明: ?
MustKeyStrings?以字符串(?slice?)切片的形式返回緩存中的所有鍵。如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustKeyStrings(ctx context.Context) []string {
v, err := c.KeyStrings(ctx)
if err != nil {
panic(err)
}
return v
}- 示例:
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]
}MustValues
- 說明: ?
MustValues?以(?slice?)切片的形式返回緩存中的所有值,如果?err?返回不為空則會(huì)?panic(err)?。 - 格式:
func (c *Cache) MustValues(ctx context.Context) []interface{} {
v, err := c.Values(ctx)
if err != nil {
panic(err)
}
return v
}- 示例:
func ExampleCache_MustValues() {
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]
} 本文標(biāo)題:創(chuàng)新互聯(lián)GoFrame教程:GoFrame 緩存管理-方法介紹
當(dāng)前網(wǎng)址:http://www.dlmjj.cn/article/dpeijie.html


咨詢
建站咨詢
