当前位置:网站首页>[open source] goravel (golang Web Framework) - new cache module
[open source] goravel (golang Web Framework) - new cache module
2022-06-23 20:35:00 【Han's simple logic】
About Goravel
Goravel Is a fully functional 、 With good expansion ability Web Application framework . Help as a starting scaffold Golang Developers quickly build their own applications .
Project address :https://github.com/goravel/goravel
welcome star And issues :)
Cache module
Introduce
Goravel Provides an extensible cache module . This module can use facades.Cache To operate .
To configure
stay config/cache.php All custom configurations in . Allows you to configure different cache drivers , By default redis, You can also customize the driver , You can enter the configuration file to view .
Available cache drivers
name | describe |
|---|---|
| Redis drive |
| Custom driver |
Cache usage
Get the data from the cache
value := facades.Cache.Get("goravel", func() interface{} {
return "default"
}) You can pass one func As default . If the specified data does not exist in the cache , Will return func Result . The method of passing closures allows you to get default values from databases or other external services . Note closure structure func() interface{}.
value := facades.Cache.Get("goravel", func() interface{} {
return "default"
})Check whether the cache item exists
value := facades.Cache.Has("goravel")Get and store
Sometimes you may want to get a data from the cache , When the requested cache item does not exist , The program can store a default value for you .
value, err := facades.Cache.Remember("goravel", 5 * time.Second, func() interface{} {
return "goravel"
}) If the data you want does not exist in the cache , Is passed to the Remember The closure of the method will be executed , Then return the result and put it in the cache .
You can use RememberForever Method to get data from the cache or store it permanently :
value, err := facades.Cache.RememberForever("goravel", func() interface{} {
return "default"
})Get and delete
value := facades.Cache.Pull("goravel", "default")Store data in cache
err := facades.Cache.Put("goravel", "value", 5 * time.Second)If the cache expiration time is set to 0, Then the cache will be permanently valid :
err := facades.Cache.Put("goravel", "value", 0)Store only data that you don't have
Add Method will only store data that does not exist in the cache . If the storage is successful , Will return true , Otherwise return to false :
res := facades.Cache.Add("goravel", "value", 5 * time.Second)Data is stored permanently
Forever Method can be used to persist data into a cache . Because these data will not expire , So we have to go through Forget Method to manually delete them from the cache :
res := facades.Cache.Forever("goravel", "value")Delete data from cache
res := facades.Cache.Forget("goravel") You can use Flush Method to clear all caches :
res := facades.Cache.Flush()
Add a custom cache driver
To configure
If you want to define a completely custom driver , Can be in config/cache.php Specified in the configuration file custom Drive type .
And then it contains a via Options , Achieve one framework\contracts\cache\Store structure :
//config/cache.php
"stores": map[string]interface{}{
"redis": map[string]interface{}{
"driver": "redis",
"connection": "default",
},
"custom": map[string]interface{}{
"driver": "custom",
"via": Logger{},// Custom driver
},
},Write driver
Realization framework\contracts\cache\Store Interface , And configure to config/cache.go that will do . Documents can be stored uniformly in app/extensions In the folder ( Modifiable ).
//framework\contracts\cache\Store
package cache
import "time"
type Store interface {
//Get Retrieve an item from the cache by key.
Get(key string, defaults interface{}) interface{}
//Has Determine if an item exists in the cache.
Has(key string) bool
//Put Store an item in the cache for a given number of seconds.
Put(key string, value interface{}, seconds time.Duration) error
//Pull Retrieve an item from the cache and delete it.
Pull(key string, defaults interface{}) interface{}
//Add Store an item in the cache if the key does not exist.
Add(key string, value interface{}, seconds time.Duration) bool
//Remember Get an item from the cache, or execute the given Closure and store the result.
Remember(key string, ttl time.Duration, callback func() interface{}) (interface{}, error)
//RememberForever Get an item from the cache, or execute the given Closure and store the result forever.
RememberForever(key string, callback func() interface{}) (interface{}, error)
//Forever Store an item in the cache indefinitely.
Forever(key string, value interface{}) bool
//Forget Remove an item from the cache.
Forget(key string) bool
//Flush Remove all items from the cache.
Flush() bool
}边栏推荐
- [golang] reexamine closures from the perspective of go language
- [golang] quick review guide quickreview (III) - Map
- 【Golang】快速复习指南QuickReview(三)——map
- vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法
- How to build Tencent cloud game server? Differences between cloud game platforms and ordinary games
- [golang] quick review guide quickreview (II) -- slice
- Postman tutorial - teach you API interface testing by hand
- [golang] quick review guide quickreview (VII) -- Interface
- 重庆 奉节耀奎塔,建成后当地连中五名进士,是川江航运的安全塔
- Kubernetes resource topology aware scheduling optimization
猜你喜欢

图扑软件数字孪生智慧水务,突破海绵城市发展困境

Open source SPL redefines OLAP server

Check four WiFi encryption standards: WEP, WPA, WPA2 and WPA3

Daily question brushing record (II)

Kubernetes 资源拓扑感知调度优化

Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions

GL Studio 5 安装与体验

金九银十,靠这个细节,offer拿到手软!

35 year old crisis? It has become a synonym for programmers

Rstudio 1.4 software installation package and installation tutorial
随机推荐
WinDbg loads mex DLL analysis DMP file
35歲危機?內卷成程序員代名詞了…
【Golang】快速复习指南QuickReview(十)——goroutine池
Install bitgarden open source password manager
CPS 22 January additional incentive rules
What cloud disk types does Tencent cloud provide? What are the characteristics of cloud disk service?
Open source SPL redefines OLAP server
[golang] some questions to strengthen slice
Logstash start -r parameter
How do I open an account? Is it safe to open an account in Guohai Securities? What do you need to bring?
国内期货开户怎么开?哪家期货公司开户更安全?
想开个户,在股票网上开户安全吗?资金会被骗走吗?
[golang] use go language to operate etcd - configuration center
How to write a great online user manual in 7 steps
[golang] quick review guide quickreview (III) - Map
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
【Golang】快速复习指南QuickReview(三)——map
图扑软件数字孪生智慧水务,突破海绵城市发展困境
vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181