auto serialization of keys

5 views
Skip to first unread message

Jonathan Swartz

unread,
May 21, 2010, 2:46:00 PM5/21/10
to perl-cach...@googlegroups.com
I was considering adding auto serialization of reference keys to CHI.
That means you could do, e.g.

$cache->set(\%params, "some value");

and it would automatically serialize \%params into a scalar key. This
is useful if you want to cache based on all the parameters to a
method, for example. Too often I've seen code doing a half-baked,
error prone serialization like

$cache->set(join(':', $params{foo}, $params{bar}), ...);

I figured I would use a compact Data::Dumper to serialize the key, for
readability's sake in case you call get_keys, and also to maintain
compatibility with drivers that might not like arbitrary binary keys.

But memcached doesn't even allow whitespace or control characters in
keys, so there might have to be an additional digest (e.g. md5)
performed on the key for memcached.

Is this a good/bad idea? Feedback welcome.

Jon

--
You received this message because you are subscribed to the Google Groups "Perl-Cache Discuss" group.
To post to this group, send email to perl-cach...@googlegroups.com.
To unsubscribe from this group, send email to perl-cache-disc...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/perl-cache-discuss?hl=en.

Mike Friedman

unread,
May 21, 2010, 2:50:42 PM5/21/10
to perl-cach...@googlegroups.com
On Fri, May 21, 2010 at 2:46 PM, Jonathan Swartz <swa...@pobox.com> wrote:
> Is this a good/bad idea? Feedback welcome.
>

Good idea. At work we use join-a-bunch-of-method-params method for
cache key construction, and a few weird cases have already bitten us.

I would advise against using a one-way hash (e.g. MD5) for serializing
the structures, as that would make inspecting and debugging the cache
difficult. Instead, how about something like URL-encoded the keys.
That will take care of whitespace and control characters.

Better yet, a pluggable formatter that can take a method name or
subroutine would be ideal.


Mike

Jonathan Swartz

unread,
May 27, 2010, 8:23:51 AM5/27/10
to perl-cach...@googlegroups.com
My plan is to use URLish encoding, but then there's the maximum length
- e.g. 256 for memcached - which would require digesting.

You'll be able to override the methods that serialize/escape/digest
keys with a subclass or role.

Jon

Reply all
Reply to author
Forward
0 new messages