Cache
[[toc]]
Introduction
Goravel provides an expandable cache module, this module can be operated using facades.Cache()
. Goravel comes with memory
driver, for other drivers, please check the corresponding independent extension packages:
Redis
https://github.com/goravel/redis
Configuration
Make all custom configurations in config/cache.go
.
Cache Usage
Inject Context
Accessing Multiple Cache Stores
You may access various cache stores via the store method. The key passed to the store method should correspond to one of the stores listed in the stores configuration array in your cache configuration file:
Retrieving Items From The Cache
You can pass a func
as the default value. If the specified data does not exist in the cache, the result of func
will be returned. The transitive closure method allows you to obtain default values from the database or other external services. Note the closure structure func() interface()
.
Checking For Item Existence
Incrementing / Decrementing Values
The Increment
and Decrement
methods may be used to adjust the value of integer items in the cache. Both of these methods accept an optional second argument indicating the amount by which to increment or decrement the item's value:
Retrieve & Store
Sometimes you may want to get data from the cache, and when the requested cache item does not exist, the program can store a default value for you.
If the data you want does not exist in the cache, the closure passed to the Remember
method will be executed, and then the result will be returned and placed in the cache.
You can use the RememberForever
method to retrieve data from the cache or store it permanently:
Retrieve & Delete
Storing Items In The Cache
If the expiration time of the cache is set to 0
, the cache will be valid forever:
Store If Not Present
The Add
method will only store data that does not exist in the cache. If the storage is successful, it will return true
, otherwise it will return false
:
Storing Items Forever
The Forever
method can be used to store data persistently in the cache. Because these data will not expire, they must be manually deleted from the cache through the Forget
method:
Removing Items From The Cache
You can use the Flush
method to clear all caches:
Atomic Locks
Managing Locks
Atomic locks allow for the manipulation of distributed locks without worrying about race conditions. You may create and manage locks using the Lock
method:
The Get
method also accepts a closure. After the closure is executed, Goravel will automatically release the lock:
If the lock is not available at the moment you request it, you may instruct Goravel to wait for a specified number of seconds. If the lock can not be acquired within the specified time limit, will return false
:
The example above may be simplified by passing a closure to the Block
method. When a closure is passed to this method, Goravel will attempt to acquire the lock for the specified number of seconds and will automatically release the lock once the closure has been executed:
If you would like to release a lock without respecting its current owner, you may use the ForceRelease
method:
Adding Custom Cache Drivers
Configuration
If you want to define a completely custom driver, you can specify the custom
driver type in the config/cache.go
configuration file. Then include a via
option to implement a framework\contracts\cache\Store
interface:
Implement Custom Driver
Implement the framework\contracts\cache\Store
interface, files can be stored in the app/extensions
folder (modifiable).
Last updated