Symfony2 Application Parses YML On Every Request - php

My Symfony2 application has some performance problems, so I ran a webgrind on it in order to see what was happening. Turns out it was parsing huge amounts of YML files on every request and I can't figure out why. I already have APC caching enabled so I don't know what it could be. Any help with this issue would be much appreciated.
Edit: Here is a screenshot of the webgrind I ran.

From my research, it appears that the APCClassLoader does not cause Symfony2 to cache the YAML files used in Doctrine. It appears to cache the configuration files by default (config.yml, parameters.yml), but the actual ORM YAML files used by Doctrine are not cached unless you specify a cache driver as shown in the documentation linked below:
http://symfony.com/doc/2.3/reference/configuration/doctrine.html#caching-drivers
So, it's possible that the solution above solved the issue if the poster had only configuration YAML and not doctrine YAML. However, if Doctrine YAML is involved, a Doctrine cache driver must be specified.
This also affects people using Annotations as they will be parsed on each page load unless a cache driver is specified (other than the Doctrine default array cache).
Thought I should post because this is a complex issue and the answer above was misleading in my case where Doctrine ORM YAML files were the source of the caching issue.
This post has more details on my specific issue and the resolution:
Why is Symfony2 app spending 70-90% of its time parsing YAML?

Figured out what was going on. I had APC enabled and working, but wasn't using the ApcUniversalClassLoader in my autoload.php. More details here. When they say in the docs "suggestions" for improved performance they really mean "you have to do this or your performance will be terrible."

Related

Symfony bundle configuration

I've just started working with Symfony and have run into a problem that I'm having a hard time tracking down information about.
I'm trying to create a bundle which has its own configuration file, e.g. configuration for doctrine connections.
All documentation I've found have never mentioned of showed how this can be set. Is it possible?
What I want to solve:
I have a bundle which when installed should handle connection to a secondary database table without any configuration needed from the main application in which the bundle has been integrated. Ultimately the configuration in the bundle should be override-able from the main application.
The bundle should be in the lack for a better work "self contained".
I've found documenation about bundle configuration. But all I've seen mentioned there is if one would like to configure the bundle and not interaction with other components (might have missed something).
tl;dr I want to have a config (e.g. AppBundle/Resources/Config/config.yml) file inside a bundle which can configure things like doctrine.
What i've tried
I've tried placing the configuration inside a config.yml file located in Resources/Config/. But I guess the file is never read.
I think it is not good idea to put something related to configuration right inside your bundle and ruin it's reusability by doing such thing. As far as I understood your task what your really need is to configure second entity manager to manage entities from secondary database when you need them. Same problem and its solution are described in following question: Doctrine 2 - Multiple databases configuration and use
Hope that will help!

Using local cache along with Doctrine ORM

