📜 ⬆️ ⬇️

OnPHP caching systems overview

Good day!
In this article we will describe how we work with the cache in plus1.wapstart.ru , what problems we had and how we solved some special cases.

To begin with about terminology.


By “cache” in this article, I will understand some kind of fast storage, which can be used, including, for caching. At the same time, the storage should have a standardized interface.
Server / storage is any application that can store data and give access to it via the interface described below. For example, this application can be memcached.

We use the onPHP framework. It has the abstract class CachePeer, from which all cache implementations should inherit. The interface of any implementation is reduced to the following methods.
')
abstract public function get($key); abstract public function delete($key); abstract public function increment($key, $value); abstract public function decrement($key, $value); abstract protected function store( $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM ); abstract public function append($key, $data); 

In our world there are the following implementations of CachePeer (clickable)



This diagram shows both the implementations of the storages (see the bridge ) and the various decorators who solve particular problems.

Onphp has support for working with Redis ; Memcached - as many as two implementations: on sockets and using Memcache ( http://php.net/Memcache ); we can work with SharedMemory . If there is none of this in the installation, then we will work with the application's memory .
In spite of all the variety of technologies supported, I don’t know a single onphp project that would use something other than Memcached.
Memcached rules this world. :)

We have two Memcache implementations for the following reasons:

We almost everywhere use PeclMemcached, which connects to the server through the Memcache extension. Other things being equal, it works faster and also supports pconnect .
With the alternative library ( MemcacheD ) we somehow did not work out. I tried to write an implementation for it, but at that time (about two years ago) it was not very stable.

About decorators:


When the ratio of projects per caching system becomes more than one, then WatermarkedPeer should be used. Its meaning comes down to the getActualWatermark () method. For example, the get implementation becomes
  public function get($key) { return $this->peer->get($this->getActualWatermark().$key); } 

This avoids key conflicts. Data from different projects / classes / etc. will be recorded under different keys. As for the rest, this cache is a standard decorator for any storage implementation.

If you need to spread data across multiple caching systems, then you can either use the memkesh cluster from the php delivery or take one of our aggregate caches. We have several:

At this more or less standard part ends. Most applications should have this set of implementations to create a normal caching system.

Then begin the particular.



Since Since almost all implementations use the decorator pattern, they can be quite successfully combined.
For example, the following construction is acceptable:

  $swordfish = ReadOnlyPeer::create( new SequentialCache( PeclMemcached::create('localhost', 9898, 0.1), array( PeclMemcached::create('backup', 9898, 0.1), ) ) ); 


Or even this:

  $swordfish = CascadeCache::create( PeclMemcached::create('unix:///var/run/memcached_sock/memcached.sock', 0), ReadOnlyPeer::create( new SequentialCache( PeclMemcached::create('localhost', 9898, 0.1), array( PeclMemcached::create('backup', 9898, 0.1), ) ) ), CascadeCache::NEGATIVE_CACHE_OFF ); 


In this case, the data will first be searched in the local memkesh, accessible via unix-socket, if they are not there, then the “memkesh” localhost: 9898 will be requested. And in case it is unavailable, then backup: 9898. At the same time, the application knows that from the caches on ports 9898 you can only read, but not write.

This does not end the onphp caches. You can make completely different configurations that will cover your tasks. CachePeer from onphp is awesome.
ps. Once upon a time here talked about a series of articles about onphp. A start was made by this post. In the future, we will touch on other topics related to the framework and its use in plus1.wapstart.ru.

pps. Taking this opportunity, I inform you that we are looking for people:
hantim.ru/jobs/11163-veduschiy-qa-menedzher-rukovoditel-otdela-testirovaniya
hantim.ru/jobs/11111-veduschiy-php-razrabotchik-team-leader

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


All Articles