Symfony 3.4 Cache Example

Symfony has a very powerful built-in caching. You can use it directly by calling the default app cache and use it.

use Symfony\Component\Cache\Adapter\FilesystemAdapter;



function getCachedData()
{

    
    $cache = $this->get('cache.app');

    
    $cacheKey = 'SOME_UNIQUE_KEY';

    
    $cacheItem = $cache->getItem($cacheKey);

    
    if ($cacheItem->isHit()) {
        
        $data = $cache->getItem($cacheKey)->get();
        return $data;
    }

    
    $data = [10,20,30];

    
    $cacheItem->set($data);

    
    
    $cacheItem->expiresAfter(60 * 60);

    
    $cache->save($cacheItem);

    
    return $data;
}


You can use this a lot in your application in places where you access databases or accessing the web services.

Have a great day 😊

By Imthiaz

Programmer, SAAS, CMS & CRM framework designer, Love Linux & Apple products, Currently addicted to mobile development & working @bluebeetle

Leave a comment

Your email address will not be published. Required fields are marked *