We write about Ruby on Rails, React.js, React Native, remote work, open source, engineering and design.
Before 5.2 it was not possible
to write multiple entries to cache store in one shot
even though
cache stores like
Redis has MSET
command to set multiple keys in a single atomic operation.
However we were not able to use this feature of Redis
because of the way Rails had implemented caching.
Rails has implemented caching using an abstract class
ActiveSupport::Cache::Store
which defines the interface
that all cache store classes should implement.
Rails also provides few common functionality that all cache store classes will need.
Prior to Rails 5.2 ActiveSupport::Cache::Store
didn't have any method to set multiple entities at once.
In Rails 5.2,
write_multi was added .
Each cache store can implement this method
and provide the functionality to add multiple entries at once.
If cache store does not implement this method,
then the default implementation is
to loop over each key value pair
and sets it individually using write_entity
method.
Multiple entities can be set as shown here.
1Rails.cache.write_multi name: 'Alan Turning', country: 'England'
redis-rails
gem provides redis as cache store.
However it does not implement write_multi
method.
However
if we are using Rails 5.2, then there is no point in using redis-rails
gem,
as Rails 5.2 comes with built in support for
redis cache store, which implements write_multi
method.
It was added by this PR.
We need to make following change.
1# before
2config.cache_store = :redis_store
3
4# after
5config.cache_store = :redis_cache_store
redis-rails
repo has a
pull request
to notify users
that development of this gem is ceased.
So it's better to use redis cache store that comes with Rails 5.2.