The voting for the PSR-16 standard has not even ended yet, and PHPixie already supports it. It would seem that caching is so much already processed area that there is nothing to surprise here, but I hope after reading the article you will find in PHPixie Cache something new and useful for yourself. As always, at the end of the article you will find instructions on how to use Cache without the framework and also information on how to extend the component and help the project.
Immediately you can quickly go through the supported drivers. So far there are only five of them: void , memory , phpfile , memcached and redis .
// /assets/config/cache.php return [ // 'default' => [ // 'driver' => 'void' ], 'second' => [ // 'driver' => 'memory', /* */ /** * , * DateInterval ( P1D 1 ). * null, */ 'defaultExpiry' => 10, /** * 1 1000, * . . * 10, 1% * . */ 'cleanupProbability' => 10 ], 'third' => [ // .php 'driver' => 'phpfile', // /assets/cache 'path' => 'third', /* */ 'defaultExpiry' => null, 'cleanupProbability' => 10 ], 'fourth' => [ // Memcached 'driver' => 'memcached', /** * Memcached::addServers, * , 1211 1 */ 'servers' => [ ['127.0.0.1'] ], /* */ 'defaultExpiry' => null ], 'fifth' => [ // Redis Predis 'driver' => 'redis', // Predis\Client 'connection' => array( 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379 ), /* */ 'defaultExpiry' => null ] ];
Using
As the header says, PHPixie Cache supports PSR-6 and the new simplified PSR-16, this is what the PHPixie \ Cache \ Pool interface looks like with which we will meet often:
namespace PHPixie\Cache; use PHPixie\Cache\Pool\Prefixed; use Psr\Cache\CacheItemPoolInterface; use Psr\SimpleCache\CacheInterface; // PSR-6 PSR-16 interface Pool extends CacheItemPoolInterface, CacheInterface { /** * PSR-6 Item * @param string $key * @param mixed $value * @return Item */ public function createItem($key, $value = null); /** * Pool . * . * @param string $prefix * @return Prefixed */ public function prefixedPool($prefix); }
And now the examples of use:
// . $storage = $cache->storage('second'); // PSR-6 public function getFairies() { $item = $this->storage->getItem('fairies'); if (!$item->isHit()) { $fairies = $this->generateFairies(); $item->set($fairies); $item->expiresAfter(100); $this->storage->save($item); } return $item->get(); } // PSR-16 public function getFairies() { $fairies = $this->storage->get('fairies'); if($fairies === null) { $fairies = $this->buildFairies(); $this->storage->set('fairies', $fairies, 100); } return $fairies; }
But it makes no sense to rewrite the work with these PSRs here as their documentation is already complete, and if you use PHPStorm, then the hints will show you everything.
Now about how PHPixie Cache is different from other libraries.
Prefix pools
When several parts of an application write to the same cache, in order to avoid collisions it is necessary to invent unique prefixes for keys. How often have you written such code:
$key = 'article-'.$id; $article = $cache->get($key);
If the same entities are cached in different parts of the application, you need to ensure that you always use the same prefix or carry this logic into a separate class. In PHPixie Cache, this problem is solved by the prefix pool, which proxies requests to the repository by automatically adding the prefix.
$storage = $cache->storage('default'); $articlesPool = $storage->prefixedPool('article'); $articlesPool->set($article->id, $article->html()); // $storage ->set('article.'.$article->id, $article->html());
As you probably already guessed, $ articlesPool implements the same PHPixie \ Cache \ Pool interface as the repository and it can also be prefixed by creating a hierarchy. This is convenient because in the future, when there will be a lot of articles, such a prefix pool can be replaced with a real separate repository without rewriting the code. Thus, you can permanently save yourself from problems with keys and prefixes.
PHPixie \ Cache itself is also Pool
Most users will most likely have only one cache storage, so why not make their lives easier?
// $cache->storage('default')->get('fairy'); // $cache->get('fairy');
When saving to files, keys are not hashed.
Most libraries generate the file name by making a hash key. This is done in order to avoid problems with the encoding if you decide to set the key with krakozyabrami, but this is not recommended in principle. On the other hand, this hashing takes CPU time, so there is no particular reason to leave it. However, the plans have to add the option to enable this function as a parameter, for those who are critical.
Optimize caching to files
Again, many libraries use serialization as a cache file format. At the same time, the meaning itself and its shelf life are serialized. There are two drawbacks: in order to check the expiration date, it is necessary to read and deserialize the entire file and the fact that deserialization itself is also not cheap for large values. What does PHPixie \ Cache do? Let's see an example of the file created:
<?php /*1483041355*/ return array(1,2,3);
The first line contains the expiration date of the file, so to check its freshness it is enough to read only it and not the entire file. In addition, the data from the file is obtained with the include
statement, and therefore the file code gets into opcache, so receiving data from it several times in a row will not actually read it from disk until it changes. By the way, the old approach with serialization will also be available in the component soon.
$slice = new \PHPixie\Slice(); $filesystem = new \PHPixie\Filesystem(); $config = $slice->arrayData([ 'default' => [ 'driver' => 'memory' ] ]); // /tmp/cache // $root = $filesystem->root('/tmp/cache/'); $cache = new \PHPixie\Cache($config, $root); // , $cache = new \PHPixie\Cache($config);
As you can see there is only one mandatory dependency, so if you are looking for a simple and clear cache, I hope you will like it.
Github: https://github.com/phpixie/cache
After the community added four new providers to the PHPixie Social, I decided it was time to add a small checklist on how to add my driver to the package:
\PHPixie\Cache\Drivers\Type\YourDriver
inheriting from \PHPixie\Cache\Drivers\Driver
.\PHPixie\Cache\Builder::$driverMap
.\PHPixie\Tests\Cache\Driver\YourDriverTest
inheriting from PHPixie\Tests\Cache\DriverTest
and PHPixie\Tests\Cache\DriverTest
test config in it..travis.yml
and composer.json
if there are any new dependencies.If anything, we are always happy to help with any problems in our chat.
Source: https://habr.com/ru/post/318778/
All Articles