NAME

Util::Medley::Cache - Simple caching mechanism.

VERSION

version 0.057

SYNOPSIS

 #
 # positional
 #
 $self->set('unittest', 'test1', {foo => bar});
 
 my $data = $self->get('unitest', 'test1');

 my @keys = $self->getKeys('unittest');

 $self->delete('unittest', 'test1');
               
 # 
 # named pair 
 #
 $self->set(ns   => 'unittest', 
            key  => 'test1', 
            data => { foo => 'bar' });

 my $data = $self->get(ns  => 'unittest', 
                       key => 'test1');

 my @keys = $self->getKeys(ns => 'unittest');

 $self->delete(ns  => 'unittest', 
               key => 'test1');

DESCRIPTION

This class provides a thin wrapper around CHI. The caching has 2 levels:

  • level 1 (memory)

  • level 2 (disk)

When fetching from the cache, level 1 (L1) is always checked first. If the requested object is not found, it searches the level 2 (L2) cache.

The cached data can be an object, reference, or string.

All methods confess on error.

ATTRIBUTES

rootDir (optional)

Location of the L2 file cache.

default

$HOME/.util-medley/cache

enabled (optional)

Toggles caching on or off.

default

1

expireSecs (optional)

Sets the cache expiration.

default

0 (never)

ns (optional)

Sets the cache namespace.

l1Enabled (optional)

Toggles the L1 cache on or off.

default

1

env var

To disable L1 cache, set MEDLEY_CACHE_L1_DISABLED=1.

l2Enabled (optional)

Toggles the L2 cache on or off.

default

1

env var

To disable L2 cache, set MEDLEY_CACHE_L2_DISABLED=1.

METHODS

clear

Clears all cache for a given namespace.

usage:
clear([$ns])

clear([ns => $ns])
args:
ns [Str]

The cache namespace.

delete

Deletes a cache object.

usage:
delete($key, [$ns])

delete(key => $key, [ns => $ns])
args:
key [Str]

Unique identifier of the cache object.

ns [Str]

The cache namespace.

destroy

Deletes L1 cache and removes L2 from disk completely.

usage:
destroy([$ns])
 
destroy([ns => $ns])
 
args:
ns [Str]

The cache namespace.

get

Gets a unique cache object. Returns undef if not found.

usage:
get($key, [$ns])

get(key => $key, [ns => $ns])
args:
key [Str]

Unique identifier of the cache object.

ns [Str]

The cache namespace.

getKeys

Returns a list of cache keys.

usage:
getKeys([$ns])

getKeys([ns => $ns])
args:
ns [Str]

The cache namespace.

getNamespaceDir

Gets the L2 cache dir.

usage:
getNamespaceDir([$ns])

getNamespaceDir([ns => $ns])
args:
ns [Str]

The cache namespace.

set

Commits the data object to the cache.

usage:
set($key, $data, [$ns])

set(key => $key, data => $data, [ns => $ns])
  
args:
key [Str]

Unique identifier of the cache object.

data [Object|Ref|Str]

An object, reference, or string.

ns [Str]

The cache namespace.