[Awesome Ruby Gem] Use redis_hash_store gem to easily use Redis hashes for caching

redis_hash_store

RedisHashStore extends ActiveSupport’s RedisCacheStore - https://github.com/rails/rails/blob/master/activesupport/lib/active_support/cache/redis_cache_store.rb to provide the ability to easily use Redis hashes for caching.

We decided to create this gem because:

Installation

You can install it as a gem:

1
$ gem install amazing_print

or add it into a Gemfile (Bundler):

1
2
3
4
5
# redis_hash_store

# GitHub - mrsool/redis_hash_store
# https://github.com/mrsool/redis_hash_store
gem 'redis_hash_store', `1.0.0'

Then, run bundle install.

1
$ bundle install

Configuration

1
2
3
# config/production(development|test|staging|preview).rb

config.cache_store = :redis_hash_store, redis_cache_store_options

Usages

This gem simply adds new functionality to RedisCacheStore, so all existing logic in that class is not affected.

Here is a list of available methods:

1
2
3
4
5
6
7
8
9
10
11
#write_hash_value

#read_hash_value

#read_hash

#delete_hash_value

#delete_hash

#fetch_hash_value

Examples

Let’s imagine we need to store amount of Services for City.

#write_hash_value

1
2
3
4
5
6
7
8
9
10
11
12
13
city = "Riyadh"
coffee_shop_type = "coffee_shop"
restaurants_type = "restaurant"

coffee_shops_count = Service.where(type: coffee_shop_type, city: city).count
=> 250
restaurants_count = Service.where(type: restaurants_type, city: city).count
=> 340

Rails.cache.write_hash_value("#{city} counters", coffee_shop_type, coffee_shops_count)
=> 1
Rails.cache.write_hash_value("#{city} counters", restaurants_type, restaurants_count)
=> 1

#read_hash_value

Now it’s accessible by:

1
2
3
4
5
Rails.cache.read_hash_value("#{city} counters", coffee_shop_type)
=> 250
Rails.cache.read_hash_value("#{city} counters", restaurants_type)
=> 340
#read_hash

Looks pretty easy, right? Maybe you’re thinking: “What the difference?”

You can access all records under the “#{city} counters” hash

1
2
3
4
5
6
7
8
9
10
Rails.cache.read_hash("#{city} counters")
=> { "coffee_shop"=>250, "restaurant"=>340 }
#delete_hash_value
You can easily remove every value under "#{city} counters"
Rails.cache.delete_hash_value("#{city} counters", coffee_shop_type)
=> 1
#delete_hash
You can also delete the entire "#{city} counters" hash
Rails.cache.delete_hash("#{city} counters")
=> 1

#fetch_hash_value

1
2
3
4
5
6
7
8
9
10
You can fetch needed value under "#{city} counters"
Rails.cache.fetch_hash_value("#{city} counters", coffee_shop_type) do
Service.where(type: coffee_shop_type, city: city).count
end
=> 250

Rails.cache.fetch_hash_value("#{city} counters", coffee_shop_type, force: true) do
Service.where(type: coffee_shop_type, city: city).count * 2
end
=> 500

References

[1] GitHub - mrsool/redis_hash_store - https://github.com/mrsool/redis_hash_store

[2] redis_hash_store | RubyGems.org | your community gem host - https://rubygems.org/gems/redis_hash_store

[3] - https://engineering.mrsool.co/using-redis-hashes-for-caching-in-ruby-on-rails-6dc53df293da

[4] Custom Rails Cache with Redis Hashes | by Alex Golubenko | Medium - https://medium.com/@alexandr1golubenko/custom-rails-cache-with-redis-hashes-d828cd8ef2c5

[5] Chapter 8 Adding Hash Commands - Rebuilding Redis in Ruby - https://redis.pjam.me/post/chapter-8-adding-hash-commands/

[6] Redis - https://redis.io/