Cache accelerates your application by storing data - once hardly retrieved - for future use. The caching manager focuses on Doctrine Cache for caching. Cache will be supported in the future.
Cache manager utilizes PSR-16 protocols to allow your application to communicate with cache engines. For now let's work with Doctrine Cache.
Please note that this documentation is currently work-in-progress. Feel free to contribute.
The recommended way to install Cache Manager is via Composer:
composer require biurad/biurad-caching
It requires PHP version 7.0 and supports PHP up to 7.4. The dev-master version requires PHP 7.1.
Cache manager offers a very intuitive API for cache manipulation. Before we show you the first examp 8000 le, we need to think about place where to store data physically. We can use a database, Memcached server, or the most available storage - hard drive. So we thought of using Doctrine Cache implementation:
// the `temp` directory will be the storage
$storage = new Doctrine\Common\Cache\ArrayCache();
The Doctrine\Common\Cache\Cache
storage is very well optimized for performance and in the first place,
it provides full atomicity of operations.
What does that mean? When we use cache we can be sure we are not reading a file that is not fully written yet (by another thread) or that the file gets deleted "under our hands". Using the cache is therefore completely safe.
For manipulation with cache, we use the BiuradPHP\Cache\SimpleCache
:
use BiuradPHP\Cache\SimpleCache;
use BiuradPHP\Cache\FastCache as Caching;
$psr = new SimpleCache($storage); // $storage from the previous example
$cache = new Caching($psr);
Let's save the contents of the '$data
' variable under the '$key
' key:
$cache->save($key, $data);
This way, we can read from the cache: (if there is no such item in the cache, the null
value is returned)
$value = $cache->load($key);
if ($value === null) ...
Method get()
has second parameter callable
$fallback
, which is called when there is no such item in the cache. This callback receives the array $dependencies by reference, which you can use for setting expiration rules.
$cache->save($key, function(& $dependencies) {
// some calculation
return 15;
}));
$value = $cache->load($key);
We could delete the item from the cache either by saving null or by calling delete()
method:
$cache->save($key, null);
// or
$cache->remove($key);
It's possible to save any structure to the cache, not only strings. The same applies for keys.
Deleting the cache is a common operation when uploading a new application version to the server. At that moment, however, using Doctrine Cache, the server can handle it's operations. because it has to build a complete new cache. Retrieving some data is not difficult, cause Doctrine Cache create temporary memory data, so you don't run into further errors. If you are experiencing difficulties in caching.
The solution is to modify application behaviour so that data are created only by one thread and others are waiting. To do this, specify the value as a callback or use an anonymous function:
$result = $cache->save($key, function() {
return buildData(); // difficult operation
});
The Cache-manager will ensure that the body of the function will be called only by one thread at once, and other threads will be waiting. If the thread fails for some reason, another gets chance.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
To run the tests you'll have to start the included node based server if any first in a separate terminal window.
With the server running, you can start testing.
vendor/bin/phpunit
If you discover any security related issues, please report using the issue tracker. use our example Issue Report template.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a message on our website, mentioning which of our package(s) you are using.
Post Here: Project Patreons - https://patreons.biurad.com
We publish all received request's on our website;
Biurad Lap
is a technology agency in Accra, Ghana. You'll find an overview of all our open source projects on our website.
Does your business depend on our contributions? Reach out and support us on to build more project's. We want to build over one hundred project's in two years. Support Us achieve our goal.
Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
Thanks to all who made Donations and Pledges to Us.
The BSD-3-Clause . Please see License File for more information.