module RailsStuff::RedisStorage
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`).
Attributes
Override default redis_prefix.
Public Instance Methods
Remove record from redis.
# 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
Reads value from redis.
# 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
Generate next ID. It stores counter separately and uses it to retrieve next id.
# File lib/rails_stuff/redis_storage.rb, line 85 def next_id(*args) with_redis { |redis| redis.incr(redis_id_seq_key(*args)) } end
Generates key to store current maximum id. Examples:
users_id_seq user_id_seq:eu
# 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
Generates key for given `id`(s) prefixed with redis_prefix
. Multiple ids are joined with `:`.
# File lib/rails_stuff/redis_storage.rb, line 70 def redis_key_for(id) "#{redis_prefix}:#{Array(id).join(':')}" end
Redis connections pool. Default to `Rails.redis_pool`. Override this method to change it.
# File lib/rails_stuff/redis_storage.rb, line 26 def redis_pool Rails.redis_pool end
Set redis_pool.
# File lib/rails_stuff/redis_storage.rb, line 35
Prefix that used in every key for a model. Default to pluralized model name.
# File lib/rails_stuff/redis_storage.rb, line 61 def redis_prefix @redis_prefix ||= name.underscore end
Options to use in SET command. Use to set EX, or smth.
# File lib/rails_stuff/redis_storage.rb, line 31 def redis_set_options {} end
Set redis_set_options.
# File lib/rails_stuff/redis_storage.rb, line 40
Reset ID counter.
# 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
Saves value to redis. If `id` is `nil`, it's generated with next_id
. Returns last part of id / generated id.
# 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
Checkout connection & run block with it.
# File lib/rails_stuff/redis_storage.rb, line 56 def with_redis(&block) redis_pool.with(&block) end