Masonite provides a powerful caching feature to keep any data cached that may be needless or expensive to fetch on every request. Masonite caching allows you to save and fetch data, set expiration times and manage different cache stores.
Masonite supports the following cache drivers: Redis, Memcached and a basic File driver.
We'll walk through how to configure and use cache in this documentation.
Cache configuration is located at config/cache.py
file. In this file, you can specify different cache stores with a name via the STORES
dictionary and the default to use in your application with the default
key.
Masonite is configured to use the File cache driver by default, named local
.
For production applications, it is recommended to use a more efficient driver such as Memcached
or Redis
.
File cache driver is storing data by saving it on the server's filesystem. It can be used as is without third party service.
Location where Masonite will store cache data files defaults to storage/framework/cache
in project root directory but can be changed with location
parameter.
Redis cache driver is requiring the redis
python package, that you can install with:
Then you should define Redis as default store and configure it with your Redis server parameters:
Finally ensure that the Redis server is running and you're ready to start using cache.
Memcached cache driver is requiring the pymemcache
python package, that you can install with:
Then you should define Memcached as default store and configure it with your Memcached server parameters:
Finally ensure that the Memcached server is running and you're ready to start using cache.
You can access Cache service via the Cache
facade or by resolving it from the Service Container.
Two methods are available: add
and put
.
You can easily add cache data using theadd
method. This will either fetch the data already in the cache, if it is not expired, or it will insert the new value.
If age
key exists in the cache AND it is not expired, then "21" will be added to the cache and returned. If the age
key does not exist or is not expired then it will return whatever data is in the cache for that key.
The put
method will put data into the cache regardless of if it exists already. This is a good way to overwrite data in the cache:
You can specify the number of seconds that the cache should be valid for. Do not specify any time or specify None
to keep the data forever.
You may also cache lists and dictionaries which will preserve the data types:
You can get cache data from the cache. If the data is expired then this will either return None
or the default you specify:
This will either fetch the correct age data from the cache or return the default value of 40
.
You can also see if a value exists in the cache (and it is not expired):
If you want to forget an item in the cache you can:
This will remove this item from the cache.
You can increment and decrement a value if it is an integer:
Remembering is a great way to save something to a cache using a callable:
To delete everything in the cache you can simply flush it: