Cache-RedisDB

 view release on metacpan or  search on metacpan

lib/Cache/RedisDB.pm  view on Meta::CPAN

    state $redis;
    $redis //= redis_connection();
    return $redis;
}

=head2 get

Takes a C<$namespace> and C<$key> parameter, and returns the scalar value
corresponding to that cache entry.

This will automatically deserialise data stored with L<Sereal>. If no data
is found, this will return C<undef>.

=cut

sub get {
    my ($self, $namespace, $key) = @_;
    my $res = redis()->get(_cache_key($namespace, $key));
    if (looks_like_sereal($res)) {
        state $decoder = Sereal::Decoder->new();
        $res = $decoder->decode($res);
    }
    return $res;
}

=head2 mget

Retrieve values for multiple keys in a single call.

Similar to L</get>, this takes a C<$namespace> as the first parameter,
but it also accepts a list of C<@keys> to look up.

Returns an arrayref in the same order as the original keys. For any
key that had no value, the resulting arrayref will contain C<undef>.

=cut

sub mget {
    my ($self, $namespace, @keys) = @_;
    my $res = Cache::RedisDB::redis()->mget(map { Cache::RedisDB::_cache_key($namespace, $_) } @keys);
    state $decoder = Sereal::Decoder->new();
    return [map { ($_ && looks_like_sereal($_)) ? $decoder->decode($_) : $_ } @$res];
}

=head2 set

Creates or updates a Redis key under C<$namespace>, C<$key> using the scalar C<$value>.
Also takes an optional C<$exptime> as expiration time in seconds.

 $redis->set($namespace, $key, $value);
 $redis->set($namespace, $key, $value, $expiry_time);

Can also be provided a callback which will be executed once the command completes.

=cut

sub set {
    my ($self, $namespace, $key, $value, $exptime, $callback) = @_;
    if (not defined $value or ref $value or Encode::is_utf8($value)) {
        state $encoder = Sereal::Encoder->new({
            freeze_callbacks => 1,
        });
        $value = $encoder->encode($value);
    }
    my $cache_key = _cache_key($namespace, $key);
    if (defined $exptime) {
        $exptime = int(1000 * $exptime);
        # PX milliseconds -- Set the specified expire time, in milliseconds
        return redis()->set($cache_key, $value, "PX", $exptime, $callback // ());
    } else {
        return redis()->set($cache_key, $value, $callback // ());
    }
}

=head2 set_nw($namespace, $key, $value[, $exptime])

Same as I<set> but do not wait confirmation from server. If the server returns
an error, it will be ignored.

=cut

sub set_nw {
    my ($self, $namespace, $key, $value, $exptime) = @_;
    return $self->set($namespace, $key, $value, $exptime, RedisDB::IGNORE_REPLY);
}

=head2 del($namespace, $key1[, $key2, ...])

Delete given keys and associated values from the cache. I<$namespace> is common for all keys.
Returns number of deleted keys.

=cut

sub del {
    my ($self, $namespace, @keys) = @_;
    return redis->del(map { _cache_key($namespace, $_) } @keys);
}

=head2 keys($namespace)

Return an arrayref of all known keys in the provided C<$namespace>.

=cut

sub keys : method {    ## no critic (ProhibitBuiltinHomonyms)
    my ($self, $namespace) = @_;
    my $prefix = _cache_key($namespace, undef);
    my $pl     = length($prefix);
    return [map { substr($_, $pl) } @{redis()->keys($prefix . '*')}];
}

=head2 ttl($namespace, $key)

Return the Time To Live (in seconds) of a key in the provided I<$namespace>.

=cut

sub ttl {
    my ($self, $namespace, $key) = @_;

    my $ms = redis()->pttl(_cache_key($namespace, $key));



( run in 0.516 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )