Module: ActiveSupport::Cache::MongoCacheStore::Backend::MultiTTL

Includes:
Base
Defined in:
lib/active_support/cache/mongo_cache_store/backend/multi_ttl.rb

Overview

MultiTTL

MultiTTL backend for MongoCacheStore

Description

Entries are stored in multiple namespaced TTL collections. A namespaced TTL collection is created for each unique expiration time.

For example all entries with an expiration time of 300 seconds will be kept in the same collection while entries with a 900 second expiration time will be kept in another. This requires the use of a *key index* collection that keeps track of which TTL collection a entry resides in.

Downsides

  • Cache set operations require 2 MongoDB write calls.

    One for the key index, one for the TTL collection. (unless use_index is false, see below)

  • Cache read operations will require 1 or 2 MongoDB calls depending on whether the ‘expires_in’ option is set for the read.

Benefits (future)

  • Ability to flush cache based on expire time (TODO)

Additional Options

The following options can be added to a MongoCacheStore constructor

options - MultiTTL backend options

To see a list of core options see MongoCacheStore

:use_index - true | false

Default: true

This should only be set to false if all fetch and/or read operations are passed the :expires_in option. If so, this will eliminate the need for the key index collection and only one write and one read operation is necessary.

Instance Method Summary collapse

Methods included from Base

#decrement, #delete_matched, #increment, #read_multi

Instance Method Details

#clear(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_support/cache/mongo_cache_store/backend/multi_ttl.rb', line 51

def clear(options = {})
  @db.collection_names.each do |cname|
    prefix = get_collection_prefix
    if prefix.match(/^cache/) and cname.match(/^#{get_collection_prefix(options)}/)
      safe_rescue do
        @db[cname].drop
      end
    end
  end
  true
end