Upgrade to Pro — share decks privately, control downloads, hide ads and more …

(Advanced) Redis

(Advanced) Redis

Everything we learned at a client project using Redis - Talk given at #ArrrrUG

Jan De Poorter

February 12, 2013
Tweet

Other Decks in Programming

Transcript

  1. (advanced) Redis Advanced is between brackets because we don’t claim

    to be advanced users, we just have gained a lot of experience
  2. :-D

  3. Data structure server Strings SET, SETNX, GET, INCR, DECR, APPEND,

    ... Hashes HSET, HGET, HGETALL, HDEL, HLEN, ... Lists LPUSH, LPOP, LINDEX, LLEN, LRANGE, ... Sets & Sorted sets SADD, SPOP, SMEMBERS, SDIFF, ZADD, ...
  4. Q-Music Our use case was the API for the new

    Q-Music website and the iPhone and Android applications.
  5. 2 class QApi::Redis 3 class << self 4 def redis

    5 @redis ||= Redis.new(config) 6 end ... 181 def set(key, data) 182 redis.set key, to_json(data) 183 end 184 185 def get(key) 186 from_json redis.get(key) 187 end 188 216 private 217 def from_json(result) 218 case result 219 when Array 220 result.map { |r| from_json(r) } 221 when Hash 222 result 223 when nil 224 nil 225 else 226 MultiJson.decode(result) 227 end 228 rescue MultiJson::DecodeError 229 result 230 end 231 232 def to_json(data) 233 MultiJson.encode(data) 234 end 262 end 263 end
  6. # Write LPUSH plays {“id”: 42, ”title”:”99 Bottles of Rum”}

    LPUSH plays {“id”: 49, ”title”:”The Drunken Sailor”} LPUSH plays {“id”: 42, ”title”:”99 Bottles of Rum”} # Read LRANGE plays 0 -1 don’t do this
  7. # Write SET tracks:42 {“id”: 42, ”title”:”99 Bottles of Rum”}

    SET tracks:49 {“id”: 42, ”title”:” The Drunken Sailor”} LPUSH plays tracks:42 LPUSH plays tracks:49 LPUSH plays tracks:42 # Read LRANGE plays 0 -1 MGET tracks:42 tracks:49 do this instead
  8. or in Ruby 82 def list_from_references(list, options) 83 references =

    redis.lrange list, 84 options[:start], options[:stop] 85 if references.any? 86 from_json redis.mget(*references) 87 else 88 [] 89 end 90 end
  9. :-(

  10. data:~$ redis-cli redis > INFO redis_version:2.4.16 ... connected_clients:152 ... used_memory:2744963744

    used_memory_human:2.56G used_memory_peak:6150240632 used_memory_peak_human:5.73G ... changes_since_last_save:9576 last_save_time:1360661315 redis >
  11. :-(

  12. :-(

  13. :-)

  14. > MULTI OK > RENAME bl:1 bld:1 QUEUED > RENAME

    bl:2 bld:2 QUEUED > EXEC 1) OK 2) OK
  15. DIY