libmemcached(3pm)

Hey bestie!! πŸ’– Welcome to libmemcached(3pm), straight outta the User Contributed Perl Documentation. This is your no-cap guide to the Perl wrapper that's lowkey obsessed with the libmemcached C library, and it is so detailed. Buckle up, because this page is long and it's coming in three parts (you're in part 1, and yes, more is coming).

✨ NAME ✨

Memcached::libmemcached β€” Thin fast full interface to the libmemcached client API.

πŸ’– VERSION πŸ’–

Version 1.001801 (with libmemcached-1.0.18 embedded). Periodt.

πŸ¦‹ SYNOPSIS πŸ¦‹

use Memcached::libmemcached;

$memc = memcached_create();

memcached_server_add($memc, "localhost");

memcached_set($memc, $key, $value);

$value = memcached_get($memc, $key);

🌸 DESCRIPTION 🌸

Okay so Memcached::libmemcached is a very thin, highly efficient wrapper around the libmemcached library. It's implemented almost entirely in C. Iconic.

It gives you full access to the rich functionality offered by libmemcached. And libmemcached itself is fast, light on memory usage, thread safe, and gives full access to server side methods. Not me hyping it up, it's literally in the docs. πŸ’…

Here's the vibe check list:

  • Synchronous and Asynchronous support.
  • TCP and Unix Socket protocols.
  • A half dozen or so different hash algorithms.
  • Implementations of the new cas, replace, and append operators.
  • Man pages written up on entire API.
  • Implements both modulo and consistent hashing solutions.

(Memcached::libmemcached is fairly new and not all the functions in libmemcached have perl interfaces yet. It's usually trivial to add functions - just a few lines in libmemcached.xs, a few lines of documentation, and a few lines of testing. Volunteers welcome!) πŸ™ˆ

The libmemcached library documentation (which is bundled with this module) serves as the primary reference for the functionality.

This documentation provides summary of the functions, along with any issues specific to this perl interface, and references to the documentation for the corresponding functions in the underlying library.

For more information on libmemcached, see http://docs.libmemcached.org

🌈 CONVENTIONS 🌈

Terminology

The term "memcache" is used to refer to the "memcached_st" structure at the heart of the libmemcached library. We'll use $memc to represent this structure in perl code. (The libmemcached library documentation uses "ptr".)

Traditional and Object-Oriented

There are two ways to use the functionality offered by this module:

  • You can import the functions you want to use and call them explicitly.
  • Or, you can use $memc as an object and call most of the functions as methods. You can do that for any function that takes a $memc (ptr) as its first argument, which is almost all of them.

Since the primary focus of this module is to be a thin wrapper around libmemcached, the bulk of this documentation describes the traditional functional interface.

The object-oriented interface is mainly targeted at modules wishing to subclass Memcached::libmemcached, such as Cache::Memcached::libmemcached. For more information see "OBJECT-ORIENTED INTERFACE".

Function Names and Arguments

The function names in the libmemcached library have exactly the same names in Memcached::libmemcached.

The function arguments are also the same as the libmemcached library and documentation, with two exceptions:

  • There are no length arguments. Wherever the libmemcached documentation shows a length argument (input or output) the corresponding argument doesn't exist in the Perl API because it's not needed.
  • Some arguments are optional.

Many libmemcached function arguments are output values: the argument is the address of the value that the function will modify. For these the perl function will modify the argument directly if it can. For example, in this call:

$value = memcached_get($memc, $key, $flags, $rc);

The $flags and $rc arguments are output values that are modified by the memcached_get() function.

See the "Type Mapping" section for the fine detail of how each argument type is handled.

Return Status

Most of the functions return an integer status value. This is shown as "memcached_return" in the libmemcached documentation.

In the perl interface this value is not returned directly. Instead a simple boolean is returned: true for 'success', defined but false for some 'unsuccessful' conditions like 'not found', and undef for all other cases (i.e., errors).

All the functions documented below return this simple boolean value unless otherwise indicated.

The actual "memcached_return" integer value, and corresponding error message, for the last libmemcached function call can be accessed via the "errstr" method.

Unimplemented Functions

Functions relating to managing lists of servers (memcached_server_push, and memcached_server_list) have not been implemented because they're not needed and likely to be deprecated by libmemcached.

Functions relating to iterating through results (memcached_result_*) have not been implemented yet. They're not a priority because similar functionality is available via the callbacks. See "set_callback_coderefs".

πŸ‘‘ EXPORTS πŸ‘‘

All the public functions in libmemcached are available for import.

All the public constants and enums in libmemcached are also available for import.

Exporter tags are defined for each enum. This allows you to import groups of constants easily. For example, to enable consistent hashing you could use:

use Memcached::libmemcached qw(:memcached_behavior :memcached_server_distribution);

memcached_behavior_set($memc, MEMCACHED_BEHAVIOR_DISTRIBUTION(), MEMCACHED_DISTRIBUTION_CONSISTENT());

The Exporter module allows patterns in the import list, so to import all the functions, for example, you can use:

use Memcached::libmemcached qw(/^memcached/);

Refer to Memcached::libmemcached::constants for a full list of the available constants and the tags they are grouped by. To see a list of all available functions and constants you can execute:

perl -MMemcached::libmemcached -le 'print $_ for @Memcached::libmemcached::EXPORT_OK'

πŸ’… FUNCTIONS πŸ’…

Functions For Managing Memcaches

memcached_create

my $memc = memcached_create();

Creates and returns a 'memcache' that represents the state of communication with a set of memcached servers. See Memcached::libmemcached::memcached_create.

memcached_clone

my $memc = memcached_clone(undef, undef);

XXX Not currently recommended for use. See Memcached::libmemcached::memcached_create.

memcached_free

memcached_free($memc);

Frees the memory associated with $memc. After calling it $memc can't be used. See Memcached::libmemcached::memcached_create.

memcached_server_count

$server_count= memcached_server_count($memc);

Returns a count of the number of servers associated with $memc. See Memcached::libmemcached::memcached_servers.

memcached_server_add and memcached_server_add_with_weight

memcached_server_add($memc, $hostname, $port);
memcached_server_add_with_weight($memc, $hostname, $port, $weight);

Adds details of a single memcached server (accessed via TCP/IP) to $memc. See Memcached::libmemcached::memcached_servers. The default weight is 0.

memcached_server_add_unix_socket and memcached_server_add_unix_socket_with_weight

memcached_server_add_unix_socket($memc, $socket_path);
memcached_server_add_unix_socket_with_weight($memc, $socket_path);

Adds details of a single memcached server (accessed via a UNIX domain socket) to $memc. See Memcached::libmemcached::memcached_servers. The default weight is 0.

memcached_behavior_set

memcached_behavior_set($memc, $option_key, $option_value);

Changes the value of a particular option. See Memcached::libmemcached::memcached_behavior.

memcached_behavior_get

memcached_behavior_get($memc, $option_key);

Get the value of a particular option. See Memcached::libmemcached::memcached_behavior.

memcached_callback_set

memcached_callback_set($memc, $flag, $value);

Set callback flag value.

The only flag currently supported is "MEMCACHED_CALLBACK_PREFIX_KEY". The $value must be less than MEMCACHED_PREFIX_KEY_MAX_SIZE (eg 128) bytes. It also can't be empty https://bugs.launchpad.net/libmemcached/+bug/667878

memcached_callback_get

$value = memcached_callback_set($memc, $flag, $return_status);

Get callback flag value. Sets return status in $return_status. The only flag currently supported is "MEMCACHED_CALLBACK_PREFIX_KEY". Returns undef on error.

Functions for Setting Values

See Memcached::libmemcached::memcached_set.

memcached_set

memcached_set($memc, $key, $value);
memcached_set($memc, $key, $value, $expiration, $flags);

Set $value as the value of $key. $expiration and $flags are both optional and default to 0.

memcached_add

memcached_add($memc, $key, $value);
memcached_add($memc, $key, $value, $expiration, $flags);

Like "memcached_set" except that an error is returned if $key is already stored in the server.

memcached_replace

memcached_replace($memc, $key, $value);
memcached_replace($memc, $key, $value, $expiration, $flags);

Like "memcached_set" except that an error is returned if $key is not already error is returned.

memcached_prepend

memcached_prepend($memc, $key, $value);
memcached_prepend($memc, $key, $value, $expiration, $flags);

Prepend $value to the value of $key. $key must already exist. $expiration and $flags are both optional and default to 0.

memcached_append

memcached_append($memc, $key, $value);
memcached_append($memc, $key, $value, $expiration, $flags);

Append $value to the value of $key. $key must already exist. $expiration and $flags are both optional and default to 0.

memcached_cas

memcached_cas($memc, $key, $value, $expiration, $flags, $cas)

Overwrites data in the server stored as $key as long as $cas still has the same value in the server.

Cas is still buggy in memached. Turning on support for it in libmemcached is optional. Please see memcached_behavior_set() for information on how to do this.

XXX and the memcached_result_cas() function isn't implemented yet so you can't get the $cas to use.

Functions for Fetching Values

See Memcached::libmemcached::memcached_get.

The memcached_fetch_result() and

memcached_get

$value = memcached_get($memc, $key);
$value = memcached_get($memc, $key, $flags, $rc);

Get and return the value of $key. Returns undef on error.

Also updates $flags to the value of the flags stored with $value, and updates $rc with the return code.

memcached_mget

memcached_mget($memc, \@keys);
memcached_mget($memc, \%keys);

Triggers the asynchronous fetching of multiple keys at once. For multiple key operations it is always faster to use this function. You must then use memcached_fetch() or memcached_fetch_result() to retrieve any keys found. No error is given on keys that are not found.

Instead of this function, you'd normally use the "mget_into_hashref" method.

memcached_fetch

memcached_fetch (still going, bestie, hold on) πŸ’…

$value = memcached_fetch($memc, $key);
$value = memcached_fetch($memc, $key, $flag, $rc);

So this one grabs the next $key and $value pair that came back in response to a memcached_mget() call. When there's literally nothing left to hand you, it returns undef. That's your "ok we're done here" signal 🫢

If you pass $flag, it gets updated to whatever flags were stored alongside the value. If you pass $rc, it gets updated to the return code. Both just get filled in for you, no cap.

It's giving very much memcached_get, except it's fetching the results from that previous memcached_mget call, and $key is an output parameter instead of an input one. Also, usually you'd just use the mget_into_hashref method instead, iykyk. πŸŽ€


✨ Functions for Incrementing and Decrementing Values ✨

So memcached servers can increment and decrement unsigned integer keys β€” and heads up, overflow and underflow are not detected πŸ’€. This gives you the ability to use memcached to generate shared sequences of values, which is lowkey iconic.

See Memcached::libmemcached::memcached_auto.

memcached_increment

memcached_increment( $key, $offset, $new_value_out );

Increments the integer value associated with $key by $offset, and hands you the new value back in $new_value_out. πŸ’–

memcached_decrement

memcached_decrement( $key, $offset, $new_value_out );

Decrements the integer value associated with $key by $offset, and drops the new value into $new_value_out. ✨

memcached_increment_with_initial

memcached_increment_with_initial( $key, $offset, $initial, $expiration, $new_value_out );

Increments the integer value associated with $key by $offset and returns the new value in $new_value_out.

Now here's the plot twist πŸŒ€ β€” if the object specified by key doesn't exist, one of two things can happen:

  • If the expiration value is MEMCACHED_EXPIRATION_NOT_ADD, the operation will fail. Periodt. 🚫
  • For all other expiration values, the operation will succeed by seeding the value for that key with an initial value, set to expire with the provided expiration time.

Either way the flags get set to zero. 🧚

memcached_decrement_with_initial

memcached_decrement_with_initial( $key, $offset, $initial, $expiration, $new_value_out );

Decrements the integer value associated with $key by $offset and returns the new value in $new_value_out.

And yep, same energy as above 🫢 if the object specified by key doesn't exist:

  • If the expiration value is MEMCACHED_EXPIRATION_NOT_ADD, the operation will fail.
  • For all other expiration values, the operation will succeed by seeding the value for that key with an initial value to expire with the provided expiration time.

Flags? Set to zero. Always zero. πŸŽ€

The _by_key master key variants

  • memcached_increment_by_key
  • memcached_decrement_by_key
  • memcached_increment_with_initial_by_key
  • memcached_decrement_with_initial_by_key

These are the master key equivalents of everything above ⬆️. They all take an extra initial $master_key parameter. That's the whole difference, bestie. πŸ‘‘


πŸ’– Functions for Deleting Values from memcached πŸ’–

See Memcached::libmemcached::memcached_delete.

memcached_delete

memcached_delete($memc, $key);
memcached_delete($memc, $key, $expiration);

Deletes $key. And if $expiration is greater than zero, then the key is deleted by memcached after that many seconds β€” so it's a lil delayed goodbye instead of an instant one. ⏳


πŸŽ€ Functions for Accessing Statistics from memcached πŸŽ€

Not yet implemented. 😭 See Memcached::libmemcached::memcached_stats.

Also see walk_stats.


🌸 Miscellaneous Functions 🌸

memcached_lib_version

$version = memcached_lib_version()

Returns a simple version string, like "1.0.17", representing the libmemcached version. And to be clear βœ‹ that's the version of the client library, not the server. Important distinction, don't get it twisted.

memcached_version

$version = memcached_version($memc)
($version1, $version2, $version3) = memcached_version($memc)

Returns the lowest version of all the memcached servers. Yes, the lowest. It's a vibe check across every server you're talking to.

  • In scalar context it returns a simple version string, like "1.2.3".
  • In list context it returns the individual version component numbers.
  • It returns an empty list if there was an error. πŸ™ˆ
Heads up: the return value differs from that of the underlying libmemcached library memcached_version() function. Don't @ me, that's just how it is.

memcached_verbosity

memcached_verbosity($memc, $verbosity)

Modifies the "verbosity" of the memcached servers associated with $memc. See Memcached::libmemcached::memcached_verbosity.

memcached_flush

memcached_flush($memc, $expiration);

Wipe clean the contents of associated memcached servers. Full reset, main character style. ✨ See Memcached::libmemcached::memcached_flush.

memcached_quit

memcached_quit($memc)

Disconnects from all currently connected servers and resets the libmemcached state associated with $memc. Not normally called explicitly β€” it just kinda happens. See Memcached::libmemcached::memcached_quit.

memcached_strerror

$string = memcached_strerror($memc, $return_code)

memcached_strerror() takes a "memcached_return" value and returns a string describing the error. That string should be treated as read-only β€” it may literally be so in future versions πŸ‘€. See also Memcached::libmemcached::memcached_strerror.

Honestly? This function is rarely needed in the Perl interface, because the return code is a dualvar that already contains the error string. So like... it's there, but you probably won't need it. πŸ’…


πŸ‘‘ Grouping Keys On Servers πŸ‘‘

Normally libmemcached hashes the $key value to select which memcached server to communicate with. Which is fine, except: if you have several keys relating to a single object, then it's very likely that the corresponding values will be stored in different memcached servers. Not ideal. 😬

It would be more efficient, in general, when setting and getting multiple related values, if it was possible to specify a different value to be hashed to select which memcached server to communicate with. And with libmemcached? You can. Slay. πŸ”₯

Most of the functions for setting and getting values have *_by_key variants for exactly this reason. These all have an extra $master_key parameter immediately after the $memc parameter. For example:

memcached_mget($memc, \%keys, \%dest);

memcached_mget_by_key($memc, $maskey_key, \%keys, \%dest);

The *_by_key variants all work in exactly the same way as the corresponding plain function, except that libmemcached hashes $master_key instead of $key to figure out which memcached server to communicate with. That's it. That's the whole trick. πŸŽ€

And if $master_key is undef? Then the functions behave the same as their non-by-key variants β€” i.e., $key is used for hashing. So you're never stranded.

By-key variants of "Functions for Fetching Values":

  • memcached_get_by_key
  • memcached_mget_by_key

By-key variants of "Functions for Setting Values":

  • memcached_set_by_key
  • memcached_replace_by_key
  • memcached_add_by_key
  • memcached_append_by_key
  • memcached_cas_by_key
  • memcached_prepend_by_key
  • memcached_delete_by_key

πŸ’– OBJECT-ORIENTED INTERFACE πŸ’–

✨ Methods ✨

  • new
$memc = $class->new; # same as memcached_create()

Literally the baby-step constructor. It does the exact same thing as memcached_create(), no cap. πŸ‘‘


  • errstr
$errstr = $memc->errstr;

Okay bestie, this one hands you the error message and the code from the most recent call to literally any libmemcached function that returns a memcached_return β€” and like, most of them do. 🫢

The return value is a dualvar, just like $! β€” meaning it's holding two separate lives: a numeric value and a string value. The numeric half is the memcached_return integer value, and the string half is the matching error message that memcached_strerror() would give you. Main character with two jobs. πŸ’…

Special case alert 🚨: if the memcached_return is MEMCACHED_ERRNO (which means a system call error went down), then the string returned by strerror() gets appended to it.

Also, real talk: this method is currently still callable as memcached_errstr() for compatibility with an earlier version β€” but that deprecated alias is gonna start warning and then straight up cease to exist in future versions. So like, migrate. Periodt. 😭


  • mget_into_hashref
$memc->mget_into_hashref( \@keys, \%dest_hash); # keys from array
$memc->mget_into_hashref( \%keys, \%dest_hash); # keys from hash

This one is the moment. It combines memcached_mget() and a memcached_fetch() loop into a single highly efficient call. The way it ate. πŸ”₯

Fetched values land in \%dest_hash, updating existing values or adding new ones as appropriate. Very flexible, very demure.

Same deal as before though: it's currently callable as memcached_mget_into_hashref() for compatibility with an earlier version, and that deprecated alias will start warning and then cease to exist in future versions. 😬


  • get_multi
$hash_ref = $memc->get_multi( @keys );

Effectively the same as:

$memc->mget_into_hashref( \@keys, $hash_ref = { } )

So it's very similar to "mget_into_hashref" but lowkey less efficient for large numbers of keys β€” because all those keys have to be pushed onto the argument stack πŸ’€ β€” and also less flexible, because you can't add/update elements into an existing hash.

This method exists to optimize subclasses that want to provide a Cache::Memcached compatible API with maximum efficiency. ✨ But heads up: "get_multi" does not support the Cache::Memcached feature where a key can be a reference to an array [ $master_key, $key ]. If you need that feature, go use "memcached_mget_by_key" directly. πŸŽ€


  • get
$value = $memc->get( $key );

Effectively the same as:

$value = memcached_get( $memc, $key );

The "get" method does support the Cache::Memcached feature where $key can be a reference to an array [ $master_key, $key ]. In that case, the call is effectively the same as:

$value = memcached_get_by_key( $memc, $key->[0], $key->[1] )

Iconic behavior honestly. 🌸


  • set_callback_coderefs
$memc->set_callback_coderefs(\&set_callback, \&get_callback);

⚠️ This interface is experimental and likely to change. (It's also currently used by Cache::Memcached::libmemcached, so don't you dare use it if you're using that module.)

You use it to specify functions that will be executed when values are set and/or get using $memc.

When the callbacks run, $_ is the value and the arguments are the key and flags value. Both $_ and the flags may be modified. πŸ‘€

Currently, the functions must return an empty list.

And yeah, predictably: it's currently callable as memcached_set_callback_coderefs() for compatibility with an earlier version, but that deprecated alias will start warning and then cease to exist in future versions. Bye bestie. πŸ’€


  • walk_stats
$memc->walk_stats( $stats_args, \&my_stats_callback );

Also experimental and likely to change, so just be aware.

It calls the memcached_stat_execute() function to issue a STAT $stats_args command to the connected memcached servers. The $stats_args argument is usually just an empty string. (Empty string = the vibe.)

The callback function gets called for each return value from each server. It'll be passed at least these parameters:

sub my_stats_callback {
  my ($key, $value, $hostport) = @_;
  # Do what you like with the above!
  return;
}

Currently the callback must return an empty list.

Now, history lesson πŸ“œ: prior to version 0.4402 the callback was passed a fourth argument which was a copy of the $stats_args value. That is no longer the case. As a temporary aid to migration, the "walk_stats" method does local $_ = $stats_args and passes $_ as the forth argument. That will work so long as the code in the callback doesn't alter $_. If your callback code requires $stats_args you should change it to be a closure instead. Worth it, I promise. πŸ¦‹


trace_level

$memc->trace_level($trace_level);
$trace_level = $memc->trace_level;

Sets the trace level (peep "Tracing Execution" down below πŸ‘‡). Returns the previous trace level. Very "here's the receipts" of it.


get_server_for_key

$memc->get_server_for_key( $key )

This method uses memcached_server_by_key to get the info about which server should contain the specified $key.

It returns a string containing the hostname:port of the appropriate server, or undef on failure. Simple, clean, slay. ✨


🌈 EXTRA INFORMATION 🌈

πŸ” Tracing Execution πŸ”

$memc->trace_level($trace_level);
  • If set >= 1 then any non-success memcached_return value will be logged via warn().
  • If set >= 2 or more then some data types will list conversions of input and output values for function calls. πŸ•΅οΈβ€β™€οΈ

The "PERL_LIBMEMCACHED_TRACE" environment variable provides a default. The value is read when memcached_create is called. Keep that in mind, iykyk.


πŸ”„ Type Mapping πŸ”„

For pointer arguments, undef is mapped to null on input and null is mapped to undef on output. Symmetry! Beautiful! πŸ’«

XXX expand with details from typemap file

(Not me leaving the original's XXX note exactly as-is. It's giving "unfinished homework" but I respect the honesty. πŸ™ˆ)


🚫 Deprecated Functions 🚫

The following functions are available but deprecated in this release. In the next release they'll generate warnings. In a future release they'll be removed. So like, this is your official warning: move on bestie. πŸ’€

  • memcached_errstr β€” Use "errstr" instead.
  • memcached_mget_into_hashref β€” Use "mget_into_hashref" instead.
  • memcached_set_callback_coderefs β€” Use "set_callback_coderefs" instead.

πŸ‘‘ AUTHOR EMERITUS πŸ‘‘

Tim Bunce, "<Tim.Bunce@pobox.com>" with help from Patrick Galbraith and Daisuke Maki.

<http://www.tim.bunce.name>


πŸ’œ CURRENT MAINTAINER πŸ’œ

Matthew Horsfall (alh) "<wolfsage@gmail.com>"

Daisuke Maki "<daisuke@endeworks.jp>" with occasional bursts of input from Tim Bunce. (Occasional bursts ✨ love that for them.)


πŸŽ€ ACKNOWLEDGEMENTS πŸŽ€

Larry Wall for Perl, Brad Fitzpatrick for memcached, Brian Aker for libmemcached, and Patrick Galbraith and Daisuke Maki for helping with the implementation. Legends, every single one. 🫢


🌍 PORTABILITY 🌍

Go check out Slaven Rezic's excellent CPAN Testers Matrix at <http://matrix.cpantesters.org/?dist=Memcached-libmemcached> πŸ’…

Along with Dave Cantrell's excellent CPAN Dependency tracker at <http://deps.cpantesters.org/?module=Memcached%3A%3Alibmemcached&perl=any+version&os=any+OS>


πŸ› BUGS πŸ›

Please report any bugs or feature requests to the GitHub issue tracker at <https://github.com/timbunce/Memcached-libmemcached/issues>. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes. It's giving "we'll text back" and actually meaning it. 😌


πŸ¦‹ CONTRIBUTING πŸ¦‹

The source is hosted at github: <https://github.com/timbunce/Memcached-libmemcached> β€” Patches and volunteers always welcome. Come through! 🌸


Copyright 2008 Tim Bunce, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Okay that's literally the whole page πŸ₯Ή this was libmemcached(3pm) from perl v5.42.3, dated 2026-08-19. Go forth and cache, bestie. πŸ’–βœ¨πŸ‘‘
πŸ“¦ where this page comes from