2

I am beginning to add a memcached layer to my application. so far so good.

I realised quite quickly however that I will be needing to invalidate/delete large batches of keys in one go when someone uploads a file to the site in order to keep the data relevant.

I have done some reading and the most common way of getting around this problem seems to be setting a version number on each key, then rather than deleting the keys when a user uploads (because there could be so many permutations) you incrememnt the version number, inducing a cache miss next time the data is accessed.

I have no idea where to begin in order to get this coded and i'm not quite sure I totally get my head around it anyway.

My code currently looks like this:-

$memcache = new Memcache;


$memcache->connect('localhost', 11211) or die ("Could not connect");


$key = md5("songsearch_$textSearch.$rangeSearch");

The two variables in the key var above are retrieved from the get request which in turn retrieves a large amount of JSON. (Think a product catalogue).

These variables also determine the query itself which is dynamically put together dependant upon these variables. Ultimately all of this gives me a key that is unique to every individual query, even though from the same script I could have hundreds of keys being generated.

I hope the above is clear, if not please ask me to clarify any points in order to better help you answer my question.

Obviously later, to set the cache the results I am using this:-

$memcache->set($key, $output, MEMCACHE_COMPRESSED, time() + 24*60*60*365);

just before I encode the JSON.

So my question really is... How can I add some form of incremental versioning to this so that if a user uploads a file I can invalidate the whole lot of keys that have been generated by this script?

Huge thanks to anyone who gets me at least on the right track.

gordyr
  • 6,078
  • 14
  • 65
  • 123
  • Sucks that memcache won't let you delete on wildcards. Would be so easy to just flush all cache entries matching some constant key prefix; `cache_key_related_to_group_of_values*` – Mike Purcell Dec 08 '11 at 23:23
  • I couldn't agree more... Would make it far more powerful/easier to work with. :-( – gordyr Dec 08 '11 at 23:25

2 Answers2

10

You are obviously on the right track. The only thing you're missing: Store a version number in the memcache, which you retrieve before you build your key.

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

// fetch the current version number
$version = $memcache->get('version_number');

// and add the version to the key
$key = md5("songsearch_$textSearch.$rangeSearch") . 'v' . $version;

So now, whenever somebody uploads new content, you simply increment the version number:

$memcache->increment('version_number');

This automatically leads to all existing queries to run into invalid entries.

To simplify the access, I recommend you wrap this in a new Memcache handler class (untested):

class VersionedMemcache extends Memcache {

    const VERSION_KEY = 'version_number';

    private $version = 1;

    public function __construct() {
        $version = parent :: get(self :: VERSION_KEY);
        if ($version === false) {
            $version = 1;
            $this->set(self :: VERSION_KEY, $version);
        }
        $this->version = $version;
    }

    public function get($key, &$flags = null) {
        $key = $key . '_v' . $this->version;
        return parent :: get($key, $flags);
    }

    public function incVersion() {
        $this->version = $this->increment(self :: VERSION_KEY);
        return $this->version;
    }

}

So now, you would simply amend your script to:

$memcache = new VersionedMemcache();

$memcache->connect('localhost', 11211) or die ("Could not connect");

$key = md5("songsearch_$textSearch.$rangeSearch");

// this will now result in a fetch on 'ab23...232afe_v1' instead of 'ab23...232afe'
$result = $memcache->get($key);

// when an upload happens, simply
$memcache->incVersion();
Dan Soap
  • 10,114
  • 1
  • 40
  • 49
  • 1
    Absolutely fantastic answer mate. You covered everything covered in detail with some great example code and excellent descriptions. Huge thanks! – gordyr Dec 09 '11 at 00:07
  • wow, i was just browsing some interesting problems on so, this is by far the most interesting/powerful answer, i've run into in a while, i learned a lot! – Landon Dec 09 '11 at 22:58
0

You could save an array and avoid keys hell.

mac
  • 42,153
  • 26
  • 121
  • 131
sathia
  • 2,192
  • 2
  • 24
  • 42
  • Thanks for the suggestion sathia. I'm pretty new to working with memcached so I wonder if you could elaborate on your answer a little more. I'm not quite sure how this would help or how to implement it. Thanks! – gordyr Dec 08 '11 at 23:26
  • 1
    Hi, I was just suggesting that you could use a single key and save on it an array of key => values, in that case you could invalidate a single key and it would reflect to all the keys/values inside it. i see much less overhead this way, and it's transparent (it's all handled by the memcache library for php) – sathia Dec 09 '11 at 10:14
  • 1
    Actually thats a very good idea! I have Cassy's solution in place and working right now, and the quality of his answer was excellent. But this is a great tip and will certainly be a method I will be using. Much appreciated. – gordyr Dec 09 '11 at 11:37