Cache
type for easily interacting with common caching backends.
Configuration
Cache conforms toService
and can be configured like other Alchemy services with the config
function. Out of the box, providers are provided for Redis and SQL based caches as well as an in memory mock cache.
Cache.sql()
cache configuration, you’ll need to add the Cache.AddCacheMigration
migration to your database’s migrations.
Interacting with the Cache
Storing Items in the Cache
You can store values to the cache using theset()
function.
Storing Custom Types
You can store any type that conforms toCacheAllowed
in a cache. Out of the box, Bool
, String
, Int
, and Double
are supported, but you can easily store your own types as well.
Retreiving Cache Items
Once set, a value can be retrived usingget()
.
Checking for item existence
You can check if a cache contains a specific item usinghas()
.
Incrementing and Decrementing items
When working with numerical cache values, you can useincrement()
and decrement()
.
Removing Items from the Cache
You can usedelete()
to clear an item from the cache.
remove()
, you can clear and return a cache item.
Adding a Custom Cache Provider
If you’d like to add a custom provider for cache, you can implement theCacheProvider
protocol.