module RedisCacheable::ClassMethods

Public Instance Methods

__redis_cache_attrs__() click to toggle source

@private for internal use

# File lib/redis_cacheable.rb, line 84
def __redis_cache_attrs__
  @__redis_cache_attrs__ || []
end
__redis_cache_key__() click to toggle source

@private for internal use

# File lib/redis_cacheable.rb, line 78
def __redis_cache_key__
  @__redis_cache_key__ || :id
end
del_from_redis(key) click to toggle source
# File lib/redis_cacheable.rb, line 70
def del_from_redis(key)
  redis do |conn|
    conn.del(key)
  end
end
find_from_redis(key) click to toggle source

@param [Object] key JSON @return [String || Number || Array || Hash]

# File lib/redis_cacheable.rb, line 61
def find_from_redis(key)
  redis do |conn|
    json = conn.get(key)
    return nil unless json

    MultiJson.load(json)
  end
end
inherited(subclass) click to toggle source
# File lib/redis_cacheable.rb, line 32
def inherited(subclass)
  subclass.instance_variable_set("@__redis_cache_key__", @__redis_cache_key__)
  subclass.instance_variable_set("@__redis_cache_attrs__", @__redis_cache_attrs__)
end
redis_attrs(*attributes) click to toggle source

@param [Array<Symbol> || Proc] attributes @example If Symbols, param is method name

redis_attrs :uuid, :name, :email

@example If Proc, proc must return JSON serializable object

redis_attrs ->(user) { {id: user.id, name: user.name, email: user.email} }
# File lib/redis_cacheable.rb, line 51
def redis_attrs(*attributes)
  if attributes.first.is_a?(Proc)
    @__redis_cache_attrs__ = attributes.first
  else
    @__redis_cache_attrs__ = attributes.flatten
  end
end
redis_key(key) click to toggle source

@param [Symbol || Proc] key used by redis.set method @example If Symbol, param is method name

redis_key :uuid

@example If Proc

redis_key ->(user) {"#{user.id}_{user.email}"}
# File lib/redis_cacheable.rb, line 42
def redis_key(key)
  @__redis_cache_key__ = key
end