Skip to content

Instantly share code, notes, and snippets.

Last active January 28, 2020 14:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save finalwebsites/bcbd5cd1203869cb8c4eb3e624dbba77 to your computer and use it in GitHub Desktop.
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();
Copy link

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