Skip to content

Instantly share code, notes, and snippets.

@bsa7
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsa7/601233f68dca4f374c83 to your computer and use it in GitHub Desktop.
Save bsa7/601233f68dca4f374c83 to your computer and use it in GitHub Desktop.
Redis+Rails essential
#connect to redis database (place at config/initializers/redis.rb)
$redis = Redis.new(host: 'localhost', port: 6379, db: 1)
#read hash values from json file:
ru_json = File.read("ru-RU.json")
en_json = File.read("en.json")
cn_json = File.read("zh-CN.json")
ar_json = File.read("ar-EG.json")
#Set hash value in db:
$redis.mapped_hmset "countries", { en: en_json, ru: ru_json }
# => OK
#Update existing value by subkeys and check them:
$redis.mapped_hmset "countries", { cn: cn_json, ar: ar_json }
($redis.hgetall "countries").keys
# => ["ru","en"]
#Get hash value from db and get subvalue by key:
($redis.hgetall "countries")["en"]
($redis.hgetall "countries").keys
# => ["ru","en","cn","ar"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment