Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added example to use with memcached

  • Loading branch information...
commit dd2259f597919eee72d83f0cd8cd4c58666561d9 1 parent 2535045
@kentaro authored
Showing with 56 additions and 0 deletions.
  1. +56 −0 eg/with_memcached.pl
View
56 eg/with_memcached.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+package My::Memcached;
+use strict;
+use warnings;
+use parent qw(Cache::Memcached::Fast);
+
+use JSON;
+use Hash::Compact;
+
+my $options = {
+ foo => {
+ alias_for => 'f',
+ },
+ bar => {
+ alias_for => 'b',
+ default => 'bar',
+ },
+};
+
+sub get {
+ my ($self, $key) = @_;
+ my $value = $self->SUPER::get($key);
+ Hash::Compact->new(decode_json $value, $options);
+}
+
+sub set {
+ my ($self, $key, $value, $expire) = @_;
+ my $hash = Hash::Compact->new($value, $options);
+ $self->SUPER::set($key, encode_json $hash->to_hash, $expire);
+}
+
+package main;
+
+my $key = 'key';
+my $value = { foo => 'foo' };
+my $memd = My::Memcached->new({servers => [qw(localhost:11211)]});
+ $memd->set($key, $value);
+
+my $cached_value = $memd->get($key);
+is_deeply $cached_value->param('foo'), 'foo';
+is_deeply $cached_value->param('bar'), 'bar';
+is_deeply $cached_value->to_hash, +{ f => 'foo' };
+
+$cached_value->param(bar => 'baz');
+$memd->set($key, $cached_value->to_hash);
+
+$cached_value = $memd->get($key);
+is_deeply $cached_value->param('foo'), 'foo';
+is_deeply $cached_value->param('bar'), 'baz';
+is_deeply $cached_value->to_hash, +{ f => 'foo', b => 'baz' };
+
+done_testing;

0 comments on commit dd2259f

Please sign in to comment.
Something went wrong with that request. Please try again.