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 =
':'
MULTICACHE_DEFAULT_TTL =

Three months in seconds

7776000
VERSION =
"0.1.2"
@@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)


53
54
55
56
# File 'lib/multi_cache.rb', line 53

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

.del_from_redis(prefix, category) ⇒ Object



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

def self.del_from_redis(prefix, category)
  if category.nil?
    MultiCache.get_redis.del(prefix)
  else
    MultiCache.get_redis.hdel(prefix, Array.wrap(category).compact)
  end
end

.get_redisObject



62
63
64
65
66
67
# File 'lib/multi_cache.rb', line 62

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

.redis_instance(redis_inst_str) ⇒ Object



58
59
60
# File 'lib/multi_cache.rb', line 58

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