PHP code example of phpfastcache / phpfastcache-bundle
1. Go to this page and download the library: Download phpfastcache/phpfastcache-bundle 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/ */
phpfastcache / phpfastcache-bundle example snippets
public function indexAction(Request $request, Phpfastcache $phpfastcache)
{
$cache = $phpfastcache->get('filecache');
$item = $cache->getItem('myAppData');
if (!$item->isHit() || $item->get() === null) {
$item->set('Wy app has now superpowers !!')->expiresAfter(3600);//1 hour
$cache->save($item);
}
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'myAppData' => $item->get(),
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
]);
}
/**
* @Route("/cached", name="cached")
*/
public function cachedAction(Phpfastcache $phpfastcache, Request $request): Response
{
return (new CacheableResponse($phpfastcache->get('filecache'), $request))->getResponse('cache_key', 3600, function () {
return new Response('Random bytes: ' . \random_bytes(255));
});
}