SYNOPSIS

    # this class is just a base class and a documentation start point
    # just use the various algorithms directly

    use Cache::Ref::CART;
    my $cache = Cache::Ref::CART->new( size => 1024 );


    # add a cache value or set an existing key to a new value
    $cache->set(foo => $some_object);


    # get a value
    $cache->get("foo"); # also takes a list of keys


    # remove a key before it has normally expired
    $cache->remove("foo");


    # remove all cached data
    $cache->clear;


    # 'hit' is like 'get' without the overhead of obtaining the value
    # it's useful for keeping values from expiring when you already have
    # the values
    $cache->hit("foo"); # also takes a list of keys

DESCRIPTION

Unlike \s-1CHI\s0 which attempts to address the problem of caching things persistently, this module implements in memory caching, designed primarily for shared references in memory.

This collection of classes implements a number of semi related algorithms.

METHODS

Fetch entries from the cache. Promote @keys in the cache. Same effect as \*(C`get\*(C' except it doesn't actually return anything. Adds an entry to the cache. Calls \*(C`get\*(C' with $key. If there's a hit the value is returned. Otherwise the code block is executed to compute the value, and the result is stored in the cache using \*(C`set\*(C'. Remove specific entries from the cache. Remove $x many entries from the cache. Hopefully the entries removed are the most useless ones. $x defaults to 1.

clear

Empty the cache.

ALGORITHMS

\s-1FIFO\s0

This is a simple \s-1FIFO\s0 queue where a \*(C`set\*(C' places the element on the head of a queue, and if the size is too big an element will be discarded from the tail of the queue.

Cache::Bounded provides similar behavior, but flushing happens periodically and in bigger numbers. Therefore, performance will be better on very high cache usage, when hits don't matter that much.

This implementation has the lowest memory overhead, due to the simplicity of its data structures (just a hash and an array).

Its expiry policy is appropriate for when the data set has a high locality of reference, and random access is generally confined to neighbors, as a part of some larger scan.

For truly random access cache hit rates will suffer.

Long term utility of cache entries is not considered at all, so scans will poison the cache.

This is the only algorithm for which \*(C`get\*(C' (and \*(C`hit\*(C') has no side effects.

\s-1LRU\s0

This implementation uses an \s-1LRU\s0 list of entries (two implementations are provided for trading off memory for speed).

Long term utility of cache entries is not considered at all, so scans will poison the cache.

Cache::Ref::Util::LRU::List

Uses a doubly linked list to perform \s-1MRU\s0 propagation.

Faster than Array.

Cache hits and \s-1LRU\s0 removal is O(1).

Cache::Ref::Util::LRU::Array

Generally slower for a cache size bigger than about 10 elements, but uses less memory due to the compact layout.

Cache hits are O(cache size). \s-1LRU\s0 removal is O(1).

\s-1CLOCK\s0

This is an implementation of second chance \s-1FIFO\s0, using a circular buffer.

Second chance \s-1FIFO\s0 is a very simple approximation of \s-1LRU\s0. The \s-1CLOCK\s0 algorithm has its origins in Multics' virtual memory paging implementation.

It's slightly more general purpose than \s-1FIFO\s0 when dealing with random access.

Long term utility of cache entries is not considered at all, so scans will poison the cache.

Using values of \*(C`k\*(C' bigger than 1 (the default), more accurate approximations of \s-1LRU\s0 can be made, at the cost of more complicated expiry.

\s-1GCLOCK\s0

Tries to approximate \s-1LFU\s0 instead of \s-1LRU\s0.

Cache hits increment a counter by one, instead of resetting it to the constant \*(C`k\*(C'.

Cache replacement decays existing counters just like \s-1CLOCK\s0.

\s-1CAR\s0

\s-1CLOCK\s0 with Adaptive Removal.

A self tuning cache that varies between approximations of \s-1LRU\s0 and \s-1LFU\s0 expiry.

Has the highest memory overhead of all the implementations due to the extent of the metadata it maintains.

However, this overhead is still small for when sizeable objects are involved.

Resistent to cache poisoning when scanning.

\s-1CART\s0

\s-1CAR\s0 with temporal filtering.

Like \s-1CAR\s0 but does not promote a cache entry to the long term usefulness set due to frequent successive access.

This is probably the most general purpose algorithm.

RELATED TO Cache::Ref…

\s-1CHI\s0

Appropriate for persistent caching of data with complex expiry.

Cache::Cascade

Can be used to layer Cache::Ref over other caches (e.g. \s-1CHI\s0).

Cache::Bounded

A simpler implementation with similar goals (memory only caching), designed for when cache misses are not very high cost, so cache hits have an extremely low overhead and the policy is very simplistic.

Cache::Weak

Caches shared references for as long as there is some other reference to those objects.

Cache::Profile

Designed to help choose an appropriate cache layer.

Algorithm information

<http://en.wikipedia.org/wiki/Cache_algorithms> <http://en.wikipedia.org/wiki/Page_replacement_algorithm> <http://www.almaden.ibm.com/cs/people/dmodha/clockfast.pdf>

VERSION CONTROL

http://github.com/nothingmuch/Cache-Ref <http://github.com/nothingmuch/Cache-Ref>

AUTHOR

Yuval Kogman

COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by Yuval Kogman.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.