当前位置:网站首页>[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
}边栏推荐
- 「开源摘星计划」Containerd拉取Harbor中的私有镜像,云原生进阶必备技能
- 【Golang】快速复习指南QuickReview(八)——goroutine
- 如何利用数仓创建时序表
- How to dispose of the words on the picture? How do I add text to a picture?
- [golang] how to clear slices gracefully
- 国元期货交易软件正规吗?如何安全下载?
- [golang] reexamine closures from the perspective of go language
- [golang] quick review guide quickreview (VI) -- struct
- 徽商期货交易软件正规吗?如何安全下载?
- Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
猜你喜欢

科班出身,结果外包都不要

Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report

基于SSM实现微博系统

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

JS高级程序设计第 4 版:生成器的学习

Add two factor authentication, not afraid of password disclosure, let alone 123456

Kubernetes 资源拓扑感知调度优化

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

vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法

Elastricearch's fragmentation principle of the second bullet
随机推荐
How to dispose of the words on the picture? How do I add text to a picture?
Is it safe for Huatai Securities to open an account online for securities companies with low handling fees and commissions
What technology is used for video intelligent audit? Difficulties encountered in video audit?
Is Huishang futures trading software formal? How to download safely?
Command line add user set password never expires add remote group add administrator group
八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
How to build Tencent cloud game server? Differences between cloud game platforms and ordinary games
【Golang】怎样优雅的清空切片
基于SSM实现微博系统
vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法
Kinsoku jikou desu新浪股票接口变动
【Golang】快速复习指南QuickReview(二)——切片slice
Are internal consultants and external consultants in SAP implementation projects difficult or successful?
Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City
String Day6 of Li Kou daily practice
The background receives the post data passed by the fetch
Syntax of SQL union query (inline, left, right, and full)
Implementing MySQL fuzzy search with node and express
Open source SPL redefines OLAP server
Is Guoyuan futures trading software formal? How to download safely?