How to use DoctrineCacheBundle? I want to cache query (so that it is not translated to SQL each time, right?). Also I want to cache the result.
I have set it up like this:
doctrine:
orm:
metadata_cache_driver:
cache_provider: metadata_cache
query_cache_driver:
cache_provider: query_cache
result_cache_driver:
cache_provider: result_cache
and
doctrine_cache:
providers:
metadata_cache:
apc: ~
query_cache:
apc: ~
result_cache:
apc: ~
And in my controller:
$cache = $this->get('doctrine_cache.providers.query_cache');
if ($cache->contains('someid')) {
$surveysEntities = $cache->fetch('someid');
} else {
$surveysEntities = $this->getDoctrine()->getRepository('MyBundle:Survey')->getSurveys();
$cache->save('someid', $surveysEntities);
}
This will cache only query? Or both query and result? Is it enough? OR I should also do sth on query in my repository?
OK, got it. This cache result. Query is cached with doctrine via config. settings.
Related
Is there a way to set a parameter in symfony based on a certain condition, f.e. an ENV var?
Expression language came to my mind but it seems its only supported in arguments, properties and calls
config.yml:
parameters:
env(CI): false
ci: '%env(CI)%'
database_host: "#=parameter(ci) ? 'localhost' : 'mysql'"
doctrine:
dbal:
connections:
cms:
host: "%database_host%"
UPDATE
Expression language doesnt work in that case (Up to SF 3.3 currently) so i used Oli's approach which works:
get_params_from_env.php:
$container->setParameter('database_host', 'mysql');
if (getenv('CI')) {
$container->setParameter('database_host', '127.0.0.1');
}
Add this line to config.yml:
imports:
- { resource: get_params_from_env.php }
Then, in the php file, use this:
<?php
$container->setParameter('app.dummy_var', getenv('DUMMY_VAR'));
You can read about including your own PHP files here, under 'advanced techniques': http://symfony.com/doc/current/configuration/configuration_organization.html
I'm trying to use Doctrine Result Caching with Redis and Predis and SncRedisBundle and Symfony.
I would like to know if my cache+doctrine+redis+predis+SncRedisBundle configuration is set properly
snc_redis:
clients:
default:
type: predis
alias: default
dsn: redis://localhost
doctrine:
metadata_cache:
client: default
entity_manager: default
document_manager: default
result_cache:
client: default
entity_manager: [default, read]
query_cache:
client: default
entity_manager: default
because below query display what expected, but how do I know if it come from cache or not?
$query = $repository->createQueryBuilder('p')
->where('p.id < :id')
->setParameter('id', '100')
->orderBy('p.id', 'ASC')
->getQuery()
->useQueryCache(true)
->useResultCache(true);
$products = $query->getResult();
As you can see in this test https://github.com/doctrine/doctrine2/blob/master/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php#L40 there is no function to do this.
You can however achieve this by either defining your own cacheId or using ->getQuery()->getResultCacheId()deprecated ->getQuery()->getQueryCacheProfile()->getCacheKey() and ->getQuery()->getResultCacheDriver()->fetch($cacheKey)
source: https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/AbstractQuery.php#L945
I am using FosElasticaBundle in a Symfony project. I configured my mappings but I get exception "expected a simple value for field [_id] but found [START_OBJECT]]".
I'd like to see the actual JSON created by FosElasticaBundle so I can directly test it against my ElasticSearch server, and understand more about the exception.
According to FosElastica documentation, everything should be logged when debug mode is enabled (i.e. in DEV environment) but I can't see this happening; I only see Doctrine queries, but no JSON.
How can I dump the JSON created by FosElasticaBundle?
Update: mappings
# FOSElasticaBundle
fos_elastica:
clients:
default: { host: %elasticsearch_host%, port: %elasticsearch_port%, logger: false }
indexes:
app:
types:
user:
mappings:
name: ~
surname: ~
persistence:
driver: orm
model: AppBundle\Entity\User
provider: ~
listener: ~
finder: ~
I think you should only set your logger to true instead of false
fos_elastica:
clients:
default:
host: %elasticsearch_host%
port: %elasticsearch_port%
logger: true <---- set true here
...
i have read the documentation of symfony2 in relation to the performance and I have realized the following steps.
Install APC 'php-apc' on my webserver and restart my webserver
Modify my doctrine configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
metadata_cache_driver: apc
result_cache_driver: apc
query_cache_driver: apc
Now if i call a action to retrieve all users from database i see in the information bar at the bottom that doctrine execute every time 114 queries. Why the queries not cached?
My action look like this:
$users = $this->getDoctrine()->getRepository('AppUserBundle:User')->findAll();
return $this->render('AppUserBundle:User:index.html.twig', array('users' => $users));
Doctrine doesn't cache query results by default. You have to explicitly point that you want to cache query using useResultCache method. For example, if you'd like to cache getting all users, write your own method in User repository class:
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function fetchAll()
{
$query = $this->createQueryBuilder('u')->getQuery();
return $query->useResultCache(true)->getResult();
}
}
The method may take additional arguments:
public function useResultCache($bool, $lifetime = null, $resultCacheId = null)
$bool - set to true if you want to cache query result
$lifetime - TTL of cached result in seconds
$resultCacheId - you can pass your own id, in case of null Doctrine will handle that
I use fosuserbundle for auth.
Database during tests is set to be in-memory:
#config_test.yml
doctrine:
dbal:
driver: pdo_sqlite
path: :memory:
memory: true
orm:
auto_generate_proxy_classes: true
auto_mapping: true
Test file looks like that (just playground until i solve that):
Pasted
And when I run tests I get 302 redirect to the login, which should not happen.
What could be wrong with that?
When I change config to use development mysql connection - it works good - response is without redirect and the body is correct.
Ok, i managed to find out why is it happening.
I digged into Client class and its doRequest method.
protected function doRequest($request)
{
// avoid shutting down the Kernel if no request has been performed yet
// WebTestCase::createClient() boots the Kernel but do not handle a request
if ($this->hasPerformedRequest) {
$this->kernel->shutdown();
} else {
$this->hasPerformedRequest = true;
}
if ($this->profiler) {
$this->profiler = false;
$this->kernel->boot();
$this->kernel->getContainer()->get('profiler')->enable();
}
return parent::doRequest($request);
}
So kernel is shutting down every request and database is deleted from memory. That's my 99% guess.
I can't do anything for that except using other client library