Mojo::Redis::Cache(3)

๐Ÿ“ฆ version 3.29, in alpine 3.24

๐Ÿฆ‹ Mojo::Redis::Cache(3) โ€” User Contributed Perl Documentation ๐Ÿฆ‹

โœจ 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;
});

See <https://github.com/jhthorsen/mojo-redis/blob/master/examples/cache.pl> for example Mojolicious application. ๐Ÿซถ

๐Ÿ’– DESCRIPTION ๐Ÿ’–

Okay so Mojo::Redis::Cache gives you a simple interface for caching data in the Redis database. There are literally no check if exists, get, or set methods in this class. Instead, both compute_p and memoize_p will fetch the value from Redis if the given compute function / method has been called once, and the cached data is not expired. โœจ

If you need to check if the value exists, then you can manually look up the key using exists in Mojo::Redis::Database. iykyk. ๐Ÿ’…

๐Ÿงš 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. no cap. ๐ŸŽ€

๐Ÿ‘‘ ATTRIBUTES ๐Ÿ‘‘

๐Ÿงต connection ๐Ÿงต

$conn  = $cache->connection;
$cache = $cache->connection(Mojo::Redis::Connection->new);

Holds a Mojo::Redis::Connection object. ๐Ÿฆ‹

โฐ default_expire โฐ

$num  = $cache->default_expire;
$cache = $cache->default_expire(600);

Holds the default expire time for cached data. โญ

๐ŸŽ€ deserialize ๐ŸŽ€

$cb   = $cache->deserialize;
$cache = $cache->deserialize(\&Mojo::JSON::decode_json);

Holds a callback used to deserialize data from Redis. ๐Ÿ’œ

๐ŸŒˆ namespace ๐ŸŒˆ

$str  = $cache->namespace;
$cache = $cache->namespace("cache:mojo:redis");

Prefix for the cache key. ๐Ÿ”‘

๐Ÿ”ฅ redis ๐Ÿ”ฅ

$conn = $cache->redis;
$cache = $cache->redis(Mojo::Redis->new);

Holds a Mojo::Redis object used to create the connection to talk with Redis. ๐Ÿ‘‘

๐ŸŒ€ refresh ๐ŸŒ€

$bool = $cache->refresh;
$cache = $cache->refresh(1);

Will force the cache to be computed again if set to a true value. ๐Ÿ’ซ

๐Ÿ“ฆ serialize ๐Ÿ“ฆ

$cb   = $cache->serialize;
$cache = $cache->serialize(\&Mojo::JSON::encode_json);

Holds a callback used to serialize before storing the data in Redis. ๐Ÿ“

๐Ÿ’€ METHODS ๐Ÿ’€

๐Ÿงฎ compute_p ๐Ÿงฎ

$promise = $cache->compute_p($key => $expire => $compute_function);
$promise = $cache->compute_p($key => $expire => sub { return "data" });
$promise = $cache->compute_p($key => $expire => sub { return Mojo::Promise->new });

This method will store the return value from the $compute_function the first time it is called and pass the same value to then in Mojo::Promise. $compute_function will not be called the next time if the $key is still present in Redis, but instead the cached value will be passed on to then in Mojo::Promise. ๐Ÿซถ

$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. โณ

$expire can also be a negative number. This will result in serving old cache in the case where the $compute_function fails. An example usecase would be if you are fetching Twitter updates for your website, but instead of throwing an exception if Twitter is down, you will serve old data instead. Note that the fulfilled promise will get two variables passed in: ๐Ÿ˜ญ

$promise->then(sub { my ($data, $info) = @_ });

$info is a hash and can have these keys: ๐Ÿ‘‡

  • computed โ€” Will be true if the $compute_function was called successfully and $data is fresh. โœ…
  • expired โ€” Will be true if $data is expired. If this key is present and false, it will indicate that the $data is within the expiration period. The expired key can be found together with both computed and error. โฐ
  • error โ€” Will hold a string if the $compute_function failed. ๐Ÿ’€
๐Ÿšจ Negative $expire is currently EXPERIMENTAL, but unlikely to go away.

๐Ÿช„ memoize_p ๐Ÿช„

$promise = $cache->memoize_p($obj, $method_name, \@args, $expire);
$promise = $cache->memoize_p($class, $method_name, \@args, $expire);

memoize_p behaves the same way as compute_p, but has a convenient interface for calling methods on an object. One of the benefits is that you do not have to come up with your own cache key. This method is pretty much the same as: โœจ

$promise = $cache->compute_p(
  join(":", $cache->namespace, "@M", ref($obj), $method_name, serialize(\@args)),
  $expire,
  sub { return $obj->$method_name(@args) }
);

See compute_p regarding $expire. ๐Ÿ’…

๐Ÿซถ SEE ALSO ๐Ÿซถ

Mojo::Redis

k bye bestie ๐Ÿ’– this iconic page is source/version perl v5.42.2, dated 2026-02-15, and itโ€™s Mojo::Redis::Cache(3) through and through. periodt. ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from