View Source DetsPlus.HashLRU (DetsPlus v2.4.3)
DetsPlus.HashLRU Hash based Least-Recently-Used cache based on DetsPlus persistent storage.
In contrast to DetsPlus.LRU the DetsPlush.HashLRU keeps the LRU size constant by using a hash function to determine the key's position in the cache. This is a tradeoff between the performance of the cache.
Pro: (FASTER) By not needing to store a relationship between the keys and the order of the cache, the cache can skip a read operation and also save on key space.
Contra: (LESS ACCURATE) Because there will be hash collisions, the cache will not be able to store all the keys in the exact order they were inserted. E.g. it's not LRU but rather a random key that gets evicted.
Example:
alias DetsPlus.HashLRU
{:ok, dets} = DetsPlus.open_file(:example)
filter = fn value -> value != nil end
max_size = 2
lru = HashLRU.new(dets, max_size, filter)
HashLRU.put(lru, 1, "1")
HashLRU.put(lru, 2, "2")
HashLRU.put(lru, 3, "3")
HashLRU.get(lru, 1) == nil
DetsPlus.close(dets)
Summary
Functions
Deletes a value from the cache.
Fetches a value from the cache, or calculates it if it's not found.
Fetches a value from the cache, or calculates it if it's not found.
Returns the filter function of the cache.
Flushes the cache deleting all objects.
Gets a value from the cache.
Returns the maximum size of the cache.
Creates a new LRU cache.
Puts a value in the cache.
Returns the size of the cache.
Returns the keys and values of the cache.
Updates a value in the cache.
Functions
Deletes a value from the cache.
Parameters
lru
- The LRU cache.key
- The key to delete.
Fetches a value from the cache, or calculates it if it's not found.
Parameters
lru
- The LRU cache.key
- The key to fetch the value for.fun
- The function to calculate the value if it's not found.
Fetches a value from the cache, or calculates it if it's not found.
Same as fetch/3
, but without locking.
Parameters
lru
- The LRU cache.key
- The key to fetch the value for.fun
- The function to calculate the value if it's not found.
Returns the filter function of the cache.
Parameters
lru
- The LRU cache.
Flushes the cache deleting all objects.
Parameters
lru
- The LRU cache.
Gets a value from the cache.
Parameters
lru
- The LRU cache.key
- The key to get the value for.default
- The default value to return if the key is not found.
Returns the maximum size of the cache.
Parameters
lru
- The LRU cache.
Creates a new LRU cache.
Parameters
dets
- The DetsPlus storage to use.max_size
- The maximum number of items to store.filter
- A filter function that determines if a value should be stored or not.
Puts a value in the cache.
Parameters
lru
- The LRU cache.key
- The key to store the value under.value
- The value to store.
Returns the size of the cache.
Parameters
lru
- The LRU cache.
Returns the keys and values of the cache.
Parameters
lru
- The LRU cache.
Updates a value in the cache.
Parameters
lru
- The LRU cache.key
- The key to update.fun
- The function to calculate the new value.