1. Go to this page and download the library: Download oopress/caching 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/ */
oopress / caching example snippets
// Connect to Redis server (replace with your own configuration)
$redisCache = new Oopress\Cache\RedisCache('localhost', 6379);
// Set a value in the cache with a 10-minute TTL
$redisCache->set('my_data', 'This is cached data!', 600);
// Retrieve the cached value
$cachedData = $redisCache->get('my_data');
if ($cachedData !== false) {
echo "Retrieved data from cache: " . $cachedData;
} else {
// Data not found in cache, fetch it from your application logic
// ...
}
// Delete a key from the cache
$redisCache->delete('my_data');
// Check if a key exists in the cache
$exists = $redisCache->has('my_data');
// Clear the entire cache
$redisCache->clear();
// Store multiple values with different TTLs
$values = [
'key1' => 'value1',
'key2' => 'value2',
];
$redisCache->setMultiple($values, [
'key1' => 300, // 5 minutes
'key2' => null, // No expiration
]);
use Oopress\Cache\MySqlCache;
// Create a new MySQL cache instance
$mysqlCache = new MySqlCache(3600, 'caching_table'); // Default TTL of 1 hour, and table name defaults to chached_requests
// Set a value in the cache
$mysqlCache->set('my_key', 'This is my value');
// Retrieve the value
$value = $mysqlCache->get('my_key');
// Delete a value
$mysqlCache->delete('my_key');
// Clear the entire cache
$mysqlCache->clear();
// Instantiate a FileCache instance
$cacheDir = '/path/to/cache/directory';
$cache = new FileCache($cacheDir);
// Cache a post query for 1 hour
$expireTime = new DateTime('+1 hour');
$cachedPostQuery = withCachedQuery($cache, $expireTime, 'post');
// Use the cached query
$posts = $cachedPostQuery([
'post_type' => 'post',
'posts_per_page' => 10,
]);
// Cache a custom function for 5 minutes
$expireTime = new DateTime('+5 minutes');
$cachedFunction = withCache($cache, $expireTime);
// Use the cached function
$result = $cachedFunction(function () {
// Some expensive operation
return slow_calculation();
});
// Cache a static method for 1 day
$expireTime = new DateTime('+1 day');
$cachedStaticMethod = withCache($cache, $expireTime);
// Use the cached static method
$result = $cachedStaticMethod([MyClass::class, 'staticMethod'], 'arg1', 'arg2');
// Cache a custom function for 5 minutes
$expireTime = new DateTime('+5 minutes');
$result = withCache($cache, $expireTime)(function () {
return 'Hello, world!';
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.