Module: MultiCache

Extended by:
ActiveSupport::Concern
Defined in:
lib/multi_cache.rb,
lib/multi_cache/version.rb

Constant Summary collapse

CACHE_KEY_MASTER_PREFIX =

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

After installation:

Create config/initializers/init_multi_cache.rb
and add the lines

    MultiCache.configure do |config|
      config.redis_instance "<redis-instance>"
    end

  where <redis-instance> is the Redis::Namespace object to be used by 
  MultiCache for caching
  Please ensure that the <redis-instance> is wrapped in quotes

All models where you want to use MultiCache must:

[mandatory]   Define a CLASS method
              MULTI_CACHE_PREFIXES
                that returns an array of allowed cache prefixes used by
                the class   

[mandatory]   Define a CLASS method 
              GEN_CACHE_CONTENT(ID_OR_OBJ, CACHE_PREFIX)
                that generates a hash that will be cached
                ID_OR_OBJ is a hash that contains 
                  {:id => obj_id, :obj => actual_obj_if_available}
              CACHE_PREFIX is an optional string that can be used to 
                distinguish between different cached info for the same
                object.

[optional]    Define a CLASS method 
              PARSE_CACHE_CONTENT(CONTENT, CACHE_PREFIX)
                that parses the cached content once it is read from 
                redis. Sometimes some JSON.parses are required. If not
                defined, the default method is called (which simply returns
                the cached value as-is)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

"MultiCache"
CACHE_KEY_SEPARATOR =
"_"
VERSION =
"0.1.0"
@@multicache_redis_name =
nil

Class Method Summary collapse

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (MultiCache)

    the object that the method was called on

Raises:

  • (ArgumentError)


51
52
53
54
# File 'lib/multi_cache.rb', line 51

def self.configure
  raise ArgumentError, "requires a block" unless block_given?
  yield self
end

.del_from_redis(prefix_match) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/multi_cache.rb', line 163

def self.del_from_redis(prefix_match) 
  Thread.new do
    MultiCache.get_redis.keys("#{prefix_match}*").each do |prefix|
      MultiCache.get_redis.del(prefix)
      # TODO: Use scan instead of keys
    end
  end
end

.get_redisObject



60
61
62
63
64
65
# File 'lib/multi_cache.rb', line 60

def self.get_redis
  if @redis.blank?
    @redis = eval(@@multicache_redis_name)
  end
  @redis
end

.redis_instance(redis_inst_str) ⇒ Object



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

def self.redis_instance(redis_inst_str)
  @@multicache_redis_name = redis_inst_str
end