|
NAMECache::Memcached::libmemcached - Perl Interface to libmemcachedSYNOPSISuse Cache::Memcached::libmemcached; my $memd = Cache::Memcached::libmemcached->new({ servers => [ "10.0.0.15:11211", "10.0.0.15:11212", "/var/sock/memcached" ], compress_threshold => 10_000 }); $memd->set("my_key", "Some value"); $memd->set("object_key", { 'complex' => [ "object", 2, 4 ]}); $val = $memd->get("my_key"); $val = $memd->get("object_key"); if ($val) { print $val->{complex}->[2] } $memd->incr("key"); $memd->decr("key"); $memd->incr("key", 2); $memd->delete("key"); $memd->remove("key"); # Alias to delete my $hashref = $memd->get_multi(@keys); # Constants - explicitly by name or by tags # see Memcached::libmemcached::constants for a list use Cache::Memcached::libmemcached qw(MEMCACHED_DISTRIBUTION_CONSISTENT); use Cache::Memcached::libmemcached qw( :defines :memcached_allocated :memcached_behavior :memcached_callback :memcached_connection :memcached_hash :memcached_return :memcached_server_distribution ); # Extra constructor options that are not in Cache::Memcached # See Memcached::libmemcached::constants for a list of available options my $memd = Cache::Memcached::libmemcached->new({ ..., no_block => $boolean, distribution_method => $distribution_method, hashing_algorithm => $hashing_algorithm, }); DESCRIPTIONThis is the Cache::Memcached compatible interface to libmemcached, a C library to interface with memcached.Cache::Memcached::libmemcached is built on top of Memcached::libmemcached. While Memcached::libmemcached aims to port libmemcached API to perl, Cache::Memcached::libmemcached attempts to be API compatible with Cache::Memcached, so it can be used as a drop-in replacement. Note that as of version 0.02000, Cache::Memcached::libmemcached inherits from Memcached::libmemcached. While you are free to use the Memcached::libmemcached specific methods directly on the object, you should use them with care, as it will mean that your code is no longer compatible with the Cache::Memcached API therefore losing some of th portability in case you want to replace it with some other package. FOR Cache::Memcached::LibMemcached USERSCache::Memcached::libmemcached is a rewrite of Cache::Memcached::LibMemcached, using Memcached::libmemcached instead of straight XS as its backend.Therefore you might notice some differences. Here are the ones we are aware of:
Cache::Memcached COMPATIBLE METHODSExcept for the minor incompatiblities, below methods are generally compatible with Cache::Memcached.newTakes on parameter, a hashref of options.set_servers$memd->set_servers( [ qw(serv1:port1 serv2:port2 ...) ]); Sets the server list. getmy $val = $memd->get($key); Retrieves a key from the memcached. Returns the value (automatically thawed with Storable, if necessary) or undef. Currently the arrayref form of $key is NOT supported. Perhaps in the future. get_multimy $hashref = $memd->get_multi(@keys); Retrieves multiple keys from the memcache doing just one query. Returns a hashref of key/value pairs that were available. set$memd->set($key, $value[, $expires]); Unconditionally sets a key to a given value in the memcache. Returns true if it was stored successfully. Currently the arrayref form of $key is NOT supported. Perhaps in the future. add$memd->add($key, $value[, $expires]); Like set(), but only stores in memcache if they key doesn't already exist. replace$memd->replace($key, $value[, $expires]); Like set(), but only stores in memcache if they key already exist. append$memd->append($key, $value); Appends $value to whatever value associated with $key. Only available for memcached > 1.2.4 prepend$memd->prepend($key, $value); Prepends $value to whatever value associated with $key. Only available for memcached > 1.2.4 incrdecrmy $newval = $memd->incr($key); my $newval = $memd->decr($key); my $newval = $memd->incr($key, $offset); my $newval = $memd->decr($key, $offset); Atomically increments or decrements the specified the integer value specified by $key. Returns undef if the key doesn't exist on the server. deleteremove$memd->delete($key); Deletes a key. XXX - The behavior when second argument is specified may differ from Cache::Memcached -- this hasn't been very well tested. Patches welcome! flush_all$memd->fush_all; Runs the memcached "flush_all" command on all configured hosts, emptying all their caches. set_compress_threshold$memd->set_compress_threshold($threshold); Set the compress threshold. enable_compress$memd->enable_compress($bool); This is actually an alias to set_compress_enable(). The original version from Cache::Memcached is, despite its naming, a setter as well. statsmy $h = $memd->stats(); This method is still half-baked. It gives you some stats. If the values are wrong, well, reports, or better yet, patches welcome. disconnect_allDisconnects from serverscas$memd->cas($key, $cas, $value[, $exptime]); XXX - This method is still broken. Sets if $cas matches the value on the server. getsget_casmy $cas = $memd->gets($key); my $cas = $memd->get_cas($key); Get the CAS value for $key get_cas_multimy $h = $memd->get_cas_multi(@keys) Gets CAS values for multiple keys Cache::Memcached::libmemcached SPECIFIC METHODSThese methods are libmemcached-specific.server_addAdds a memcached server.server_add_unix_socketAdds a memcached server, connecting via unix socket.server_list_freeFrees the memcached server list.UTILITY METHODSWARNING: Please do not consider the existance for these methods to be final. They may be renamed or may entirely disappear from future releases.get_compress_thresholdReturn the current value of compress_thresholdset_compress_enableSet the value of compress_enableget_compress_enableReturn the current value of compress_enableset_compress_savingsSet the value of compress_savingsget_compress_savingsReturn the current value of compress_savingsBEHAVIOR CUSTOMIZATIONCertain libmemcached behaviors can be configured with the following methods.(NOTE: This API is not fixed yet) behavior_setbehavior_getIf you want to customize something that we don't have a wrapper for, you can directly use these method.set_no_blockCache::Memcached::libmemcached->new({ ... no_block => 1 }); # or $memd->set_no_block( 1 ); Set to use blocking/non-blocking I/O. When this is in effect, get() becomes flaky, so don't attempt to call it. This has the most effect for set() operations, because libmemcached stops waiting for server response after writing to the socket (set() will also always return success) Please consult the man page for "memcached_behavior_set()" for details before setting. is_no_blockGet the current value of no_block behavior.set_distribution_method$memd->set_distribution_method( MEMCACHED_DISTRIBUTION_CONSISTENT ); Set the distribution behavior. get_distribution_methodGet the distribution behavior.set_hashing_algorithm$memd->set_hashing_algorithm( MEMCACHED_HASH_KETAMA ); Set the hashing algorithm used. get_hashing_algorithmGet the hashing algorithm used.set_support_cas$memd->set_support_cas($boolean); # or $memd = Cache::Memcached::libmemcached->new( { ... support_cas => 1 } ); Enable/disable CAS support. set_binary_protocol$memd->set_binary_protocol( 1 ); $binary = $memd->is_binary_protocol(); Enable/disable binary protocol OPTIMIZE FLAGThere's an EXPERIMENTAL optimization available for some corner cases, where if you know before hand that you won't be using some features, you can disable them all together for some performance boost. To enable this mode, set an environment variable named PERL_LIBMEMCACHED_OPTIMIZE to a true valueNO MASTER KEY SUPPORTIf you are 100% sure that you won't be using the master key support, where you provide an arrayref as the key, you get about 4~5% performance boost.VARIOUS MEMCACHED MODULESBelow are the various memcached modules available on CPAN.Please check tool/benchmark.pl for a live comparison of these modules. (except for Cache::Memcached::XS, which I wasn't able to compile under my main dev environment) Cache::MemcachedThis is the "main" module. It's mostly written in Perl.Cache::Memcached::libmemcachedCache::Memcached::libmemcached, which is the module for which your reading the document of, is a perl binding for libmemcached (http://tangent.org/552/libmemcached.html). Not to be confused with libmemcache (see below).Cache::Memcached::FastCache::Memcached::Fast is a memcached client written in XS from scratch. As of this writing benchmarks shows that Cache::Memcached::Fast is faster on get_multi(), and Cache::Memcached::libmemcached is faster on regular get()/set()Memcached::libmemcachedMemcached::libmemcached is a straight binding to libmemcached, and is also the parent class of this module.It has most of the libmemcached API. If you don't care about a drop-in replacement for Cache::Memcached, and want to benefit from low level API that libmemcached offers, this is the way to go. Cache::Memcached::XSCache::Memcached::XS is a binding for libmemcache (http://people.freebsd.org/~seanc/libmemcache/). The main memcached site at http://danga.com/memcached/apis.bml seems to indicate that the underlying libmemcache is no longer in active development.CAVEATSUnless you know what you're getting yourself into, don't try to subclass this module just yet. Internal structures may change without notice.AUTHORCopyright (c) 2008 Daisuke Maki <daisuke@endeworks.jp>LICENSEThis program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.See http://www.perl.com/perl/misc/Artistic.html
Visit the GSP FreeBSD Man Page Interface. |