当前位置:网站首页>[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

Redis drive

custom

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
}
原网站

版权声明
本文为[Han's simple logic]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112292142094741.html