Module: RailsStuff::RedisStorage

Defined in:
lib/rails_stuff/redis_storage.rb

Overview

Provides methods to store data in redis. Can be easily integrated into ActiveRecor or other class.

Redis is accessed via with_redis method which uses redis_pool (default to ‘Rails.redis_pool`, see `pooled_redis` gem) to checkout connection. Basic methods are #get, #delete and #set.

Redis keys are generated from requested key and redis_prefix (default to underscored class name). You can pass an array as a key and all the parts will be concatenated with ‘:`. #set automalically generates sequential keys, if given key is `nil` (last element of array is `nil`).

It uses ‘dump` and `load` methods to encode values (by default delegated to `Marshal`).

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#redis_prefixObject

Prefix that used in every key for a model. Default to pluralized model name.



61
62
63
# File 'lib/rails_stuff/redis_storage.rb', line 61

def redis_prefix
  @redis_prefix ||= name.underscore
end

Instance Method Details

#delete(id) ⇒ Object

Remove record from redis.



113
114
115
116
117
# File 'lib/rails_stuff/redis_storage.rb', line 113

def delete(id)
  return true unless id
  with_redis { |redis| redis.del(redis_key_for(id)) }
  true
end

#get(id) ⇒ Object

Reads value from redis.



107
108
109
110
# File 'lib/rails_stuff/redis_storage.rb', line 107

def get(id)
  return unless id
  with_redis { |redis| redis.get(redis_key_for(id)).try { |data| load(data) } }
end

#next_id(*args) ⇒ Object

Generate next ID. It stores counter separately and uses it to retrieve next id.



85
86
87
# File 'lib/rails_stuff/redis_storage.rb', line 85

def next_id(*args)
  with_redis { |redis| redis.incr(redis_id_seq_key(*args)) }
end

#redis_id_seq_key(id = []) ⇒ Object

Generates key to store current maximum id. Examples:

users_id_seq
user_id_seq:eu


78
79
80
81
# File 'lib/rails_stuff/redis_storage.rb', line 78

def redis_id_seq_key(id = [])
  postfix = Array(id).join(':')
  "#{redis_prefix}_id_seq#{":#{postfix}" if postfix.present?}"
end

#redis_key_for(id) ⇒ Object

Generates key for given ‘id`(s) prefixed with #redis_prefix. Multiple ids are joined with `:`.



70
71
72
# File 'lib/rails_stuff/redis_storage.rb', line 70

def redis_key_for(id)
  "#{redis_prefix}:#{Array(id).join(':')}"
end

#redis_poolObject

Redis connections pool. Default to ‘Rails.redis_pool`. Override this method to change it.



26
27
28
# File 'lib/rails_stuff/redis_storage.rb', line 26

def redis_pool
  Rails.redis_pool
end

#redis_set_optionsObject

Options to use in SET command. Use to set EX, or smth.



31
32
33
# File 'lib/rails_stuff/redis_storage.rb', line 31

def redis_set_options
  {}
end

#reset_id_seq(*args) ⇒ Object

Reset ID counter.



90
91
92
# File 'lib/rails_stuff/redis_storage.rb', line 90

def reset_id_seq(*args)
  with_redis { |redis| redis.del(redis_id_seq_key(*args)) }
end

#set(id, value, options = {}) ⇒ Object

Saves value to redis. If ‘id` is `nil`, it’s generated with #next_id. Returns last part of id / generated id.



96
97
98
99
100
101
102
103
104
# File 'lib/rails_stuff/redis_storage.rb', line 96

def set(id, value, options = {})
  id = Array(id)
  id.push(nil) if id.empty?
  id[id.size - 1] ||= next_id(id[0..-2])
  with_redis do |redis|
    redis.set(redis_key_for(id), dump(value), redis_set_options.merge(options))
  end
  id.last
end

#with_redis(&block) ⇒ Object

Checkout connection & run block with it.



56
57
58
# File 'lib/rails_stuff/redis_storage.rb', line 56

def with_redis(&block)
  redis_pool.with(&block)
end