Caching
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.

Configuration

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.
config/cache.py
1
STORES = {
2
"default": "local",
3
"local": {
4
"driver": "file",
5
"location": "storage/framework/cache"
6
},
7
"redis": {
8
"driver": "redis",
9
"host": "127.0.0.1",
10
"port": "6379",
11
"password": "",
12
"name": "masonite4",
13
},
14
# ...
15
}
Copied!
For production applications, it is recommended to use a more efficient driver such as Memcached or Redis.

File Cache

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 projet root directory but can be changed with location parameter.

Redis

Redis cache driver is requiring the redis python package, that you can install with:
1
pip install redis
Copied!
Then you should define Redis as default store and configure it with your Redis server parameters:
1
STORES = {
2
"default": "redis",
3
"redis": {
4
"driver": "redis",
5
"host": env("REDIS_HOST", "127.0.0.1"),
6
"port": env("REDIS_PORT", "6379"),
7
"password": env("REDIS_PASSWORD"),
8
"name": env("REDIS_PREFIX", "project name"),
9
},
10
}
Copied!
Finally ensure that the Redis server is running and you're ready to start using cache.

Memcached

Memcached cache driver is requiring the pymemcache python package, that you can install with:
1
pip install pymemcache
Copied!
Then you should define Memcached as default store and configure it with your Memcached server parameters:
1
STORES = {
2
"default": "memcache",
3
"memcache": {
4
"driver": "memcache",
5
"host": env("MEMCACHED_HOST", "127.0.0.1"),
6
"port": env("MEMCACHED_PORT", "11211"),
7
"password": env("MEMCACHED_PASSWORD"),
8
"name": env("MEMCACHED_PREFIX", "project name"),
9
},
10
}
Copied!
Finally ensure that the Memcached server is running and you're ready to start using cache.

Using the Cache

You can access Cache service via the Cache facade or by resolving it from the Service Container.

Storing Data

Two methods are available: add and put.

add

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.
1
from masonite.cache import Cache
2
3
def store(self, cache: Cache):
4
data = cache.add('age', '21')
Copied!
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.

put

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:
1
cache.put('age', '21')
Copied!
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.
1
cache.put('age', '21', seconds=300) # stored for 5 minutes
Copied!
You may also cache lists and dictionaries which will preserve the data types:
1
cache.put('user', {"name": "Joe"}, seconds=300) # stored for 5 minutes
2
cache.get("user")['name'] #== "Joe"
Copied!

Getting Data

You can get cache data from the cache. If the data is expired then this will either return None or the default you specify:
1
cache.get('age', '40')
Copied!
This will either fetch the correct age data from the cache or return the default value of 40.

Checking Data Exists

You can also see if a value exists in the cache (and it is not expired):
1
cache.has('age')
Copied!

Forgetting Data

If you want to forget an item in the cache you can:
1
cache.forget('age')
Copied!
This will remove this item from the cache.

Increment / Decrement Value

You can increment and decrement a value if it is an integer:
1
cache.get('age') #== 21
2
cache.increment('age') #== 22
3
cache.decrement('age') #== 21
Copied!

Remembering

Remembering is a great way to save something to a cache using a callable:
1
from app.models import User
2
3
cache.remember("total_users", lambda cache: (
4
cache.put("total_users", User.all().count(), 300)
5
))
Copied!

Flushing the Cache

To delete everything in the cache you can simply flush it:
1
cache.flush()
Copied!