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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With