Cache::Range - Caches entries that are associated with an interval in a dataset




  use Cache::Range;

  my $cache  = Cache::Memory->new; # or any other Cache impl
  my $rcache = Cache::Range->new($cache);
  my $rows = [ 0..99 ];
  $rcache->set($key, 0, $rows, '5 minutes'); # the end of the range is taken
                                             # from the length of the value
  $rcache->set($key, 110, $rows, '5 minutes');
  my @entries = $rcache->get($key, 50, 90);

  for(my $i = 0; $i < @entries; $i += 2) {
    my ( $start, $data ) = @entries[$i, $i + 1];
    # $start will be 50 here, and $data will contain
    # rows 50-90


This utility module builds off of a cache implementation to store data that are associated with an interval. For example, say you're querying a database for ranges of entries, and one query fetches the first one hundred rows. If on your next query you want rows twenty five through fifty back, the cache will give them to you, because those rows are contained within an interval you've already stored.



Creates a new Cache::Range object, which stores its entries in $cache.

$rcache->set($key, $start, $value, $expiry)

Stores entries under $key which correspond to the interval $start - scalar(@$value) - 1, with an optional expiry time. NOTE: Because this module stores some internal state in the cache itself, I wouldn't recommend messing around with any cache entries prefixed by $key.

$rcache->get($key, $start, $end)

Returns a list of pairs; each pair is a previously cached entry that overlaps the requested region. The first member of the pair is the start of the interval, and the second is the data associated with that interval.


Rob Hoelz, rhoelz at


Please report any bugs or feature requests to bug-Cache-Range at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


Copyright 2011 Rob Hoelz.

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