SimpleDB::Class::Cache - Memcached interface for SimpleDB.
version 1.0503
An API that allows you to cache item data to a memcached server. Technically I should be storing the item itself, but since the item has a reference to the domain, and the domain has a reference to the simpledb object, it could cause all sorts of problems, so it's just safer to store just the item's data.
use SimpleDB::Class::Cache; my $cache = SimpleDB::Class::Cache->new(servers=>[{host=>'127.0.0.1', port=>11211}]); $cache->set($domain->name, $id, $value); my $value = $cache->get($domain->name, $id); my ($val1, $val2) = @{$cache->mget([[$domain->name, $id1], [$domain->name, $id2]])}; $cache->delete($domain->name, $id); $cache->flush;
These methods are available from this class:
Constructor.
A hash containing configuration params to connect to memcached.
An array reference of servers (sockets and/or hosts). It should look similar to:
[ { host => '127.0.0.1', port=> '11211' }, { socket => '/path/to/unix/socket' }, ]
Returns the array reference of servers passed into the constructor.
Returns a Memcached::libmemcached object, which is constructed using the information passed into the constructor.
Returns a key after it's been processed for completeness. Merges a domain name and a key name with a colon. Keys cannot have any spaces in them, and this fixes that. However, it means that "foo bar" and "foo_bar" are the same thing.
They domain name to process.
They id name to process.
Delete a key from the cache.
Throws SimpleDB::Class::Exception::InvalidParam, SimpleDB::Class::Exception::Connection and SimpleDB::Class::Exception.
The domain name to delete from.
The key to delete.
Empties the caching system.
Throws SimpleDB::Class::Exception::Connection and SimpleDB::Class::Exception.
Retrieves a key value from the cache.
Throws SimpleDB::Class::Exception::InvalidObject, SimpleDB::Class::Exception::InvalidParam, SimpleDB::Class::Exception::ObjectNotFound, SimpleDB::Class::Exception::Connection and SimpleDB::Class::Exception.
The domain name to retrieve from.
The key to retrieve.
Retrieves multiple values from cache at once, which is much faster than retrieving one at a time. Returns an array reference containing the values in the order they were requested.
An array reference of domain names and ids to retrieve.
Sets a key value to the cache.
Throws SimpleDB::Class::Exception::InvalidParam, SimpleDB::Class::Exception::Connection, and SimpleDB::Class::Exception.
The name of the domain to set the info into.
The name of the key to set.
A hash reference to store.
A time in seconds for the cache to exist. Default is 3600 seconds (1 hour).
This class throws a lot of inconvenient, but useful exceptions. If you just want to avoid them you could:
my $value = eval { $cache->get($key) }; if (SimpleDB::Class::Exception::ObjectNotFound->caught) { $value = $db->fetchValueFromTheDatabase; }
The exceptions that can be thrown are:
When an uknown exception happens, or there are no configured memcahed servers in the cacheServers directive in your config file.
When it can't connect to the memcached servers that are configured.
When you pass in the wrong arguments.
When you request a cache key that doesn't exist on any configured memcached server.
When an object can't be thawed from cache due to corruption of some sort.
SimpleDB::Class is Copyright 2009-2010 Plain Black Corporation (http://www.plainblack.com/) and is licensed under the same terms as Perl itself.
To install SimpleDB::Class, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SimpleDB::Class
CPAN shell
perl -MCPAN -e shell install SimpleDB::Class
For more information on module installation, please visit the detailed CPAN module installation guide.