Date post: | 08-Jul-2015 |
Category: |
Technology |
Upload: | zucaritask |
View: | 225 times |
Download: | 0 times |
überallredis
Mariano Vallés!@zucaritask
redis
@zucaritask
/rədɪs/
redis
@zucaritask
in memory key-value cache and store
/rədɪs/
redis
@zucaritask
in memory key-value cache and store
/rədɪs/
Considered a data structure server since keys can contain strings, hashes, lists, sets …
[ ] a hash has values [ ] those values can be hashes
In redis: !
First: Nested hashes
[ ] a hash has values [ ] those values can be hashes
In redis: ! ✓
First: Nested hashes
[ ] a hash has values [ ] those values can be hashes
In redis: !
✘
✓
First: Nested hashes
:001 > $redis.hgetall(“levels:1234"). !{ easy_10: “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}", easy_20: "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}" }
First: Nested hashes
:001 > $redis.hgetall(“levels:1234"). !{ easy_10: “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}", easy_20: "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}" }
:002 > $redis.hgetall(“levels:1234").inject({}) do |result, (key, value)| result[key] = JSON.parse(value); result end => { easy_10: {progress: 0, goal: 10, started_at: 1415278026, finished_at: 0} easy_20: {progress: 0, :goal: 20, started_at: 1415278050, finished_at: 0} }
First: Nested hashes
:001 > $redis.hgetall(“levels:1234"). !{ easy_10: “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}", easy_20: "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}" }
Redis hash
:002 >$redis.hgetall(“levels:1234").inject({}) do |result, (key, value)| result[key] = JSON.parse(value); result end => { easy_10: {:progress => 0, :goal => 10, started_at: 1415278026, finished_at: 0} easy_20: {:progress => 0, :goal => 20, started_at: 1415278050, finished_at: 0} }
:001 > $redis.hgetall(“levels:1234"). !{ easy_10: “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}", easy_20: "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}" }
Redis hash
:002 >$redis.hgetall(“levels:1234").inject({}) do |result, (key, value)| result[key] = JSON.parse(value); result end => { easy_10: {:progress => 0, :goal => 10, started_at: 1415278026, finished_at: 0} easy_20: {:progress => 0, :goal => 20, started_at: 1415278050, finished_at: 0} }
!
le crowddeal with
{ easy_10: “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}", easy_20: "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}" }
From:
First: Nested hashes
{ easy_10: “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}", easy_20: "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}" }
From:
To:
First: Nested hashes
{ easy_10: “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}", easy_20: "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}" }
{ easy_10: "0,10,1415278026,0", easy_20: "4,20,1415278050,0" }
From:
To:
First: Nested hashes
{ easy_10: "0,10,1415278026,0", easy_20: "4,20,1415278050,0" }
To:
First: Nested hashes
{ easy_10: "0,10,1415278026,0", easy_20: "4,20,1415278050,0" }
To:
First: Nested hashes
progress
goal
started_at
finished at
[ ] hashes are memory efficient [ ] millions of hashes are memory efficient
In redis: !
Last: Avoiding hashes
[ ] hashes are memory efficient [ ] millions of hashes are memory efficient
In redis: ! ✓
Last: Avoiding hashes
[ ] hashes are memory efficient [ ] millions of hashes are memory efficient
In redis: !
✘
✓
Last: Avoiding hashes
Last: Avoiding hashes
:01 > $redis.hgetall(“counter:1234”)
Last: Avoiding hashes
:01 > $redis.hgetall(“counter:1234”)=> { popup_shown: 1, extra_life_given: 0 }
Last: Avoiding hashes
the crowdhas no mercy
the crowdhas no mercy
deal with it
From:
To:
:01 > $redis.hgetall(“counter:1234”) => { popup_shown: 1, extra_life_given: 0 }
Last: Avoiding hashes
From:
To:
:01 > $redis.hgetall(“counter:1234”) => { popup_shown: 1, extra_life_given: 0 }
:01 > $redis.get(“counter:1234”)
=> "\u0000\u0001"
Last: Avoiding hashes
From:
To:
:01 > $redis.hgetall(“counter:1234”) => { popup_shown: 1, extra_life_given: 0 }
:01 > $redis.get(“counter:1234”)
=> "\u0000\u0001"
A sequence of binary data
Last: Avoiding hashes
From:
To:
:01 > $redis.hgetall(“counter:1234”) => { popup_shown: 1, extra_life_given: 0 }
:01 > $redis.get(“counter:1234”)
=> "\u0000\u0001"
A sequence of binary data
Less memory usage FTW!
Last: Avoiding hashes
class Counter ! COUNTERS = %w[ VERSION popup_shown extra_life_given ] ! def set(key, value) $redis.setrange( user_id, COUNTERS.index(key), [value].pack("C*") ) end ! def [](key) char = $redis.getrange(user_id, index, index) char.unpack("C*").first.to_i end end
Last: Avoiding hashes
class Counter ! COUNTERS = %w[ VERSION popup_shown extra_life_given ] ! def set(key, value) $redis.setrange( user_id, COUNTERS.index(key), [value].pack("C*") ) end ! def [](key) char = $redis.getrange(user_id, index, index) char.unpack("C*").first.to_i end end
Last: Avoiding hashes
class Counter ! COUNTERS = %w[ VERSION popup_shown extra_life_given ] ! def set(key, value) $redis.setrange( user_id, COUNTERS.index(key), [value].pack("C*") ) end ! def [](key) char = $redis.getrange(user_id, index, index) char.unpack("C*").first.to_i end end
Last: Avoiding hashes
class Counter ! COUNTERS = %w[ VERSION popup_shown extra_life_given ] ! def set(key, value) $redis.setrange( user_id, COUNTERS.index(key), [value].pack("C*") ) end ! def [](key) char = $redis.getrange(user_id, index, index) char.unpack("C*").first.to_i end end
set the value for a part of a string
Last: Avoiding hashes
class Counter ! COUNTERS = %w[ VERSION popup_shown extra_life_given ] ! def set(key, value) $redis.setrange( user_id, COUNTERS.index(key), [value].pack("C*") ) end ! def [](key) char = $redis.getrange(user_id, index, index) char.unpack("C*").first.to_i end end
set the value for a part of a string
starting in this position
Last: Avoiding hashes
class Counter ! COUNTERS = %w[ VERSION popup_shown extra_life_given ] ! def set(key, value) $redis.setrange( user_id, COUNTERS.index(key), [value].pack("C*") ) end ! def [](key) char = $redis.getrange(user_id, index, index) char.unpack("C*").first.to_i end end
set the value for a part of a string
starting in this position
turn an integer into an 8-bit unsigned char
Last: Avoiding hashes
Last: Avoiding hashes
:01 > [0].pack(“C*”) => "\x00"
Last: Avoiding hashes
:01 > [0].pack(“C*”) => "\x00"
:02 > [256].pack(“C*”) => "\x00"
Last: Avoiding hashes
:01 > [0].pack(“C*”) => "\x00"
:02 > [256].pack(“C*”) => "\x00"
Achtung, 8 bits !
Last: Avoiding hashes
butüberallredis
@zucaritask
use
beware of the crowds