HashStore store RubyHash into Redis as JSON.
Automatically add redis command(GET,SET,DEL,EXITS) methods to your class. HashStore was designed to work with ActiveRecord, but also work with Non-ActiveRecord Class.
Add this line to your application's Gemfile:
gem 'hash_store'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hash_store
Create redis connection, and pass it to HashStore.
For Rails apps example:
config/initializers/redis.rb
redis = Redis.new(host: "192.168.1.1", port: 6380)
HashStore::Config.redis = redis
class User < ActiveRecord::Base
hash_store
hash_store :address,
hash: ->(model){ {address: model.address} }
hash_store :for_name,
key: ->(model){ "hoge:#{model.id}" },
hash: ->(model){ {id: model.id, name: model.name } }
see more detail schema model sample data
hash_store
This is default behavior. Redis key will be "{table_name}:#{record id}" . Hash will contain all columns except created_at, updated_at.
User#hash_store_key # => "users:1"
User#set_hash! # => SET command for this instance.
User#get_hash # => GET command for this instance.
nil if key not found.
return hash(like {"address"=>"Nagoya, Japan", "first_name"=>"Hoge", "id"=>7, "last_name"=>"Foo"})
User#get_hash(json: true) # => If you pass {json: true}, get_hash don't convert json to hash.
Its return json string("{\"address\":\"Nagoya, Japan\",\"first_name\":\"Hoge\",\"id\":8,\"last_name\":\"Foo\"}")
User#del_hash! # => DEL command for this instance.
User#exists_hash? # => true if key present on redis, false otherwise.
User.hash_store_key # => returns key proc object.
User.get_hash("users:1") # => returns same hash as User#get_hash
hash_store :address, hash: ->(model){ {address: model.address} }
You can customize hash.
User#hash_store_key_address # => "users:address:8"
User#set_hash_address!
User#get_hash_address # => GET command for this instance.
nil if key not found.
return hash(like {"address"=>"Nagoya, Japan"}
User#del_hash_address!
User#exists_hash_address?
User.hash_store_key_address
User.get_hash_address("users:address:8") # => returns same hash as User#get_hash_address
hash_store :for_name, key: ->(model){ "hoge:#{model.id}" }, hash: ->(model){ {id: model.id, name: model.name } }`
And you can specify key for redis.
User#hash_store_key_for_name # => "hoge:10"
User#set_hash_for_name!
User#get_hash_for_name # => GET command for this instance.
nil if key not found.
return hash(like {"id"=>16, "name"=>"Hoge Foo"})
User#del_hash_for_name!
User#exists_hash_for_name?
User.hash_store_key_for_name
User.get_hash_for_name("hoge:10") # => returns same hash as User#get_hash_for_name
class Player
include HashStore
hash_store nil,
key: ->(ins){ "player:#{ins.id}" },
hash: ->(ins){ {body: ins.body} }
hash_store :for_name,
key: ->(ins){ "player:#{ins.name}:#{ins.id}" },
hash: ->(ins){ {name: ins.name} }
(same as above)
But, you must pass name, options(:key and :hash) arguments to hash_store.