I was working with Silex and Doctrine ORM. To make my database queries faster, I wanted to have a caching of some sort.
I looked at PhpFastCache - which provides a good caching framework - but does not really integrate with Doctrine. The best part about this is that I can have a local cache independent of any external service - like memcached. Since I have a small site which is hosted on shared host, I cannot spend money on having a service on cloud.
I also looked at existing cache providers for Doctrine ORM and all of them use external cache service.
The last thing I know I would have to do is write a provider myself using the PhpFastCache, but just wanted to make sure that there is no alternative online that I can use. I have tried my best by searching online all day today, but I just wanted to make sure.
Just to add: I have looked at APC and Memcache, but I have my site on shared hosting, and I would need a dedicated hosting for installing the PECL modules for APC/Memcache :(.
Doctrine includes quite a few cache drivers that do not seem to be documented. There is not one for PhpFastCache, but there are two that cache directly to the filesystem. Check out FilesystemCache and PhpFileCache. You can see the full list in the repository.
If I had to guess, I'd say that FilesystemCache is what you want. It stores serialized data in a plain file. PhpFileCache stores it as a PHP file, and then uses include to read it later. That means it has to be parsed by PHP on read, which is probably slower unless you use a PHP bytecode cache like APC.
Neither solution will be as fast as something like Memcache since they both read from the filesystem instead of memory, but they should provide an optimization for slow database queries that are run often.
Edit: As Kiran Madipally pointed out, it should be easy create your own PhpFastCache driver by extending CacheProvider.
I quickly wrote a provider for PhpFastCache. I have added the gist here:
https://gist.github.com/thephoenics/ee7de9f95bfdf5f6c24f

What should be first: setting or cache object

I've a very basic question which drives me nuts. I maintain my own little framework. I can configure the framework with a YAML/JSON/XML/whatever settings file. The framework uses also a cache (any of memcached/couchbase/whatever even file based caching if no caching server is installed).
So no I've the following problem: I like to cache my settings parsed from the settings file in the cache but I would like to define the cache type used for that in the settings file.
What would be a proper solution for this? I can't imagine how I should manage this which leeds me to the thought that I probably have a very basic design / architecture error in my framework. Is there any solution at all?
Well as my experience from Symfony1 and Symfony2 goes, cache as much as you can.
In their production environment everything is cached, so you would run into your problem that the cache type is described in the settings file which is itself cached.
The proper solution to this is: As in Symfony: Delete the cache in prod, once you made changes to settings.
For dev the settings are always re-read as you do not profile in debug, so ease of development is more important that bootstrap time.
I recommend splitting this like symfony did.
For prod your settings are seldomly changed so parsing a file that can be cached is wasted resources and speed is typically priority 1 in prod.
Edit: Your options regarding the order of bootstrapping:
Always read settings first then decide which cache you will need.
Use a settings configuration cache that is hard coded (e.g file based)
I recommend using option 2. Your framework is cache agnostic as this can be configured, which is good but for basic settings of your framework you simply do not need that. You don't want to setup different cache mechanisms like memcache, sql etc just for basic settings.
Symfony solves this by the most effective way, as it provides a default cache generation for settings and this is simply a php file. Thats all. When symfony loads settings it looks for a certain file to include, if it does not exist, symfony caches it by creating plain php and then reads it.
You can determine the file type from the file extension. Then you can read the cache type and create a cache. The cache should be a singleton.
For the creation of the cache I would use an abstract factory, which implementation is dependent on the file type. The appropriate implementation of the abstract factory then can read the settings file and create the right cache.

Output Cache and Redis?

I'm trying to implement the whole page cache in my website. (Just like stackoverflow). I have already implemented the Output Cache, but my friend told me that stackoverflow uses redis as their cache layer and I'm confused about the redis part.
Is redis the same as outputcache? Can I implement outputcache by using redis? (For yii developers, I'm using Yii's outputcache).
Thanks!
Yii's output cache will store the cached content using the active cache component, which can be CDummyCache/CDbCache/CApcCache/CFileCache/CMemCache, etc(what you set in the config file under the components area).
As it stands right now, there is no official CRedisCache component, but there is this extension: http://www.yiiframework.com/extension/rediscache/ which might help you.
Also, since Redis is key/value store and a bit more(though you won't use that bit more at all i guess) you can give CMemCache a try(having in mind you have memcache php extension and memcached daemon installed on your server).
L.E: i also found this for you: https://github.com/phpnode/YiiRedis which seems very neat.

APC as a session handler using Symfony components

I'm using Symfony components in my web application. I need to store session in APC but unfortunatelly I can't find the way to do it.
As I see here Symfony does not support APC as a session handler. Is that true?
I have found an old example of using APC as a session handler in Symfony. But there all configuration is done in factories.yml file which I don't have since I'm only using standalone Symfony components.
Can anyone give me an example of using APC as a session handler using only Symfony components?
Since I don't get any answer here for a long time I will answer the question myself. For now there is no built in suport for APC as a session handler in Symphony framework. There is no particlar reason for it, likely Symphony developers just did not get to it.
The solution is simple, just code APCSessionHandler.php file yourself (I was not doing it because we decided not use this in project), APCSessionHandler will be very similar to MemcachedSessionHandler.php file.
How to store PHP sessions in APC Cache? suggests it is feasible but a bad idea for a busy site. The accepted answer lists a few useful ideas

Categories