Memcached

Learn more about the component and how to use it.

The Memcached component allows you to perform operations on Memcached servers.

Memcached is an open source distributed memory caching system that uses an in-memory key-value model for data storage. For more information, see the official website.

Parameters

Take a look at the configuration parameters of the component. Parameters supported by Double Braces expressions are marked with (DB).

Important: when using multiple servers, the same Basic credential must be used through all servers.

Usage examples

Retrieval operations

Get One

Retrieves only 1 record from the server by its Key.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Get One

  • Key: id_A

Output:

{
    "total": 1,
    "data": [
        {
        	"key": "id_A",
        	"value": <value>
        }
    ]
}

Get Many

Retrieve any number of records from the server by a Key Set.

  • Account: <basic-account>

  • Address: localhost:11211

  • Operation: Get Many

  • Key Set:

[
	"id_A",
	"id_B",
	"id_C",
	"id_D"
]

Output:

{
    "total": 4,
    "data": [
        {
        	"key": "id_A",
        	"value": <value>
        },
        {
        	"key": "id_B",
        	"value": <value>
        },
        {
        	"key": "id_C",
        	"value": <value>
        },
        {
        	"key": "id_D",
        	"value": <value>
        }
    ]
}

Important: when retrieving data from a Memcached server, the component can only handle Java serializable objects and will always try to output them in JSON format or its string representation. If the retrieved value cannot be parsed into these formats, an Exception can be thrown.

Storage operations

Add

Stores a record on the server by its Key, Expiration and Value, only if it does not already exist.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Add

  • Key: id_A

  • Expiration: 3600

  • Value:

{
    "code": "A",
    "name": "John",
    "address": {
    	"street": "John's Street"
    },
    "number": [
    	"+5511999999999"
    ]
}

Output:

{
    "success": true,   //false, if the key already exists
    "command": "add"
}

Set

Stores a record on the server by its Key, Expiration and Value, overriding it if it already exists.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Set

  • Key: id_A

  • Expiration: 3600

  • Value:

{
    "code": "A",
    "name": "John",
    "address": {
    	"street": "John's Street"
    },
    "number": [
    	"+5511999999999"
    ]
}

Output:

{
    "success": true,
    "command": "set"
}

Replace

Replaces a record's Value by its Key, Expiration and the new Value, only if the record already exists.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Replace

  • Key: id_A

  • Expiration: 3600

  • Value:

{
    "code": "A",
    "name": "John",
    "address": {
    	"street": "John's Street"
    },
    "number": [
    	"+5511999999999"
    ]
}

Output:

{
    "success": true,   //false, if the key does not exist
    "command": "replace"
}

Append

Appends a value to a record's Value by its Key and the Value to be appended, only if the record already exists. Requires Raw Mode parameter to be enabled.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Append

  • Key: id_A

  • Expiration: 3600

  • Raw Mode: enabled

  • Value:

" - value to be appended"

Output:

{
    "success": true,   //false, if the key does not exist
    "command": "append"
}

Important: since the Append operation appends new data to existing data without retrieving it, it is not possible to simply append complex data structures such as JSON objects. Therefore, the existing data must also have been stored as raw data, and the Raw Mode parameter must be enabled for this operation.

Prepend

Prepends a value to a record's Value by its Key and the Value to be prepended, only if the record alright exists. Requires Raw Mode parameter to be enabled.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Prepend

  • Key: id_A

  • Expiration: 3600

  • Raw Mode: enabled

  • Value:

"value to be prepended - "

Output:

{
    "success": true,   //false, if the key does not exist
    "command": "prepend"
}

Important: since the Prepend operation prepends new data to existing data without retrieving it, it is not possible to simply prepend complex data structures such as JSON objects. Therefore, the existing data must also have been stored as raw data, and the Raw Mode parameter must be enabled for this operation.

CAS (Check And Set)

Update a record's Value by its Key, Expiration and the new Value, only if the record alright exists and if it wasn't changed during the record's retrieval. This operation is useful in environments with race conditions on updating cache data.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: CAS (Check And Set)

  • Key: id_A

  • Expiration: 3600

  • Value:

{
    "code": "A",
    "name": "John",
    "address": {
    	"street": "John's Street"
    },
    "number": [
    	"+5511999999999"
    ]
}

Output:

{
    "success": true,   //false, if the key does not exist
    "command": "cas"
}

Delete operations

Delete

Delete a record from the server by its Key.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Delete

  • Key: id_A

Output:

{
    "success": true,    //false, if the key does not exist
    "command": "delete"
}

Touch operations

Touch

Updates a record's expiration by its Key and a new Expiration value.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Touch

  • Key: id_A

  • Expiration: 3600

Output:

{
    "success": true,    //false, if the key does not exist
    "command": "touch"
}

Get And Touch

Retrieves only 1 record from the server by its Key while updating its Expiration value.

  • Account: <basic-account>

  • Addresses: localhost:11211

  • Operation: Get And Touch

  • Key: id_A

  • Expiration: 3600

Output:

{
    "total": 1,
    "data": [
        {
        	"key": "id_A",
        	"value": <value>
        }
    ]
}

Important: when retrieving data from a Memcached server, the component can only handle Java serializable objects and will always try to output them in JSON format or its string representation. If the retrieved value cannot be parsed into these formats, an Exception can be thrown.

Last updated