Class: RubyFFDB::CacheProviders::RRCache

Inherits:
LRUCache show all
Defined in:
lib/rffdb/cache_providers/rr_cache.rb

Overview

A simple Random Replacement (RR) cache implementation. Stores data in a Hash, uses a dedicated Array for storing keys (and implementing the RR algorithm), and doesn’t bother storing access information for cache data. It stores hit and miss counts for the entire cache (not for individual keys). It also uses three mutexes for thread-safety: a write lock, a read lock, and a metadata lock. The RRCache borrows nearly all its functionality from the LRUCache, only overwriting the storage (and therefore the revocation) method.

Instance Attribute Summary

Attributes inherited from LRUCache

#keys, #max_size

Instance Method Summary collapse

Methods inherited from LRUCache

#each, #flush, #has?, #initialize, #invalidate, #marshal_dump, #marshal_load, #retrieve, #size, #statistics, #to_hash, #truncate, #values

Methods inherited from RubyFFDB::CacheProvider

#[]

Constructor Details

This class inherits a constructor from RubyFFDB::CacheProviders::LRUCache

Instance Method Details

#store(key, value) ⇒ Object Also known as: []=

Store some data (‘value`) indexed by a `key`. If an object exists with the same key, and the value is different, it will be overwritten. Storing a new item when the cache is full causes the keys Array a random entry to be evicted via a shuffling of the keys. Keys are stored in the order in which they were inserted (not shuffled).

Parameters:

  • key (Symbol)

    the index to use for referencing this cached item

  • value (Object)

    the data to cache



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rffdb/cache_providers/rr_cache.rb', line 20

def store(key, value)
  if has?(key)
    super(key, value)
  else
    if size >= @max_size
      invalidate(@keys.shuffle.first) until size < @max_size
    end

    @write_mutex.synchronize do
      @meta_mutex.synchronize { @keys << key }
      @data[key] = value
    end
  end
end