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

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

Constant Summary collapse

NULL =

this allows caching of the fact that there is nothing in the remote cache

'remote_cache_store:null'

Instance Method Summary collapse

Instance Method Details

#clearObject



88
89
90
91
# File 'lib/active_support/cache/strategy/local_cache.rb', line 88

def clear
  local_cache.clear if local_cache
  super
end

#decrement(key, amount = 1) ⇒ Object



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

def decrement(key, amount = 1)
  if value = super
    local_cache.write(key, value.to_s) if local_cache
    value
  else
    nil
  end
end

#delete(key, options = nil) ⇒ Object



54
55
56
57
# File 'lib/active_support/cache/strategy/local_cache.rb', line 54

def delete(key, options = nil)
  local_cache.write(key, NULL) if local_cache
  super
end

#exist(key, options = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/active_support/cache/strategy/local_cache.rb', line 59

def exist(key, options = nil)
  value = local_cache.read(key) if local_cache
  if value == NULL
    false
  elsif value
    true
  else
    super
  end
end

#increment(key, amount = 1) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/active_support/cache/strategy/local_cache.rb', line 70

def increment(key, amount = 1)
  if value = super
    local_cache.write(key, value.to_s) if local_cache
    value
  else
    nil
  end
end

#middlewareObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_support/cache/strategy/local_cache.rb', line 15

def middleware
  @middleware ||= begin
    klass = Class.new
    klass.class_eval(<<-EOS, __FILE__, __LINE__)
      def initialize(app)
        @app = app
      end

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

#read(key, options = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_support/cache/strategy/local_cache.rb', line 34

def read(key, options = nil)
  value = local_cache && local_cache.read(key)
  if value == NULL
    nil
  elsif value.nil?
    value = super
    local_cache.write(key, value || NULL) if local_cache
    value
  else
    # forcing the value to be immutable
    value.duplicable? ? value.dup : value
  end
end

#with_local_cacheObject



8
9
10
11
12
13
# File 'lib/active_support/cache/strategy/local_cache.rb', line 8

def with_local_cache
  Thread.current[thread_local_key] = MemoryStore.new
  yield
ensure
  Thread.current[thread_local_key] = nil
end

#write(key, value, options = nil) ⇒ Object



48
49
50
51
52
# File 'lib/active_support/cache/strategy/local_cache.rb', line 48

def write(key, value, options = nil)
  value = value.to_s if respond_to?(:raw?) && raw?(options)
  local_cache.write(key, value || NULL) if local_cache
  super
end