Using the MariaDB memcached User-Defined Functions


The memcached MariaDB User-Defined Functions (UDFs) enable you to set and retrieve objects through a SQL interface, in MariaDB 5.0 or greater. This technique has the following benefits:

To install the MariaDB memcached UDFs, download the UDF package from https://launchpad.net/memcached-udfs. 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-1.1.tar.gz
shell> cd memcached_functions_mysql-1.1
shell> # If memcached library is not found, set LDFLAGS=-Llibrary_directory before next command.
shell> ./configure --with-mysql=/usr/local/mysql/bin/mysql_config

Now build and install the functions:

shell> make
shell> make install

Copy the MariaDB memcached UDFs into your MariaDB 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 , "Compiling and Installing User-Defined Functions".

Once installed, you must initialize the function within MariaDB 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';

Repeat this process for each function to provide access to within MySQL. Once you have created the association, the information is retained, even over restarts of the MariaDB server. To simplify the process, use 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 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');
Note

The list of servers used by the memcached UDFs is not persistent over restarts of the MariaDB server. If the MariaDB 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 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:

Retornar