---
name: "memcached_callback"
section: "3"
platform: "linux"
package: "libmemcached-awesome"
version: "1.1.4-4"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/memcached_callback"
---

# memcached\_callback(3)

libmemcached Documentation

## NAME

memcached\_callback - libmemcached Documentation

## SYNOPSIS

- **#include \<libmemcached/memcached.h\>**

  Compile and link with -lmemcached

- **memcached\_return\_t \<#\_CPPv418memcached\_return\_t\> memcached\_callback\_set(memcached\_st \<#\_CPPv412memcached\_st\> \*ptr, memcached\_callback\_t flag, const void \*data)**

  - **Parameters**

    - •

      **ptr** -- pointer to initialized **memcached\_st** \<#\_CPPv412memcached\_st\> struct

    - •

      **flag** -- **memcached\_callback\_t** flag to set

    - •

      **data** -- data corresponding to **flag**

  - **Returns**

    **memcached\_return\_t** \<#\_CPPv418memcached\_return\_t\> indicating success

- **void \*memcached\_callback\_get(memcached\_st \<#\_CPPv412memcached\_st\> \*ptr, memcached\_callback\_t flag, memcached\_return\_t \<#\_CPPv418memcached\_return\_t\> \*error)**

  - **Parameters**

    - •

      **ptr** -- pointer to initialized **memcached\_st** \<#\_CPPv412memcached\_st\> struct

    - •

      **flag** -- **memcached\_callback\_t** flag to query

    - •

      **error** -- pointer to **memcached\_return\_t** \<#\_CPPv418memcached\_return\_t\> indicating success

  - **Returns**

    the **data** previously set

- **typedef enum memcached\_callback\_t memcached\_callback\_t**

  - **enum memcached\_callback\_t**

  - **enumerator MEMCACHED\_CALLBACK\_CLEANUP\_FUNCTION**

    When **memcached\_free()** \<#\_CPPv414memcached\_freeP12memcached\_st\> or **memcached\_reset** is called this function will be executed. At the point of its execution all connections are closed.

    Its signature is:

    - **typedef memcached\_return\_t \<#\_CPPv418memcached\_return\_t\> (\*memcached\_cleanup\_fn)(const memcached\_st \<#\_CPPv412memcached\_st\> \*ptr)**

      - **enumerator MEMCACHED\_CALLBACK\_CLONE\_FUNCTION**

    When **memcached\_clone()** \<#\_CPPv415memcached\_cloneP12memcached\_stP12memcached\_st\> is called this function will be executed.

    Its signature is:

    - **typedef memcached\_return\_t \<#\_CPPv418memcached\_return\_t\> (\*memcached\_clone\_fn)(memcached\_st \<#\_CPPv412memcached\_st\> \*destination, const memcached\_st \<#\_CPPv412memcached\_st\> \*source)**

      - **enumerator MEMCACHED\_CALLBACK\_PREFIX\_KEY**

    See **MEMCACHED\_CALLBACK\_NAMESPACE**.

  - **enumerator MEMCACHED\_CALLBACK\_NAMESPACE**

    You can set a value which will be used to create a domain for your keys. The value specified here will be prefixed to each of your keys. The value can not be greater then **MEMCACHED\_MAX\_NAMESPACE** \<#c.MEMCACHED\_MAX\_NAMESPACE\> - 1 and will reduce **MEMCACHED\_MAX\_KEY** \<#c.MEMCACHED\_MAX\_KEY\> by the value of your key.

    The prefix key is only applied to the primary key, not the master key. **MEMCACHED\_FAILURE** \<#\_CPPv4N18memcached\_return\_t17MEMCACHED\_FAILUREE\> will be returned if no key is set. In the case of a key which is too long, **MEMCACHED\_BAD\_KEY\_PROVIDED** \<#\_CPPv4N18memcached\_return\_t26MEMCACHED\_BAD\_KEY\_PROVIDEDE\> will be returned.

    If you set a value with the value being NULL then the prefix key is disabled.

  - **enumerator MEMCACHED\_CALLBACK\_USER\_DATA**

    This allows you to store a pointer to a specific piece of data. This can be retrieved from inside of **memcached\_fetch\_execute()** \<#\_CPPv423memcached\_fetch\_executeP12memcached\_stP20memcached\_execute\_fnPv8uint32\_t\>. Cloning a **memcached\_st** \<#\_CPPv412memcached\_st\> will copy the pointer to the clone.

  - **enumerator MEMCACHED\_CALLBACK\_MALLOC\_FUNCTION**

    Deprecated since version \<0.32: Use **memcached\_set\_memory\_allocators()** \<#\_CPPv431memcached\_set\_memory\_allocatorsP12memcached\_st19memcached\_malloc\_fn17memcached\_free\_fn20memcached\_realloc\_fn19memcached\_calloc\_fnPv\> instead.

  - **enumerator MEMCACHED\_CALLBACK\_REALLOC\_FUNCTION**

    Deprecated since version \<0.32: Use **memcached\_set\_memory\_allocators()** \<#\_CPPv431memcached\_set\_memory\_allocatorsP12memcached\_st19memcached\_malloc\_fn17memcached\_free\_fn20memcached\_realloc\_fn19memcached\_calloc\_fnPv\> instead.

  - **enumerator MEMCACHED\_CALLBACK\_FREE\_FUNCTION**

    Deprecated since version \<0.32: Use **memcached\_set\_memory\_allocators()** \<#\_CPPv431memcached\_set\_memory\_allocatorsP12memcached\_st19memcached\_malloc\_fn17memcached\_free\_fn20memcached\_realloc\_fn19memcached\_calloc\_fnPv\> instead.

  - **enumerator MEMCACHED\_CALLBACK\_GET\_FAILURE**

    This function implements the read through cache behavior. On failure of retrieval this callback will be called.

    You are responsible for populating the result object provided. This result object will then be stored in the server and returned to the calling process.

    You must clone the **memcached\_st** \<#\_CPPv412memcached\_st\> in order to make use of it. The value will be stored only if you return **MEMCACHED\_SUCCESS** \<#\_CPPv4N18memcached\_return\_t17MEMCACHED\_SUCCESSE\> or **MEMCACHED\_BUFFERED** \<#\_CPPv4N18memcached\_return\_t18MEMCACHED\_BUFFEREDE\>. Returning **MEMCACHED\_BUFFERED** \<#\_CPPv4N18memcached\_return\_t18MEMCACHED\_BUFFEREDE\> will cause the object to be buffered and not sent immediately (if this is the default behavior based on your connection setup this will happen automatically).

    The prototype for this is:

    - **typedef memcached\_return\_t \<#\_CPPv418memcached\_return\_t\> (\*memcached\_trigger\_key)(memcached\_st \<#\_CPPv412memcached\_st\> \*ptr, char \*key, size\_t key\_length, memcached\_result\_st \<#\_CPPv419memcached\_result\_st\> \*result)**

      - **enumerator MEMCACHED\_CALLBACK\_DELETE\_TRIGGER**

    This function implements a trigger upon successful deletion of a key. The **memcached\_st** \<#\_CPPv412memcached\_st\> structure will need to be cloned in order to make use of it.

    The prototype for this is:

    - **typedef memcached\_return\_t \<#\_CPPv418memcached\_return\_t\> (\*memcached\_trigger\_delete\_key)(memcached\_st \<#\_CPPv412memcached\_st\> \*ptr, char \*key, size\_t key\_length)**

      ## DESCRIPTION

**libmemcached** can have callbacks set key execution points. These either provide function calls at points in the code, or return pointers to structures for particular usages.

**memcached\_callback\_get()** takes a callback flag and returns the structure or function set by **memcached\_callback\_set()**.

**memcached\_callback\_set()** changes the function/structure assigned by a callback flag. No connections are reset.

You can use **MEMCACHED\_CALLBACK\_USER\_DATA** to provide custom context if required for any of the callbacks.

## RETURN VALUE

**memcached\_callback\_get()** returns the function or structure that was provided. Upon error, nothing is set, NULL is returned, and the **memcached\_return\_t** \<#\_CPPv418memcached\_return\_t\> argument is set to **MEMCACHED\_FAILURE** \<#\_CPPv4N18memcached\_return\_t17MEMCACHED\_FAILUREE\>.

**memcached\_callback\_set()** returns **MEMCACHED\_SUCCESS** \<#\_CPPv4N18memcached\_return\_t17MEMCACHED\_SUCCESSE\> upon successful setting, otherwise **MEMCACHED\_FAILURE** \<#\_CPPv4N18memcached\_return\_t17MEMCACHED\_FAILUREE\> on error.

## SEE ALSO

**[memcached(1)](/memcached/1.md) \<https://linux.die.net/man/1/memcached\>** **[libmemcached(3)](/libmemcached/3.md) \<https://linux.die.net/man/3/libmemcached\>** **[memcached\_strerror(3)](/memcached_strerror/3.md) \<https://linux.die.net/man/3/memcached\_strerror\>**
