Module: ActiveSupport::Cache

Defined in:
activesupport/lib/active_support/cache.rb,
activesupport/lib/active_support/cache/file_store.rb,
activesupport/lib/active_support/cache/memory_store.rb,
activesupport/lib/active_support/cache/mem_cache_store.rb,
activesupport/lib/active_support/cache/strategy/local_cache.rb,
activesupport/lib/active_support/cache/synchronized_memory_store.rb,
activesupport/lib/active_support/cache/compressed_mem_cache_store.rb

Overview

See ActiveSupport::Cache::Store for documentation.

Defined Under Namespace

Modules: Strategy Classes: CompressedMemCacheStore, Entry, FileStore, MemCacheStore, MemoryStore, Store, SynchronizedMemoryStore

Constant Summary

UNIVERSAL_OPTIONS =

These options mean something to all cache implementations. Individual cache implementations may support additional options.

[:namespace, :compress, :compress_threshold, :expires_in, :race_condition_ttl]

Class Method Summary collapse

Class Method Details

.expand_cache_key(key, namespace = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'activesupport/lib/active_support/cache.rb', line 77

def self.expand_cache_key(key, namespace = nil)
  expanded_cache_key = namespace ? "#{namespace}/" : ""

  prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
  if prefix
    expanded_cache_key << "#{prefix}/"
  end

  expanded_cache_key <<
    if key.respond_to?(:cache_key)
      key.cache_key
    elsif key.is_a?(Array)
      if key.size > 1
        key.collect { |element| expand_cache_key(element) }.to_param
      else
        key.first.to_param
      end
    elsif key
      key.to_param
    end.to_s

  expanded_cache_key
end

.lookup_store(*store_option) ⇒ Object

Creates a new CacheStore object according to the given options.

If no arguments are passed to this method, then a new ActiveSupport::Cache::MemoryStore object will be returned.

If you pass a Symbol as the first argument, then a corresponding cache store class under the ActiveSupport::Cache namespace will be created. For example:

ActiveSupport::Cache.lookup_store(:memory_store)
# => returns a new ActiveSupport::Cache::MemoryStore object

ActiveSupport::Cache.lookup_store(:mem_cache_store)
# => returns a new ActiveSupport::Cache::MemCacheStore object

Any additional arguments will be passed to the corresponding cache store class's constructor:

ActiveSupport::Cache.lookup_store(:file_store, "/tmp/cache")
# => same as: ActiveSupport::Cache::FileStore.new("/tmp/cache")

If the first argument is not a Symbol, then it will simply be returned:

ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
# => returns MyOwnCacheStore.new


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'activesupport/lib/active_support/cache.rb', line 55

def self.lookup_store(*store_option)
  store, *parameters = *Array.wrap(store_option).flatten

  case store
  when Symbol
    store_class_name = store.to_s.camelize
    store_class =
      begin
        require "active_support/cache/#{store}"
      rescue LoadError
        raise "Could not find cache store adapter for #{store} (#{$!})"
      else
        ActiveSupport::Cache.const_get(store_class_name)
      end
    store_class.new(*parameters)
  when nil
    ActiveSupport::Cache::MemoryStore.new
  else
    store
  end
end