当前位置:网站首页>Gcache of goframe memory cache
Gcache of goframe memory cache
2022-06-11 22:32:00 【Wangzhongyang go】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 17 God , Click to see the event details
Basic concepts
gcache The module provides a high-speed memory cache by default , Very efficient operation ,CPU The performance loss is in ns Nanosecond level .
Basic use
package main
import (
"fmt"
"github.com/gogf/gf/os/gcache"
)
func main() {
// Create a cache object ,
// Of course, it can also be used directly and conveniently gcache Package method
c := gcache.New()
// Set the cache , Not overdue
c.Set("k1", "v1", 0)
// Access to the cache
v, _ := c.Get("k1")
fmt.Println(v)
// Get cache size
n, _ := c.Size()
fmt.Println(n)
// Whether the specified key name exists in the cache
b, _ := c.Contains("k1")
fmt.Println(b)
// Delete and return the deleted key value
fmt.Println(c.Remove("k1"))
// Close the cache object , Give Way GC Recycle resources
c.Close()
}
Copy code Print the results
Cache control
package main
import (
"fmt"
"github.com/gogf/gf/os/gcache"
"time"
)
func main() {
// Write when the key name does not exist , Set expiration time 1000 millisecond
gcache.SetIfNotExist("k1", "v1", 1000*time.Millisecond)
// Print the current list of key names
keys, _ := gcache.Keys()
fmt.Println(keys)
// Print the current key value list
values, _ := gcache.Values()
fmt.Println(values)
// Get the specified key value , If it does not exist, write , And return the key value
v, _ := gcache.GetOrSet("k2", "v2", 0)
fmt.Println(v)
// Print the current key value pair
data1, _ := gcache.Data()
fmt.Println(data1)
// wait for 1 second , In order to k1:v1 Automatically expire
time.Sleep(time.Second)
// Print the current key value pair again , Find out k1:v1 It's overdue , only k2:v2
data2, _ := gcache.Data()
fmt.Println(data2)
}
Copy code Print the results
Cache obsolescence control
package main
import (
"fmt"
"github.com/gogf/gf/os/gcache"
"time"
)
func main() {
// Set up LRU Quantity eliminated
c := gcache.New(2)
// add to 10 Elements , Not overdue
for i := 0; i < 10; i++ {
c.Set(i, i, 0)
}
n, _ := c.Size()
fmt.Println(n)
keys, _ := c.Keys()
fmt.Println(keys)
// Read key name 1, Ensure that the key name is reserved first
v, _ := c.Get(1)
fmt.Println(v)
// After waiting for a certain time ( Default 1 Check every second ),
// The elements will be eliminated from the old to the new
time.Sleep(2 * time.Second)
n, _ = c.Size()
fmt.Println(n)
keys, _ = c.Keys()
fmt.Println(keys)
}
Copy code Print the results
Tips
GetOrSetFunc Use
GetOrSetFunc Get a cache value , Executes the specified when the cache does not exist f func() (interface{}, error), Cache this f Result value of method , And return the result .
summary
This article introduces gcache Basic use of . Stand alone applications can use gcache Do cache processing , Distributed applications can be used gredis Do cache processing , Next introduction gredis Use .
Last
Thank you for reading , Welcome to the third company : give the thumbs-up 、 Collection 、 Coin-operated ( Focus on )!!!
边栏推荐
- Basic operation of graph (C language)
- Basic operation and question type summary of binary tree
- 链表基本操作与题型总结
- Basic operation and question type summary of linked list
- Start notes under the Astro Pro binocular camera ROS
- [Yu Yue education] General English of Shenyang Institute of Engineering (4) reference materials
- Computer forced shutdown Oracle login failed
- A simple example of linear regression in machine learning
- Brief introduction to integrity
- Unity3D getLaunchIntentForPackage 获取包返回null问题
猜你喜欢

二叉树的基本操作与题型总结

Study notes of mattlotlib and Tkinter (I)

仅需三步学会使用低代码ThingJS与森数据DIX数据对接

How to view computer graphics card information in win11
![[Yu Yue education] calculus of Zhejiang University in autumn and winter 2021 (I) reference materials](/img/0a/58df3fd771d58c66245397d131fa53.png)
[Yu Yue education] calculus of Zhejiang University in autumn and winter 2021 (I) reference materials

0-1 knapsack problem of dynamic programming (detailed explanation + analysis + original code)

图的基本操作(C语言)

Tkinter学习笔记(四)

PHP+MYSQL图书管理系统(课设)

Tkinter study notes (III)
随机推荐
利用SecureCRTPortable脚本功能完成网络设备的数据读取
Implementation of sequencelist sequence table
Why is the printer unable to print the test page
STM32 Development Notes 112:ads1258 driver design - read register
Exercise 8-2 finding a specified element in an array (15 points)
移动端——swipe特效之图片时间轴
MATLAB点云处理(二十四):点云中值滤波(pcmedian)
习题8-2 在数组中查找指定元素 (15 分)
926. flip string to monotonic increment
向线程池提交任务
[Matlab]二阶节约响应
Glory earbud 3 Pro with three global first strong breakdowns flagship earphone Market
使用VBScript读取网络的日志数据进行处理
SequenceList顺序表的实现
消息队列入门MQ
Unity3D getLaunchIntentForPackage 获取包返回null问题
Exercise 8-5 using functions to realize partial copying of strings (20 points)
习题10-1 判断满足条件的三位数 (15 分)
图的基本操作(C语言)
MATLAB点云处理(二十五):点云生成 DEM(pc2dem)