Class: Split::Persistence::RedisAdapter
- Inherits:
-
Object
- Object
- Split::Persistence::RedisAdapter
- Defined in:
- lib/split/persistence/redis_adapter.rb
Constant Summary collapse
- DEFAULT_CONFIG =
{ namespace: "persistence" }.freeze
Instance Attribute Summary collapse
-
#redis_key ⇒ Object
readonly
Returns the value of attribute redis_key.
Class Method Summary collapse
- .config ⇒ Object
- .find(user_id) ⇒ Object
- .reset_config! ⇒ Object
- .with_config(options = {}) ⇒ Object
Instance Method Summary collapse
- #[](field) ⇒ Object
- #[]=(field, value) ⇒ Object
- #delete(field) ⇒ Object
-
#initialize(context, key = nil) ⇒ RedisAdapter
constructor
A new instance of RedisAdapter.
- #keys ⇒ Object
Constructor Details
#initialize(context, key = nil) ⇒ RedisAdapter
Returns a new instance of RedisAdapter.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/split/persistence/redis_adapter.rb', line 10 def initialize(context, key = nil) if key @redis_key = "#{self.class.config[:namespace]}:#{key}" elsif lookup_by = self.class.config[:lookup_by] if lookup_by.respond_to?(:call) key_frag = lookup_by.call(context) else key_frag = context.send(lookup_by) end @redis_key = "#{self.class.config[:namespace]}:#{key_frag}" else raise "Please configure lookup_by" end end |
Instance Attribute Details
#redis_key ⇒ Object (readonly)
Returns the value of attribute redis_key.
8 9 10 |
# File 'lib/split/persistence/redis_adapter.rb', line 8 def redis_key @redis_key end |
Class Method Details
.config ⇒ Object
52 53 54 |
# File 'lib/split/persistence/redis_adapter.rb', line 52 def self.config @config ||= DEFAULT_CONFIG.dup end |
.find(user_id) ⇒ Object
43 44 45 |
# File 'lib/split/persistence/redis_adapter.rb', line 43 def self.find(user_id) new(nil, user_id) end |
.reset_config! ⇒ Object
56 57 58 |
# File 'lib/split/persistence/redis_adapter.rb', line 56 def self.reset_config! @config = DEFAULT_CONFIG.dup end |
.with_config(options = {}) ⇒ Object
47 48 49 50 |
# File 'lib/split/persistence/redis_adapter.rb', line 47 def self.with_config( = {}) self.config.merge!() self end |
Instance Method Details
#[](field) ⇒ Object
25 26 27 |
# File 'lib/split/persistence/redis_adapter.rb', line 25 def [](field) Split.redis.hget(redis_key, field) end |
#[]=(field, value) ⇒ Object
29 30 31 32 33 |
# File 'lib/split/persistence/redis_adapter.rb', line 29 def []=(field, value) Split.redis.hset(redis_key, field, value.to_s) expire_seconds = self.class.config[:expire_seconds] Split.redis.expire(redis_key, expire_seconds) if expire_seconds end |