1. Go to this page and download the library: Download hejunjie/cache library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hejunjie / cache example snippets
use Hejunjie\Cache;
// Create a cache structure: Memory -> Redis -> File -> Database
$cache = new Cache\MemoryCache(
new Cache\RedisCache(
new Cache\FileCache(
new MyDataSource(), // A data source that implements the DataSourceInterface.
'[File] Cache folder path',
'[File] Cache duration (seconds)'
),
'[Redis] Configuration'
'[Redis] Prefix'
'[Redis] Persistent connection'
),
'[Memory] Cache duration (seconds)',
'[Memory] Cache quantity (to prevent memory overflow)'
);
$data = $cache->get('user:123'); // Automatically performs a layer-by-layer lookup, with cache misses propagating down to the underlying data source.
// Custom Data Sources - database
class MyDataSource implements \Hejunjie\Tools\Cache\Interfaces\DataSourceInterface
{
protected DataSourceInterface $wrapped;
// Constructor, no need for a constructor if it's the last layer.
// public function __construct(
// DataSourceInterface $wrapped
// ) {
// $this->wrapped = $wrapped;
// }
public function get(string $key): ?string
{
// Get the corresponding content from the database based on the key.
// Return the content as a string string.
// If the next layer returns data, store it in the current layer. If it's the last layer, the following code is not needed.
// $content = $this->wrapped->get($key);
// if ($content !== null) {
// $this->set($key, $content);
// }
// return $content;
}
public function set(string $key, string $value): bool
{
// Store the value in the database based on the key.
// Return the storage result as `bool`.
}
public function del(string $key, string $value): void
{
// Perform a delete operation based on the key.
// No need to return any value.
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.