Best practice for using Cache Component in Symfony 3 - symfony

What is the best practice for using the Cache Component in Symfony 3?
Simple example:
If I call getCategoryById (In Repository) from different places (Controller, FormType, Twig function, Listener, ...), how can I verify that the data is cached or not?
Problem:
I can't call Cache Component in Repository and I don't want to write and duplicate the same code in every place ( isHint ... ).
Question:
So what is the best practice? Create an intermediate cache service between all components and the Repository?
Thank you very much :)

Related

Symfony: How to handle common request scope data

I'm migrating a legacy PHP project (pre-OO) to Symfony2. On every request I have to:
compute some dynamic data (depending on the current date and/or some request parameter)
use that data (multiple times!) in the rendered response.
A naive approach would be:
At the start of every controller method, call some global helper function to compute the data.
At the end of every controller method, pass the data as a parameter to the twig template.
Sounds tedious. Maybe it would be better to:
Create a subscriber for request events that computes the data when a request comes in and provides access to it via getter methods.
Define that subscriber/service as a global twig variable in config.yml.
In twig templates, call the getter methods on that service as needed.
Is that viable?
In particular, are the twig variable/service and the subscriber always identical? Or could the service be a newly created instance?
Is this some sort of misuse? Or is there an officially recommended way for such a use case?
EDIT The data is not only needed in every twig template but in some controllers, too.
Calling a specific method in every Controller-Action would really be a bad solution. Your solution to use a subscriber isn't perfect either.
Without knowing your use-case in detail it´s hard to find a suitable way.
Maybe one approach would be to write a Twig-Extension and injecting a Service into this Extension. The Service would get the Request-Stack via Dependency-Injection and compute the relevant Data. You could then access this data "on demand" through the Twig-Extension during the rendering.
Another approach could be using sub-requests during the rendering (Symfony: How to handle common request scope data)
Maybe these tips already helped you a bit. Otherwise let me know more details, where / how you need the data during the rendering.

Caching entity methods/properties in symfony2

Im wondering what the best practice for caching entity / values is in symfony2?
For example if i have a entity $entity, with a method ->getLongCalculation(), i want to store that result rather than have to work it out every time its called.
Obviously its very easy just to store the result in APC using the entity ID but the problem i have is that that has to be done outside of the entity, as it doesnt have access to the container.
So for example i end up with code repeated everywhere in my controllers where i call the getter, checking if we already know the value.
Is there any clean way for me to just be able to call $entity->getLongCalculation() and the function itself handle any caching? Obviously i can just pass the container into the entity but i understand thats not best practice?
Whats the best way to go about this?
Thank you.
Is it acceptable for you to store in database the result of longCalculation ?
In this case ( i used it for my needs ) you can use lifeCycle events ( prePersist & preUpdate) to populate your result in entity
http://symfony.com/fr/doc/current/book/doctrine.html
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#lifecycle-events
In my opinion,
Yes, saving the results of calculations in database - in the same table or as a relation, it doesn't matter. It's better than cache, because it changes only when the entity is changing. Then you can use Doctrine cache system - it's quite simple and needs only to set the cache method on the repository or query (setResultCacheMethod or sth like that).
You can achieve the best performance with Doctrine cache (sql and results), redis for session, proper database architecture which avoids long time calculations and Varnish with ESI (reverse proxy cache).
At the and of the work you can finish with application at least 1000% faster than in the beginning.
Regards,

Symfony service factory with many children, all with different dependencies

I am working on a very large game project in Symfony, and keep running into variations of the same problem.
I have a service that I need to separate into multiple "sub-services" because there is too much code to be contained within one class. For instance, a custom JSON serializer handler that needs a separate handler service for each method of serialization.
I'm having trouble working out the best practice for passing dependencies between "families of services." I would preferably like to keep all the definitions within services.yml just so things don't break in the future, but am assuming that may not be achievable.
Here's a better example - I have an "ActionQueueService" which takes a rather lengthy queue of actions from the user. What I would like to do is create a separate class to handle each type of action, so -
ActionQueueService // processes action queue JSON and delegates to sub-services
AbstractAction
PurchaseAction extends AbstractAction
SellAction extends AbstractAction
HarvestAction extends AbstractAction
Now if these three "actions" are defined as services they can have their own dependencies. However, they all have to be injected into the ActionQueueService - what happens if there are 38 of them (which there will be one day).
The next logical step for me was to create ActionFactory. Now I'm only passing in one dependency to ActionQueueService, and it can invoke any action service simply by calling -
$this->actionFactory->get('Harvest');
The problem I have is that each child has it's own separate list of dependencies. A Purchase or Sell action would need the Character service and the ShopStock service, a HarvestAction would need the HarvestService. Because I've decided to use a Factory method, I have to instantiate the sub-service within the factory class. I don't want to pass every dependency into the Factory only to have that either a) inject every dependency into every child or b) handle some crazy child constructor logic.
One solution would be to pass the service container into the factory and come up with a naming convention that allows me to create the service on the fly. I've heard that this is fairly bad practice though. Maybe a wrapper around the service container that limits the amount of bad stuff that could be done with it.
If anybody has any ideas on how this could be solved with Symfony I'd appreciate it. There are similar answers out there but unfortunately I don't know any languages other than PHP/Symfony.

Where is the model for non-database functions in Symfony2

I have used Symfony 1.x forever now and am starting to get a handle on 2.x. What a daunting task converting from 1.x to 2.x is as I'm sure many of you already know.
I have a question about the Model for non database stuff.
in 1.x you had /lib/model/doctrine/abc.class.php and /lib/model/doctrine/abcTable.class.php. All your non-database model functions went in abc.class.php and all your database stuff went in abcTable.class.php
I want to add a couple of GD functions to make thumbnails of uploaded images but I'm pretty sure in the MVC conventions they are not suppose to go in the controller file. But I can't find a model folder or any place that even says model for non database stuff anywhere in the Symfony2 docs.
Can someone please let me know what the new convention for "Model" for non database elements and custom functions that use to go in lib/model folder are now located (or called) in Symfony2?
There is not really a convention for this type of stuff per say. You can add the methods to the model, or you can create a separate class that consumes a model and does all of the work that needs to be done.
I personally would create a service that handles the process. Takes the input from the controller, creates the model and the the thumbnails. I would not put the gd functions into my model classes.

I am needing to change the table schema without reloading the app domain (EF Model Caching Issue)

I have a custom implementation of a multi tenant code first system basically SQL Schema Divisions of the tenants. I am using the ToTable method to map the schema correctly on the first call, but as I have read about the model being cached changing the schema on the second call do a different tenant does not work. Is there any ways in EF 4.1 to disable the caching or to rebuild the model every time.. Yes i know this is not great for performance. Thanks for any help..
Although it is an old question, but for all those who face this issue and end up finding this question for a possible solution. Here it goes...
Initially caching could be turned off by setting the "CacheForContextType" property of the ModelBuilder to ‘false’ in the OnModelCreating method. This method is defined in DBContext as virtual and needs to be overridden. But in EF 4.1 this property has been removed, since model creation is an expensive process and the Microsoft team wanted to promote a better pattern. Check this link
It seems like the Build() command on the ModelBuilder is what you're looking for.
modelBuilder.Build().Compile().CreateObjectContext...

Resources