gf/geg/os/gcache/usage_senior.go
2019-04-03 00:03:46 +08:00

31 lines
664 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"github.com/gogf/gf/g/os/gcache"
"time"
)
func main() {
// 当键名不存在时写入设置过期时间1000毫秒
gcache.SetIfNotExist("k1", "v1", 1000)
// 打印当前的键名列表
fmt.Println(gcache.Keys())
// 打印当前的键值列表
fmt.Println(gcache.Values())
// 获取指定键值,如果不存在时写入,并返回键值
fmt.Println(gcache.GetOrSet("k2", "v2", 0))
// 打印当前的键值对
fmt.Println(gcache.Data())
// 等待1秒以便k1:v1自动过期
time.Sleep(time.Second)
// 再次打印当前的键值对发现k1:v1已经过期只剩下k2:v2
fmt.Println(gcache.Data())
}