Last active
February 7, 2020 01:22
-
-
Save mkeneqa/20fc7ed836d6f0d97d606d148c692206 to your computer and use it in GitHub Desktop.
Symfony Caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//.... | |
/** | |
* @Route("/coldefjson", name="coloumn_definition_json") | |
*/ | |
public function colDefJson() | |
{ | |
$cache = new FilesystemAdapter(); | |
//delete the exisitng cached data | |
//$cache->delete('col_def_list'); | |
$definitions = $cache | |
->getItem('col_def_cache') | |
->get('value'); | |
if(empty($definitions)) { | |
//create new cache | |
$definitions = $cache->get('col_def_cache', function (ItemInterface $item) { | |
$item->expiresAfter(5000); | |
//list the coloumn definitions | |
return R::getAll('SELECT * FROM ' . Database::COLUMN_DEFINITION_TABLE); | |
}); | |
} | |
return $this->json([ | |
'colDef'=>$definitions | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | |
use Symfony\Contracts\Cache\ItemInterface; | |
/* _________________ */ | |
/* within some controller class . . . */ | |
$my_data = $Person->get('Mike'); | |
//init and add to cache | |
$cache = new FilesystemAdapter(); | |
$cache->get('rep_col_data_cache',function (ItemInterface $item) use ($my_data) { | |
$item->expiresAfter(300); //seconds | |
return $my_data; | |
}); | |
/* _________________ */ | |
/* within another class/function . . . */ | |
//retrieve data from cache | |
$cache = new FilesystemAdapter(); | |
$my_data = $cache | |
->getItem('rep_col_data_cache') | |
->get('value'); | |
/* _________________ */ | |
/* within another class/function . . . */ | |
//delete cache with key | |
$cache = new FilesystemAdapter(); | |
$cache->delete('rep_col_data_cache'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref: https://symfony.com/doc/current/components/cache.html