Reputation: 1395
I'm developing a site with Symfony2.4 which stores some "semi-static" info in the database (such as addresses, telephones, social media URL's, etc...) to allow the client modify that data through backend.
The site works fine but I think it should be some way to reduce the accesses to the database for retrieving those data for every request (because is printed in all pages).
Is there any way to cache that data? Is a good practice store it in a user session the first time it enters the site?
Thanks.
Upvotes: 0
Views: 162
Reputation: 10890
You should use APC for instance.
Assuming you have php-apc
extension installed and enabled (you can check it in phpinfo
) this is all you need to do:
in your config_prod.yml
(you don't want results to be cached in dev
environment)
doctrine:
orm:
metadata_cache_driver: apc
result_cache_driver: apc
query_cache_driver: apc
and then in your query:
$queryBuilder
(...)
->useQueryCache(true)
->useResultCache(true)
(...)
First time you make this query it will fetch data from database. Next time it will fetch data from cache. You can also set lifetime of cache: example
EDIT: Above link is to symfony 1.x documentation, however usage of useQueryCache
and useResultCache
are the same in symfony 1.x and Symfony2.x.
For bigger doc on Symfony2 Doctrine configuration check this link as @Francesco Casula mentioned
Upvotes: 1