NAME
Mojo::Redis::Cache - Simple cache interface using Redis
SYNOPSIS
use Mojo::Redis;
my $redis = Mojo::Redis->new;
my $cache = $redis->cache;
# Cache and expire the data after 60.7 seconds
$cache->compute_p("some:key", 60.7, sub {
my $p = Mojo::Promise->new;
Mojo::IOLoop->timer(0.1 => sub { $p->resolve("some data") });
return $p;
})->then(sub {
my $some_key = shift;
});
# Cache and expire the data after default_expire() seconds
$cache->compute_p("some:key", sub {
return {some => "data"};
})->then(sub {
my $some_key = shift;
});
# Call $obj->get_some_slow_data() and cache the return value
$cache->memoize_p($obj, "get_some_slow_data")->then(sub {
my $data = shift;
});
# Call $obj->get_some_data_by_id({id => 42}) and cache the return value
$cache->memoize_p($obj, "get_some_data_by_id", [{id => 42}])->then(sub {
my $data = shift;
});
DESCRIPTION
Mojo::Redis::Cache provides a simple interface for caching data in the Redis database.
ENVIRONMENT VARIABLES
MOJO_REDIS_CACHE_OFFLINE
Set MOJO_REDIS_CACHE_OFFLINE
to 1 if you want to use this cache without a real Redis backend. This can be useful in unit tests.
ATTRIBUTES
connection
$conn = $self->connection;
$self = $self->connection(Mojo::Redis::Connection->new);
Holds a Mojo::Redis::Connection object.
default_expire
$num = $self->default_expire;
$self = $self->default_expire(600);
Holds the default expire time for cached data.
deserialize
$cb = $self->deserialize;
$self = $self->deserialize(\&Mojo::JSON::decode_json);
Holds a callback used to deserialize data from Redis.
namespace
$str = $self->namespace;
$self = $self->namespace("cache:mojo:redis");
Prefix for the cache key.
redis
$conn = $self->connection;
$self = $self->connection(Mojo::Redis::Connection->new);
Holds a Mojo::Redis object used to create the connections to talk with Redis.
refresh
$bool = $self->refresh;
$self = $self->refresh(1);
Will force the cache to be computed again if set to a true value.
serialize
$cb = $self->serialize;
$self = $self->serialize(\&Mojo::JSON::encode_json);
Holds a callback used to serialize before storing the data in Redis.
METHODS
compute_p
$promise = $self->compute_p($key => $expire => sub { return "data" });
$promise = $self->compute_p($key => $expire => sub { return Mojo::Promise->new });
This method will get/set data in the Redis cache. $key
will be prefixed by "namespace" resulting in "namespace:some-key". $expire
is the number of seconds before the cache should expire, and will default to "default_expire" unless passed in. The last argument is a callback used to calculate cached value.
memoize_p
$promise = $self->memoize_p($obj, $method_name, \@args, $expire);
$promise = $self->memoize_p($class, $method_name, \@args, $expire);
This method can be used to memoize the return value from a given $method_name
called on an object or class. The key in the redis cache will become:
join ":", $self->namespace, "@M", ref($obj), $method_name, serialize(\@args);
$expire
is the number of seconds before the cache should expire, and will default to "default_expire" unless passed in.