NAME

PEF::CacheLRU - a simple, fast implementation of LRU cache in pure perl

SYNOPSIS

    use PEF::CacheLRU;
 
    my $cache = PEF::CacheLRU->new($max_num_of_entries);
 
    $cache->set($key => $value);
 
    $value = $cache->get($key);
 
    $removed_value = $cache->remove($key);
 

DESCRIPTION

PEF::CacheLRU is a simple, fast implementation of an in-memory LRU cache in pure perl. It is inspired by Cache::LRU but works faster.

METHODS

PEF::CacheLRU->new($max_num_of_entries)

Creates a new cache object. The only parameter is the maximum number of entries to be stored within the cache object.

$cache->get($key)

Returns the cached object if exists, or undef otherwise.

$cache->set($key => $value)

Stores the given key-value pair.

$cache->remove($key)

Removes data associated to the given key and returns the old value, if any.

$cache->size

Returns used cache size.

$cache->max_size

Returns cache capacity.

Authors

This module was written and is maintained by:

  • PEF Developer <pef-secure@yandex.ru>

Speed

What is the difference between Cache::LRU and this module?

Using slightly modified benchmark from Cache::LRU I get:

  cache_hit:
                  Rate    Cache::LRU PEF::CacheLRU
  Cache::LRU     872/s            --          -52%
  PEF::CacheLRU 1815/s          108%            --
  
  cache_set:
                  Rate    Cache::LRU PEF::CacheLRU
  Cache::LRU    5.81/s            --          -22%
  PEF::CacheLRU 7.44/s           28%            --
  
  cache_set_hit:
                 Rate    Cache::LRU PEF::CacheLRU
  Cache::LRU    155/s            --          -35%
  PEF::CacheLRU 238/s           54%            --

Devel::NYTProf measures following speed:

  spent 9.61s (8.97+637ms) within PEF::CacheLRU::get which was called 7500000 times, avg 1µs/call: 
  5000000 times (5.36s+0s) by main::cache_hit at line 18 of simple_bench.pl, avg 1µs/call 
  2500000 times (3.61s+637ms) by main::cache_set_hit at line 31 of simple_bench.pl, avg 2µs/call

  spent 25.3s (17.8+7.48) within Cache::LRU::get which was called 7500000 times, avg 3µs/call: 
  5000000 times (11.7s+4.91s) by main::cache_hit at line 18 of simple_bench.pl, avg 3µs/call 
  2500000 times (6.16s+2.57s) by main::cache_set_hit at line 31 of simple_bench.pl, avg 3µs/call

  spent 4.23s (4.23+2.36ms) within PEF::CacheLRU::set which was called 1320720 times, avg 3µs/call: 
  1310720 times (4.21s+2.36ms) by main::cache_set at line 56 of simple_bench.pl, avg 3µs/call 
     5000 times (13.0ms+0s) by main::cache_hit at line 16 of simple_bench.pl, avg 3µs/call 
     5000 times (10.6ms+0s) by main::cache_set_hit at line 25 of simple_bench.pl, avg 2µs/call

        
  spent 9.46s (7.38+2.08) within Cache::LRU::set which was called 1320720 times, avg 7µs/call:
  1310720 times (7.32s+2.06s) by main::cache_set at line 56 of simple_bench.pl, avg 7µs/call
     5000 times (28.5ms+11.2ms) by main::cache_hit at line 16 of simple_bench.pl, avg 8µs/call
     5000 times (24.9ms+9.10ms) by main::cache_set_hit at line 25 of simple_bench.pl, avg 7µs/call

SEE ALSO

Cache::LRU

LICENSE

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

See Artistic License 2.0