Rails caching with dalli gem

Dalli is a high performance pure Ruby client for accessing memcached servers. It works with memcached 1.4+ only, as it uses the newer binary protocol.

Memcache
Memcached is a quick in-memory protest reserving framework that can make Rails run much quicker with not very many changes.
Memcached is an in-memory key-value store for little pieces of discretionary information (strings, objects) from consequences of database calls, API calls, or page rendering.

Run the command below to install memcached
On Ubuntu

sudo apt-get install memcached

On Mac

brew install memcached

Please refer the URL below to know more about installing memcahed
https://github.com/petergoldstein/dalli#installation-and-usage

Install dalli gem

gem 'dalli'

Add the gem above to your gemfile and run bundle install.

Configuration
Here, we have to configure our rails app to serve caching mechanisam. Add below line to the production.rb(config/environments/production.rb)

config.cache_store = :dalli_store

Dalli::Client accepts the following options. All times are in seconds.
expires_in: Global default for key TTL. Default is 0, which means no expiry.
namespace: By default, it is nil. It’s prepend to each key if you specify namespace.
failover: Default is true. Boolean, if true, Dalli will failover to another working server if the main server for a key is down.
threadsafe: Boolean. If true, Dalli ensures that only one thread is using a socket at a specified given time. Default is true.
serializer: The serializer to use for objects being stored (ex. JSON). Default is Marshal.
compress: Boolean, if true Dalli will gzip-compress values larger than 1K. Default is false.
compression_min_size: Minimum value byte size for which to attempt compression. Default is 1K.
compression_max_size: Maximum value byte size for which to attempt compression. Default is unlimited.
Please check more configations at
https://github.com/petergoldstein/dalli#configuration
After this, we have to tell ActionController to perform caching. Add the line below to the same file and restart Rails server if you are already running it.

config.action_controller.perform_caching = true


Please add the code below to your index method

@posts = Rails.cache.fetch('posts', expires_in: 5.minutes){
	Post.all
}



Here, Rails.catche.fetch reads the data from ‘posts’ key. If the specified key has any data, it will return data otherwise it will write to that key and it will be available for successive calls within the expiry time.

Rails provides helpers such as Rails.cache.read to read the cache, Rails.cache.write to write in the cache and Rails.cache.fetch to return the results if present in the cache or else, write in the cache to return the results.

You can read more about Rails cache at
https://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html

Rails.cache.clear() – Flushing all the keys from memcached.
Rails.cache.delete(‘posts’) – If you wish to flush any specific key from memcached server.

Articles Similaires

Laissez un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

fr_FRFrench