Skip to content

Real world examples

Jonathan Guthrie edited this page Sep 17, 2013 · 1 revision

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/mainmenu.inc')

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(
    '/includes/smallcart.inc'
)

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.

Clone this wiki locally