Hash::CompactA hash-based object implementation with key alias and default value support | |
Download |
Hash::Compact Ranking & Summary
Advertisement
- License:
- Perl Artistic License
- Publisher Name:
- Kentaro Kuribayashi
- Publisher web site:
- http://search.cpan.org/~kentaro/
Hash::Compact Tags
Hash::Compact Description
A hash-based object implementation with key alias and default value support When we store some structured value into a column of a relational database or some key/value storage, redundancy of long key names can be a problem for storage space.Hash::Compact is a Perl module, yet another hash-based object implementation which aims to be aware of both space efficiency and easiness to use for us.SYNOPSIS 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; use strict; use warnings; use Test::More; my $key = 'key'; my $value = { foo => 'foo' }; my $memd = My::Memcached->new({servers => }); $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; Requirements: · Perl
Hash::Compact Related Software