Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example scripts for PHP caching blog post
require_once __DIR__.'/../vendor/autoload.php';
try {
$cache = new PageCache\PageCache();
} catch (\Exception $e) {
// Log PageCache error or simply do nothing.
// In case of a PageCache error, the page will load normally, without cache.
//The rest of your PHP code, everything below will be cached
$memcaches_obj = new Memcached();
$memcaches_obj->addServer('', 11211);
if (!$value = $memcaches_obj->get('my_object_key')) {
$value = 'Some Value';
// store new values for 1 hour
$memcaches_obj->set('my_object_key', $value, 3600);
try {
$redis = new Redis();
$redis->connect('localhost', 6379);
$redis->setEx('my_object_key', 3600, 'Some Value');
//get value
$value = $redis->get('my_object_key');
} catch (Exception $ex) {
echo $ex->getMessage();
use Phpfastcache\CacheManager;
use Phpfastcache\Drivers\Memcached\Config;
require __DIR__ . '/../../vendor/autoload.php';
$InstanceCache = CacheManager::getInstance('memcached', new Config([
'host' =>'',
'port' => 11211
$CachedString = $InstanceCache->getItem('my_object_key');
if (is_null($CachedString->get())) {
$CachedString->set('Some Value')->expiresAfter(60); // minutes
echo $CachedString->get();
} else {
echo $CachedString->get();

This comment has been minimized.

Copy link
Owner Author

@finalwebsites finalwebsites commented Jan 28, 2020

If you like to read my article where this examples are used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment