Skip to content

Instantly share code, notes, and snippets.

@mkeneqa
Last active February 7, 2020 01:22
Show Gist options
  • Save mkeneqa/20fc7ed836d6f0d97d606d148c692206 to your computer and use it in GitHub Desktop.
Save mkeneqa/20fc7ed836d6f0d97d606d148c692206 to your computer and use it in GitHub Desktop.
Symfony Caching
<?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
]);
}
<?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');
@mkeneqa
Copy link
Author

mkeneqa commented Feb 7, 2020

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