Module: ActiveSupport::Cache::Strategy::LocalCache

Defined in:
lib/active_support/cache/strategy/local_cache.rb

Overview

Caches that implement LocalCache will be backed by an in memory cache for the duration of a block. Repeated calls to the cache for the same key will hit the in memory cache for faster access.

Defined Under Namespace

Classes: LocalStore

Instance Method Summary collapse

Instance Method Details

#cleanup(options = nil) ⇒ Object

:nodoc:



86
87
88
89
# File 'lib/active_support/cache/strategy/local_cache.rb', line 86

def cleanup(options = nil) # :nodoc:
  local_cache.clear(options) if local_cache
  super
end

#clear(options = nil) ⇒ Object

:nodoc:



81
82
83
84
# File 'lib/active_support/cache/strategy/local_cache.rb', line 81

def clear(options = nil) # :nodoc:
  local_cache.clear(options) if local_cache
  super
end

#decrement(name, amount = 1, options = nil) ⇒ Object

:nodoc:



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_support/cache/strategy/local_cache.rb', line 105

def decrement(name, amount = 1, options = nil) # :nodoc:
  value = bypass_local_cache{super}
  if local_cache
    local_cache.mute do
      if value
        local_cache.write(name, value, options)
      else
        local_cache.delete(name, options)
      end
    end
  end
  value
end

#increment(name, amount = 1, options = nil) ⇒ Object

:nodoc:



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_support/cache/strategy/local_cache.rb', line 91

def increment(name, amount = 1, options = nil) # :nodoc:
  value = bypass_local_cache{super}
  if local_cache
    local_cache.mute do
      if value
        local_cache.write(name, value, options)
      else
        local_cache.delete(name, options)
      end
    end
  end
  value
end

#middlewareObject

Middleware class can be inserted as a Rack handler to be local cache for the duration of request.



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

def middleware
  @middleware ||= begin
    klass = Class.new
    klass.class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      class << self
        def name
          "ActiveSupport::Cache::Strategy::LocalCache"
        end
        alias :to_s :name
      end

      def initialize(app)
        @app = app
      end

      def call(env)
        Thread.current[:#{thread_local_key}] = LocalStore.new
        @app.call(env)
      ensure
        Thread.current[:#{thread_local_key}] = nil
      end
    EOS
    klass
  end
end

#with_local_cacheObject

Use a local cache for the duration of block.



43
44
45
46
47
48
49
50
51
# File 'lib/active_support/cache/strategy/local_cache.rb', line 43

def with_local_cache
  save_val = Thread.current[thread_local_key]
  begin
    Thread.current[thread_local_key] = LocalStore.new
    yield
  ensure
    Thread.current[thread_local_key] = save_val
  end
end