The memcached MySQL User Defined Functions (UDFs) enable you to set and retrieve objects from within MySQL 5.0 or greater.
To install the MySQL memcached UDFs, download
the UDF package from http://libmemcached.org/.
Unpack the package and run configure to
configure the build process. When running
configure, use the
--with-mysql
option and specify the location
of the mysql_config command.
shell>tar zxf memcached_functions_mysql-0.5.tar.gz
shell>cd memcached_functions_mysql-0.5
shell>./configure --with-mysql-config=/usr/local/mysql/bin/mysql_config
Now build and install the functions:
shell>make
shell>make install
Copy the MySQL memcached UDFs into your MySQL plugins directory:
shell> cp /usr/local/lib/libmemcached_functions_mysql* /usr/local/mysql/lib/mysql/plugins/
The plugin directory is given by the value of the
plugin_dir
system variable. For
more information, see Section 23.3.2.5, “Compiling and Installing User-Defined Functions”.
Once installed, you must initialize the function within MySQL
using CREATE
and specifying the return value
and library. For example, to add the
memc_get()
function:
mysql> CREATE FUNCTION memc_get RETURNS STRING SONAME "libmemcached_functions_mysql.so";
You must repeat this process for each function that you want to
provide access to within MySQL. Once you have created the
association, the information is retained, even over restarts of
the MySQL server. You can simplify the process by using the SQL
script provided in the memcached
UDFs
package:
shell> mysql <sql/install_functions.sql
Alternatively, if you have Perl installed, then you can use the supplied Perl script, which checks for the existence of each function and creates the function/library association if it is not already defined:
shell> utils/install.pl --silent
The --silent
option installs everything
automatically. Without this option, the script asks whether you
want to install each of the available functions.
The interface remains consistent with the other APIs and
interfaces. To set up a list of servers, use the
memc_servers_set()
function, which accepts a
single string containing and comma-separated list of servers:
mysql> SELECT memc_servers_set('192.168.0.1:11211,192.168.0.2:11211');
The list of servers used by the memcached UDFs is not persistent over restarts of the MySQL server. If the MySQL server fails, then you must re-set the list of memcached servers.
To set a value, use memc_set
:
mysql> SELECT memc_set('myid', 'myvalue');
To retrieve a stored value:
mysql> SELECT memc_get('myid');
The list of functions supported by the UDFs, in relation to the standard protocol functions, is shown in the following table.
MySQL memcached UDF Function | Equivalent to |
---|---|
memc_get() | Generic get() |
memc_get_by_key(master_key, key, value) | Like the generic get() , but uses the supplied master
key to select the server to use. |
memc_set() | Generic set() |
memc_set_by_key(master_key, key, value) | Like the generic set() , but uses the supplied master
key to select the server to use. |
memc_add() | Generic add() |
memc_add_by_key(master_key, key, value) | Like the generic add() , but uses the supplied master
key to select the server to use. |
memc_replace() | Generic replace() |
memc_replace_by_key(master_key, key, value) | Like the generic replace() , but uses the supplied
master key to select the server to use. |
memc_prepend(key, value) | Prepend the specified value to the current value of
the specified key . |
memc_prepend_by_key(master_key, key, value) | Prepend the specified value to the current value of
the specified key , but uses the
supplied master key to select the server to use. |
memc_append(key, value) | Append the specified value to the current value of
the specified key . |
memc_append_by_key(master_key, key, value) | Append the specified value to the current value of
the specified key , but uses the
supplied master key to select the server to use. |
memc_delete() | Generic delete() |
memc_delete_by_key(master_key, key, value) | Like the generic delete() , but uses the supplied
master key to select the server to use. |
memc_increment() | Generic incr() |
memc_decrement() | Generic decr() |
The respective *_by_key()
functions are
useful when you want to store a specific value into a specific
memcached server, possibly based on a
differently calculated or constructed key.
The memcached
UDFs include some additional
functions:
memc_server_count()
Returns a count of the number of servers in the list of registered servers.
memc_servers_set_behavior(behavior_type, value)
,memc_set_behavior(behavior_type, value)
Set behaviors for the list of servers. These behaviors are identical to those provided by the
libmemcached
library. For more information onlibmemcached
behaviors, see Section 14.5.3.1, “Usinglibmemcached
”.You can use the behavior name as the
behavior_type
:mysql> SELECT memc_servers_behavior_set("MEMCACHED_BEHAVIOR_KETAMA",1);
memc_servers_behavior_get(behavior_type)
,memc_get_behavior(behavior_type, value)
Returns the value for a given behavior.
memc_list_behaviors()
Returns a list of the known behaviors.
memc_list_hash_types()
Returns a list of the supported key-hashing algorithms.
memc_list_distribution_types()
Returns a list of the supported distribution types to be used when selecting a server to use when storing a particular key.
memc_libmemcached_version()
Returns the version of the
libmemcached
library.memc_stats()
Returns the general statistics information from the server.