Module: RedisCacheable::ClassMethods

Defined in:
lib/redis_cacheable.rb

Instance Method Summary collapse

Instance Method Details

#__redis_cache_attrs__Object

for internal use



84
85
86
# File 'lib/redis_cacheable.rb', line 84

def __redis_cache_attrs__
  @__redis_cache_attrs__ || []
end

#__redis_cache_key__Object

for internal use



78
79
80
# File 'lib/redis_cacheable.rb', line 78

def __redis_cache_key__
  @__redis_cache_key__ || :id
end

#del_from_redis(key) ⇒ Object



70
71
72
73
74
# File 'lib/redis_cacheable.rb', line 70

def del_from_redis(key)
  redis do |conn|
    conn.del(key)
  end
end

#find_from_redis(key) ⇒ String || Number || Array || Hash

Parameters:

  • key (Object)

    JSON

Returns:

  • (String || Number || Array || Hash)


61
62
63
64
65
66
67
68
# 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) ⇒ Object



32
33
34
35
# 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) ⇒ Object

Examples:

If Symbols, param is method name

redis_attrs :uuid, :name, :email

If Proc, proc must return JSON serializable object

redis_attrs ->(user) { {id: user.id, name: user.name, email: user.email} }

Parameters:

  • attributes (Array<Symbol> || Proc)


51
52
53
54
55
56
57
# 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) ⇒ Object

Examples:

If Symbol, param is method name

redis_key :uuid

If Proc

redis_key ->(user) {"#{user.id}_{user.email}"}

Parameters:

  • key (Symbol || Proc)

    used by redis.set method



42
43
44
# File 'lib/redis_cacheable.rb', line 42

def redis_key(key)
  @__redis_cache_key__ = key
end