📜 ⬆️ ⬇️

Memcached tags in PHP in haste, without frameworks

Foreword


Today, such a tool as memcached, is almost 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 habraeffekt or similar invasions of visitors.

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

The only thing BUT - this style has become a factory for the production of bicycles, and today I would like to share with you one of them. Namely: a simple class for working with memcached that supports tag functionality, or as they call their friend (rarely) - namespaces.

')
I am attracted to linear programming by the fact that its execution speed is maximum, and the load is not necessary for me with things like routing, automatic search for files with the class I need, etc. completely absent, because I already know where it is, and I do not need tens and hundreds of things that will gather dust somewhere in a distant folder.

Closer to the point


The task was simple - the class should be easy to use, and adding a key to the cache as well as to a specific tag should occur with a single call to the set function of the 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 the backslash has become 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 the key of the news * format, we simply execute:

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

Pay attention to the asterisk at the end of the key, it is she who makes it clear to the delete method that we need to delete everything from the news tag , otherwise the method will try to delete the news key .
If you need to remove any particular key, and information about it from the tag, we write the same as written in set:

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

Multiple deletion is also possible by passing several parameters:

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

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

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

or

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

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

Code itself


The code is quite voluminous ... placed on the first surs-hosting ...
The class code is here.

Possible improvements

There are ideas that you can change slightly ... it is to add support to more than one server, and some debugging and error handling ...

Where used

In two projects, one for work, another for a 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.

Source: https://habr.com/ru/post/113572/


All Articles