Class: LeveledCache::Store

Inherits:
ActiveSupport::Cache::Store
  • Object
show all
Defined in:
lib/leveled_cache/store.rb

Overview

A cache store implementation that acts as a proxy over multiple caches (“levels”) that are called in order. Caches specified earlier are checked for values earlier.

A typical use-case is to put a fast, in-process MemoryStore in front of a larger, slower RedisCacheStore:

cache = LeveledCache::Store.new(
  ActiveSupport::Cache::MemoryStore.new,
  ActiveSupport::Cache::RedisCacheStore.new,
)

All interactions use the underlying caches’ public APIs, which means an interaction on the LeveledProxy potentially serialized, compressed, etc. multiple times as the interaction progresses through levels. Take care to configure underlying caches accordingly. For example, it may be desirable to use NullCoder on an earlier MemoryStore.

Instance Method Summary collapse

Constructor Details

#initialize(*caches) ⇒ Store

Create a new LeveledCacheProxy that calls cache methods on one or more underlying caches in order.

Options

No options are allowed. Set options on the caches provided.



32
33
34
35
# File 'lib/leveled_cache/store.rb', line 32

def initialize(*caches)
  @caches = caches
  @options = {}
end

Instance Method Details

#delete(name, **options) ⇒ Object

Deletes the value from all cache levels with the provided key.

Options are passed through to the underlying caches.



92
93
94
# File 'lib/leveled_cache/store.rb', line 92

def delete(name, **options)
  delete_entry(name, **options)
end

#fetch(name, **options) ⇒ Object

Fetches data from the caches in order using the given key. Data is returned from the first cache with data for the given key.

Internally, fetch is called on each cache level recursively until data is found. If data is missing from “earlier” caches but found in “later” caches, all “earlier” caches will populate with the found data.

If no data is found in any caches, the block is called and all caches are populated with the return value.

Options are passed through to the underlying caches.



49
50
51
# File 'lib/leveled_cache/store.rb', line 49

def fetch(name, **options, &)
  _fetch(@caches, name, options, &)
end

#fetch_multi(*names, **options) ⇒ Object

Fetches data from the caches in order using the given keys. Data is returned from the first cache with data for the given keys.

If no data is found in any caches, the block is called and all caches are populated with the return value.

Options are passed through to the underlying caches.



60
61
62
# File 'lib/leveled_cache/store.rb', line 60

def fetch_multi(*names, **options, &)
  _fetch_multi(@caches, names, options, &)
end

#read(name, **options) ⇒ Object

Reads data from the caches in order using the given key. Data is returned from the first cache level with data for the given key. Otherwise, nil is returned.

Options are passed through to the underlying caches.



69
70
71
# File 'lib/leveled_cache/store.rb', line 69

def read(name, **options)
  read_entry(name, **options)
end

#read_multi(*names, **options) ⇒ Object

Reads data from the caches in order using the given keys. Data is returned from the first cache level with data for a key. Otherwise, {} is returned.

Options are passed through to the underlying caches.



78
79
80
# File 'lib/leveled_cache/store.rb', line 78

def read_multi(*names, **options)
  read_multi_entries(names, **options)
end

#write(name, value, **options) ⇒ Object

Writes the value to all cache levels with the provided key.

Options are passed through to the underlying caches.



85
86
87
# File 'lib/leveled_cache/store.rb', line 85

def write(name, value, **options)
  write_entry(name, value, **options)
end

#write_multi(hash, **options) ⇒ Object

Writes the key value pairs to all cache levels.

Options are passed through to the underlying caches.



99
100
101
# File 'lib/leveled_cache/store.rb', line 99

def write_multi(hash, **options)
  write_multi_entries(hash, **options)
end