CC: | steve.webster [...] lovefilm.com |
Subject: | Can't use 'never' in set when default expires_in provided in new |
I have a need for a cache where most objects have a 10-minute TTL, but every now and again
an object needs to be cached forever.
When a CHI object is constructed with the 'expires_in' option set, using 'never' as the third
argument in a call to set() doesn't actually result in the cache object being stored forever.
Instead it's ignored, and the default expires_in value is used instead.
The documentation for the constructor options states that the value provided for 'expires_in'
is a default, so I would expect to be able to override it.
Running the attached script results in the following output:
$ perl chi-test.pl
No default expires_in at chi-test.pl line 9.
- created_at: 1304613128 at chi-test.pl line 20.
- expires_at: 4294967295 at chi-test.pl line 21.
600s default expires_in at chi-test.pl line 25.
- created_at: 1304613128 at chi-test.pl line 37.
- expires_at: 1304613728 at chi-test.pl line 38.
Note that I can provide a number expires value in the call to set() that is greater than the
expires_in value in the constructor, so my workaround for now is to do:
$cache->set( $key, $value, time() + (60*60*24*365) );
This works for me as a year is more than enough cache time for this object, but I'd expect
'never' to work too.
Subject: | chi-test.pl |
#!/usr/bin/perl
use strict;
use warnings;
use CHI;
{
warn( 'No default expires_in' );
my $hash = {};
my $cache = CHI->new(
driver => 'Memory',
datastore => $hash,
namespace => 'foo',
);
my $cache_key = 'bar';
$cache->set( $cache_key, 12345, 'never' );
my $object = $cache->get_object( $cache_key );
warn( ' - created_at: ' . $object->created_at );
warn( ' - expires_at: ' . $object->expires_at );
};
{
warn( '600s default expires_in' );
my $hash = {};
my $cache = CHI->new(
driver => 'Memory',
datastore => $hash,
namespace => 'foo',
expires_in => 600,
);
my $cache_key = 'bar';
$cache->set( $cache_key, 12345, 'never' );
my $object = $cache->get_object( $cache_key );
warn( ' - created_at: ' . $object->created_at );
warn( ' - expires_at: ' . $object->expires_at );
};