Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Symfony 2, is there a good way to interact with the app/cache cache using read() and write() methods?

I want to save data to Symfony's file cache. Is there a good way to interact with the app/cache cache using read() and write() methods?

EDIT

The excellent winzou CacheBundle was exactly what I needed.

like image 712
mattalxndr Avatar asked Nov 15 '12 23:11

mattalxndr


2 Answers

You can do it from the controller:

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;

//Define your file path based on the cache one
$filename = $this->container->getParameter('kernel.cache_dir') . '/yourapp/filename.txt';

//Create your own folder in the cache directory
$fs = new Filesystem();
try {
    $fs->mkdir(dirname($filename));
} catch (IOException $e) {
    echo "An error occured while creating your directory";
}

//Write your file
file_put_contents($filename, 'Text content');

//Read the content of your file    
echo file_get_contents($filename);
like image 136
cheesemacfly Avatar answered Oct 07 '22 06:10

cheesemacfly


Note that DoctrineCacheBundle is now recommended. Don't be misled by the Doctrine branding - this is distinct and separate from the ORM and DBAL etc.

like image 36
Ian Gregory Avatar answered Oct 07 '22 05:10

Ian Gregory