PHP code example of code-corner / performance-cache
1. Go to this page and download the library: Download code-corner/performance-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/ */
code-corner / performance-cache example snippets
use CodeCorner\PerformanceCache\Cache;
use CodeCorner\PerformanceCache\FileCacheHandler;
// Initialize cache with default handler (FileCacheHandler)
$cache = new Cache();
use CodeCorner\PerformanceCache\Cache;
use App\CustomCacheHandler; // Replace with your custom cache handler
// Initialize cache with custom handler
$customHandler = new CustomCacheHandler();
$cache = new Cache($customHandler);
$key = 'my_key';
$value = 'my_value';
$ttl = 3600; // Optional TTL (time-to-live) in seconds
if ($cache->set($key, $value, $ttl)) {
echo "Value successfully cached!\n";
} else {
echo "Failed to cache the value.\n";
}
$key = 'my_key';
$defaultValue = 'default_value'; // Optional default value if key not found
$cachedValue = $cache->get($key, $defaultValue);
echo "Cached Value: $cachedValue\n";
$key = 'my_key';
if ($cache->delete($key)) {
echo "Cache entry successfully deleted!\n";
} else {
echo "Failed to delete the cache entry.\n";
}
if ($cache->clear()) {
echo "Cache cleared successfully!\n";
} else {
echo "Failed to clear the cache.\n";
}
$keys = ['key1', 'key2', 'key3'];
$defaultValue = 'default_value'; // Optional default value if any key is not found
$cachedValues = $cache->getMultiple($keys, $defaultValue);
foreach ($cachedValues as $key => $value) {
echo "Key: $key, Value: $value\n";
}