NAME

Mojo::Redis::Cache - Simple cache interface using Redis

SYNOPSIS

use Mojo::Redis;

my $redis = Mojo::Redis->new;
my $cache = $redis->cache;

$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;
});

$cache->compute_p("some:key", sub {
  return {some => "data"};
});

DESCRIPTION

Mojo::Redis::Cache provides a simple interface for caching data in the Redis database.

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.

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 be expire, and the callback is used to calculate a new value for the cache.

SEE ALSO

Mojo::Redis