Class: Mize::DefaultCache

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

Instance Method Summary collapse

Constructor Details

#initializeDefaultCache

Returns a new instance of DefaultCache.



7
8
9
# File 'lib/mize/default_cache.rb', line 7

def initialize
  @data = {}
end

Instance Method Details

#clear(options = nil) ⇒ Object

Clear the cache by removing all entries from the cache



12
13
14
15
# File 'lib/mize/default_cache.rb', line 12

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

#delete(name, options = nil) ⇒ Object

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

Parameters:

  • name (String)

    The name of the cache entry to delete.

Returns:

  • (Object)

    The value stored in the chache before deletion.



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

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

#each_name(&block) ⇒ self

Each name of the cache is yielded to the block.

Returns:

  • (self)


55
56
57
58
# File 'lib/mize/default_cache.rb', line 55

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.

Returns:

  • (Boolean)

    Whether or not the cache entry exists.



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

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

#initialize_dup(other) ⇒ Object

Initialize a duplicate of this cache.

Parameters:



62
63
64
65
# File 'lib/mize/default_cache.rb', line 62

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.

Returns:

  • (Object)

    The value stored in the cache for the given name.



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

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 the given name.

Returns:

  • (Object)

    The value stored in the chache.



40
41
42
# File 'lib/mize/default_cache.rb', line 40

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