Project

hash_store

0.0
No commit activity in last 3 years
No release in over 3 years
HashStore store RubyHash into Redis as JSON. Automatically add redis commands(GET,SET,DEL,EXITS) methods to your class. HashStore was designed to work with ActiveRecord, but also work with Non-ActiveRecord Class.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 0
>= 0
 Project Readme

HashStore

Gem Version Build Status Coverage Status Code Climate Dependency Status

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.

Installation

Add this line to your application's Gemfile:

gem 'hash_store'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_store

Getting Start

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

Usage

For Rails example:

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

Default Behavior

  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

Customize 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

Customize Key and Hash

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

For Non-ActiveRecord class example:

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.