Memcached tags in PHP in a hurry, without frameworks

Foreword


Today, such an instrument as memcached is practically indispensable in the work of large projects. It eases the load on the server both on ordinary days and at times when the site is exposed to a habraeffect or similar invasion of visitors.

For a long time I had to write completely linear code without using OOP, with the exception of classes for work with MySQL, Memcached, and sessions. All other classes consisted only of static functions, which essentially only replaced, say, member_get_id () with Member :: get_id () ;.

The only BUT - this style has turned into a bicycle factory, and today I would like to share one of them with you. Namely: a simple class for working with memcached that supports the functionality of tags, or, as their friend is called (rarely) - namespace.


Linear programming attracts me by the fact that its execution speed is maximum, and the load is loaded with such unnecessary things as rooting, automatic search for files with the class I need, etc. completely absent, because I already know where it is, and I do not need dozens and hundreds of things that will gather dust somewhere in a distant folder.

Get to the point


The task was simple - the class should be easy to use, and adding the key to the cache and to a specific tag should be done with one call to the set function of this class. I implemented the functional in this way:

$mcache->set('news\p1', $data, $ttl);

This code adds the news_p1 key to the cache (note that backslash has turned into an underscore), and also adds the same key name to the ns_news tag (the ns_ prefix is ​​added automatically). In the future, if after editing or deleting a record you need to delete all cached data with a key in the news * format

$mcache->delete('news*');

key, we simply do this: Pay attention to the asterisk at the end of the key, it makes it clear to the delete method that we need to delete everything from the news tag , in Otherwise, the method will try to delete the news key .
If you need to delete a specific key, and information about it from the tag, we write in the same way as they wrote in set:

$mcache->delete('news\p1');

Multiple deletion by passing several parameters is also possible:

$mcache->delete('news*', 'members*', 'categories', 'comments\p1');

And so that everything would be in the same style, we also write in the get method:

$mcache->get('news*');

or

$mcache->get('news\p1');

It is important to know: The first option, where we request news *, will not return all the data from the keys that are contained in the news tag, but only a list of the keys themselves.

Code itself


The code is quite voluminous ... placed on the first available surfing hosting ... the
class code is here

Possible improvements

There are ideas that can be changed slightly ... this is to add support for more than one server, and some debugging and error handling ...

Where used

In two projects, one for work, another hobby ... or rather socialz.net , I will write about its technical details soon, when I launch v2 which is quite interesting, but still in the testing phase.

Also popular now: