Implementation of Memcached access for Lasso 9
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Memcached for Lasso 9

Intuitive Memcached client for Lasso 9 — supports single / multiple servers.

Written by Ke Carlton, Zeroloop

Published with permission by LassoSoft Inc

What is Memcached?

Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. More information about Memcached can be found online

Lasso Memcached Client

The Lasso 9 Memcached client supports multiple Memcached servers, automatic server redundancy + fail over, native object storage + retrieval — and is simple to use.

Getting started

It’s best to run Memcached on a dedicated virtual machine as most of the available memory will be used by Memcached. Depending on throughput, a VM with1GB memory should be a good starting point.

Install Memcached

Download and install memcached for your appropriate platform:

Ubuntu / Redhat / Centos:


Configure Lasso

To configure Lasso for Memcached simply specify your Memcached server(s). These settings are unique to each Lasso Instance:

memcached_server = ''


memcached_servers = array(

Test your Memcached servers:


Simple syntax

Once your server test returns online for all servers you can start to use Memcached. The syntax is very simple and utilises Lasso’s blocks:

memcached('string_example') => 'I was cached on [date].'
memcached('include_example') => file('/file/to/')
memcached('capture_example') => { return array(1,2,3) }

Supported blocks

The given block should be a string, file or capture. Captures should return the value to store when they are invoked. Strings and files are compiled and invoked as Lasso source code. Each block is only invoked if a valid key is not present in the cache.

Supported types

The core Lasso types are automatically encoded and decoded when they are stored and retrieved from Memcached. The types supported are: null, integer, decimal, string, bytes, pair, array and map.

Graceful mode

By default the Memcached client runs in a graceful mode — this means that errors are suppressed and the cached will be skipped if no Memcached servers are unavailable or similar. During development you may find it useful to disable graceful mode:

memcached->graceful = false


You can specify an expiration value in seconds or as a date object.

memcached('expire_60_seconds',60) => 'I was cached on [date].'
memcached('expire_tomorrow',date) => 'I was cached on [date].'

You can also force memcached to refresh the cached item by specify a boolean parameter.

memcached('force_refresh',true) => 'I was cached on [date].'


memcached('force_refresh',60,true) => 'I was cached on [date].'

Real world examples

The simplest way of handling expiration is to use a revision value as part of the key. In the below example the CMS system sets a variable $revision that represents the last date/time the sites content was changed.

memcached('mainmenu_' + $revision) => file('/includes/')

The following example caches all of a users friends for 10 minutes.

memcached('friends_' + user_id,600) => { return current_user->friends }

This example caches a view of a user’s cart. The values of user_id and cart_version are stored in the user session. When the user modifies their cart, cart_version is updated and the view is re-cached.

memcached('usercart_' + user_id + '_' + cart_version) => file(

Old items simply drop out of memcached as newer items take the space. Working with unique keys is the simplest way of managing the cache — it’s much easier than trying to manually expire items.

Working directly with Memcached

The Lasso client for Memcached also supports all the standard memcached calls — each command is documented at the end of this document. They can be access like so:


The memcached client can also be used locally:

local(memcached) = memcached(array(''))

Standard Commands

The Lasso memcached client also supports all standard Memcached commands. These can be utilized if you'd like to work with Memcached directly.


set(key::string,value::any,expires::integer=0)::boolean set(key::string,value::any,expires::date)::boolean

Store the specified value:

memcached->set('mykey','my val)


get(key::string)::any get(keys::array,asmap::boolean=false)::map

Retrieve the specified key(s) from the server.

Returns either the value or a map containing the results if multiple keys are specified:




Append to value to an existing key.




Prepend to value to an existing key.


cas (check and set)


Store the specified value only if it hasn't change since last retrieved. Use the cas parameter provided by gets(array,true) to make this call.

memcached->cas('mykey','new value',12345)



The command "delete" allows for explicit deletion of items




Increment the specified key by the value — key must exist. Returns new integer value.




Decrement the specified key by the value — key must exist. Returns new integer value.



touch(key::string,expires::integer)::boolean touch(key::string,expires::date)::boolean

Reset the expiration time on a key — key must exist. Supported by Memcached version 1.4.8 and greater. Returns boolean



flush::boolean flush(expires::integer)::boolean flush(expires::date)::boolean

Flush the cach of all key or keys due to expire after specific value. Returns boolean.



Returns the version of the first specified server.


Returns a map contain the version of each server.


Written by Ke Carlton, Zeroloop

Published with permission by LassoSoft Inc.