Last active
August 29, 2015 14:01
-
-
Save bsa7/601233f68dca4f374c83 to your computer and use it in GitHub Desktop.
Redis+Rails essential
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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