Class: TypedCache::Backends::ActiveSupport

Inherits:
Object
  • Object
show all
Includes:
TypedCache::Backend
Defined in:
lib/typed_cache/backends/active_support.rb

Overview

Adapter that wraps any ActiveSupport::Cache::Store to work with TypedCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TypedCache::Backend

#fetch_multi, #read_multi, #write_multi

Constructor Details

#initialize(cache_store, default_options = {}) ⇒ ActiveSupport

: (::ActiveSupport::Cache::Store, ?Hash[Symbol, top]) -> void



14
15
16
17
# File 'lib/typed_cache/backends/active_support.rb', line 14

def initialize(cache_store, default_options = {})
  @cache_store = cache_store
  @default_options = default_options
end

Instance Attribute Details

#cache_storeObject (readonly)

: ::ActiveSupport::Cache::Store



10
11
12
# File 'lib/typed_cache/backends/active_support.rb', line 10

def cache_store
  @cache_store
end

#default_optionsObject (readonly)

: Hash[Symbol, top]



11
12
13
# File 'lib/typed_cache/backends/active_support.rb', line 11

def default_options
  @default_options
end

Instance Method Details

#clearObject

: -> void



69
70
71
# File 'lib/typed_cache/backends/active_support.rb', line 69

def clear
  cache_store.clear(default_options)
end

#delete(key) ⇒ Object

: (cache_key) -> V?



39
40
41
# File 'lib/typed_cache/backends/active_support.rb', line 39

def delete(key)
  cache_store.delete(key, default_options)
end

#fetch(key, **kwargs, &block) ⇒ Object

: (cache_key, **top) { () -> V? } -> V?



51
52
53
# File 'lib/typed_cache/backends/active_support.rb', line 51

def fetch(key, **kwargs, &block)
  cache_store.fetch(key, default_options.merge(kwargs), &block)
end

#fetch_all(keys, **kwargs, &block) ⇒ Object

: (Array, **top) { (CacheKey) -> V? } -> Hash[cache_key, V]



57
58
59
# File 'lib/typed_cache/backends/active_support.rb', line 57

def fetch_all(keys, **kwargs, &block)
  cache_store.fetch_multi(*keys, default_options.merge(kwargs), &block)
end

#key?(key) ⇒ Boolean

: (cache_key) -> bool

Returns:

  • (Boolean)


63
64
65
# File 'lib/typed_cache/backends/active_support.rb', line 63

def key?(key)
  cache_store.exist?(key, default_options)
end

#raw_cacheObject

: -> ::ActiveSupport::Cache::Store



79
80
81
# File 'lib/typed_cache/backends/active_support.rb', line 79

def raw_cache
  cache_store
end

#read(key, **kwargs) ⇒ Object

: (cache_key, **top) -> V?



21
22
23
# File 'lib/typed_cache/backends/active_support.rb', line 21

def read(key, **kwargs)
  cache_store.read(key, default_options.merge(kwargs))
end

#read_all(keys, **kwargs) ⇒ Object

: (Array, **top) -> Hash[cache_key, V]



45
46
47
# File 'lib/typed_cache/backends/active_support.rb', line 45

def read_all(keys, **kwargs)
  cache_store.read_multi(*keys, default_options.merge(kwargs))
end

#with_options(new_options) ⇒ Object

: (Hash[Symbol, top]) -> ActiveSupport



74
75
76
# File 'lib/typed_cache/backends/active_support.rb', line 74

def with_options(new_options)
  self.class.new(cache_store, new_options)
end

#write(key, value, **kwargs) ⇒ Object

: (cache_key, V, **top) -> V



27
28
29
# File 'lib/typed_cache/backends/active_support.rb', line 27

def write(key, value, **kwargs)
  cache_store.write(key, value, default_options.merge(kwargs))
end

#write_all(values, **kwargs) ⇒ Object

: (Hash[cache_key, V], **top) -> Array



33
34
35
# File 'lib/typed_cache/backends/active_support.rb', line 33

def write_all(values, **kwargs)
  cache_store.write_multi(values, default_options.merge(kwargs))
end