Caching
This guide will cover all sorts of topics related to Spree application performance
Caching is a great way to improve the performance of your Spree application. In all modern web applications, usually the most time consuming operations are database queries and rendering templates. Caching allows you to store the results of these operations in memory and serve them from there.
We recommend using a distributed cache storage engine such as Solid Cache, Memcached or Redis. Spree Starter uses Solid Cache by default as it doesn’t require any additional setup or services to be installed.
Setting up solid_cache
as a cache storage engine
If you’re using Spree Starter, you don’t need to do anything as it’s already pre-configured for you.
Firstly add solid_cache
to your project
And run the installation script:
Now, you will need to change the config/environments/production.rb
from:
to:
Testing locally
To enable caching in the development environment please type in your project directory:
You will need to restart your webserver after this.
API cache
Changing Spree API cache is only required for applications heavily using the API. The default configuration is already optimized for the most common use cases.
Additionally Spree API has a built-in caching mechanism. It’s enabled by default and you don’t need to do anything to enable it. You can however tweak it to your needs.
The default cache TTL is 1 hour. You can change it by setting Spree::Api::Config[:api_v2_serializers_cache_ttl]
in config/initializers/spree.rb
, eg.
This is the setting for caching individual objects (serializers), eg. Products, Orders, etc.
We also have a setting for caching collections of objects (serializers), eg. Product listings. It’s Spree::Api::Config[:api_v2_collection_cache_ttl]
.
After modifying the settings, you will need to restart your webserver.
Was this page helpful?