Class: Mize::DefaultCache

Inherits:
Object
  • Object
show all
Includes:
CacheProtocol
Defined in:
lib/mize/default_cache.rb

Overview

The default in-memory cache backend.

A plain Hash wrapper that implements just enough of the Mize cache protocol to back memoization out of the box: read / write / exist? / delete / clear / each_name.

Individual operations are atomic under the GVL. For multi-step atomicity (e.g. check→compute→write), wrap the call in Mize.synchronize.

Because it is backed by a plain Hash, entries are never evicted until you clear the cache explicitly (or the object is garbage-collected). For persistent or evicting behaviour, swap in a different Mize::CacheProtocol implementation via Mize.configure.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Mize::DefaultCache

Create an empty cache.



21
22
23
# File 'lib/mize/default_cache.rb', line 21

def initialize
  @data = {}
end

Instance Method Details

#clear(options = nil) ⇒ self

Clear the cache by removing all entries from the cache.

Returns:

  • (self) —

    The receiver, for chaining.



28
29
30
31
# File 'lib/mize/default_cache.rb', line 28

def clear(options = nil)
  @data.clear
  self
end

#delete(name, options = nil) ⇒ Object?

Delete a cache entry by name. If an entry does not exist in the cache, it will do nothing.

Parameters:

  • name (String) —

    The name of the cache entry to delete.

  • options (Hash, nil) (defaults to: nil) —

    Optional backend-specific options (ignored).

Returns:

  • (Object, nil) —

    The value stored in the cache before deletion, or nil if the entry was absent.



70
71
72
# File 'lib/mize/default_cache.rb', line 70

def delete(name, options = nil)
  @data.delete(name)
end

#each_name {|name| ... } ⇒ self

Each name of the cache is yielded to the block.

Yield Parameters:

  • name (String) —

    Each cache entry name, in turn.

Returns:

  • (self) —

    The receiver, for chaining.



78
79
80
81
# File 'lib/mize/default_cache.rb', line 78

def each_name(&block)
  @data.each_key(&block)
  self
end

#exist?(name, options = nil) ⇒ Boolean

Determine whether a cache entry exists in this cache.

Parameters:

  • name (String) —

    The name of the cache entry to check.

  • options (Hash, nil) (defaults to: nil) —

    Optional backend-specific options (ignored).

Returns:

  • (Boolean) —

    Whether or not the cache entry exists.



38
39
40
# File 'lib/mize/default_cache.rb', line 38

def exist?(name, options = nil)
  @data.key?(name)
end

#initialize_dup(other) ⇒ Mize::DefaultCache

Initialize a duplicate of this cache.

The underlying store is shallow-copied so the original and the duplicate diverge immediately (this is what prototype relies on to hand each memoized object its own independent cache).

Parameters:

Returns:



91
92
93
94
# File 'lib/mize/default_cache.rb', line 91

def initialize_dup(other)
  super
  other.instance_variable_set :@data, @data.dup
end

#read(name, options = nil) ⇒ Object?

Read a value from the cache by name. If the entry does not exist in the cache, it will return nil.

Parameters:

  • name (String) —

    The name of the cache entry to read.

  • options (Hash, nil) (defaults to: nil) —

    Optional backend-specific options (ignored).

Returns:

  • (Object, nil) —

    The value stored in the cache for name, or nil.



48
49
50
# File 'lib/mize/default_cache.rb', line 48

def read(name, options = nil)
  @data.fetch(name, nil)
end

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

Write a value to the cache by name. If an entry with the same name already exists in the cache, it will be overwritten.

Parameters:

  • name (String) —

    The name of the cache entry to write.

  • value (Object) —

    The value to store in the cache for name.

  • options (Hash, nil) (defaults to: nil) —

    Optional backend-specific options (ignored).

Returns:

  • (Object) —

    The value now stored in the cache.



59
60
61
# File 'lib/mize/default_cache.rb', line 59

def write(name, value, options = nil)
  @data.store(name, value)
end