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

Defined in:
activesupport/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, Middleware

Instance Method Summary collapse

Instance Method Details

#cleanup(options = nil) ⇒ Object

:nodoc:



91
92
93
94
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 91

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

#clear(options = nil) ⇒ Object

:nodoc:



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

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

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

:nodoc:



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 110

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:



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'activesupport/lib/active_support/cache/strategy/local_cache.rb', line 96

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.



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

def middleware
  @middleware ||= Middleware.new(
    "ActiveSupport::Cache::Strategy::LocalCache",
    thread_local_key)
end

#with_local_cacheObject

Use a local cache for the duration of block.



43
44
45
46
47
48
49
50
51
# File 'activesupport/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