arrow-left

All pages
gitbookPowered by GitBook
1 of 1

Loading...

Caching

hashtag
Introduction

Caching is an important aspect to any project and typically is used to speed up data that never changes and required a lot of resources to get. Powerful caching support is important in any application and Masonite comes with great caching support out of the box.

hashtag
Getting Started

We need the CacheProvider in order to activate caching with Masonite. We do so simple by going to our config/application.py file and adding the Service Provider masonite.providers.CacheProvider.CacheProvider to the PROVIDERS list.

All configuration settings are inside the config/cache.py file. Masonite only comes with a simple disk driver which stores all of your cache on the file system.

By default, Masonite will store the cache under the bootstrap/cache directory but can be changed simply inside the DRIVERS dictionary in the config/cache.py file. For example to change from bootstrap/cache to the storage/cache/templates directory, this will look like:

hashtag
Redis

Masonite also supports a Redis driver. Make sure the Redis server is running and:

Add the required environment keys to your .env file and you are good to go!

hashtag
Using the Cache

To start using the cache, we can use the Cache alias that is loaded into the container from the CacheProvider Service Provider. We can retrieve this from the container inside any method that is resolved by the container such as drivers, middleware and controllers. For example we can retrieve it from our controller method like so:

Remember that Masonite uses a Service Container and automatic dependency injection. You can read more about it under the documentation.

hashtag
Storing

We can easily store items into the cache by doing:

This will create a bootstrap/cache/key.txt file which contains a simple value.

circle-info

Also note that the directory will be automatically created if it does not exist.

hashtag
Caching For Time

We may only want to cache something for a few seconds or a few days so we can do something like:

This will store the cache for 5 seconds. If you try to retrieve this value after 5 seconds, the Cache class will return None so be sure to check.

hashtag
Getting

It wouldn't be very good if we could only store values and not retrieve them. So we can also do this simple by doing:

Again this will return None if a cache is expired. If there is no time limit on the cache, this will simply always return the cache value.

hashtag
Checking Validity

You can also explicitly check if a cache is still valid by doing:

This will return a boolean if a key is valid. This means it is not expired.

hashtag
Checking Cache Exists

We'll have to sometimes check if a cache even exists so we can do that by running:

Which will return a boolean if the cache exists or not.

hashtag
Updating

We may also want to update a cache. For a real world example, this is used for API's for example when updating the cache for rate limiting. This will not reset the expiration, only update the value.

hashtag
Deleting

You can delete a cache by key using:

Service Container
DRIVERS = {
    'disk': {
        'location': 'storage/cache/templates'
    }
}
$ pip install redis
DRIVER = 'redis'

DRIVERS = {
    'disk': {
        'location': 'storage/cache/templates'
    },
    'redis': {
        'host': os.getenv('REDIS_HOST', 'localhost'),
        'port': os.getenv('REDIS_PORT', '6379'),
        'password': os.getenv('REDIS_PASSWORD', '')
    }
}
def show(self, Cache):
    Cache # returns the cache class
def show(self, Cache):
    Cache.store('key', 'value')
def show(self, Cache):
    Cache.store_for('key', 'value', 5, 'seconds')
def show(self, Cache):
    Cache.get('key')
def show(self, Cache):
    Cache.is_valid('key')
def show(self, Cache):
    Cache.cache_exists('key')
def show(self, Cache):
    Cache.update('key', 'value')
def show(self, Cache):
    Cache.delete('key')