I need to cache some application specific data using Symfony 2's caching system so that I can run cache:clear to clear it. All the cache relies under app/cache but how do I actually go about caching data?
http://symfony.com/doc/current/cookbook/index.html
The only topic I see is about HTML caching with Varnish.
If you are using Doctrine already just use those cache classes.
Add a service to config.yml:
services:
cache:
class: Doctrine\Common\Cache\ApcCache
And use it in your controller:
if ($fooString = $this->get('cache')->fetch('foo')) {
$foo = unserialize($fooString);
} else {
// do the work
$this->get('cache')->save('foo', serialize($foo));
}
Simple way use Doctrine cache providers.
At first, register service(sample in config.yml):
services:
memcached:
class: Memcached
calls:
- [ addServer, ['localhost', 11211] ]
memcached_cache:
class: Doctrine\Common\Cache\MemcachedCache
calls:
- [ setMemcached, [#memcached] ]
Then to use get service, for example in controler:
$cache = $this->get('memcached_cache');
to send in another service use calls:
calls:
- [ setCacheProvider, [#memcached_cache] ]
or arguments:
arguments:
- #memcached_cache
In the same way, you can use other interfaces of Doctrine Cache package.
Doctrine Cache provides a very simple interface for which several out of the box implementations are provided:
ApcCache (requires ext/apc)
ArrayCache (in memory, lifetime of the request)
FilesystemCache (not optimal for high concurrency)
MemcacheCache (requires ext/memcache)
MemcachedCache (requires ext/memcached)
PhpFileCache (not optimal for high concurrency)
RedisCache.php (requires ext/phpredis)
WinCacheCache.php (requires ext/wincache)
XcacheCache.php (requires ext/xcache)
ZendDataCache.php (requires Zend Server Platform)
If you do not already use Doctrine, you may require Common Library for Doctrine projects: php composer.phar require doctrine/common or require only Caching library offering an object-oriented API for many cache backends: php composer.phar require doctrine/cache
How to use Doctrine Caching you can read in Doctrine Common documentation on Doctrine Project web site
Symfony 3.1 provide a new Cache component.
Symfony2 does not provide any component for application layer caching.
Like you were already told, you can use the Doctrine Common caching library http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html
If you want something more advanced, you can also use one of the cache bundle provided by the community. For instance, the https://github.com/TheBigBrainsCompany/TbbcCacheBundle#cachebundle which provides tools for a good caching strategy.
There is no partial cache in Symfony2, the build-in cache is full HTTP only.
You have to use a reverse proxy, and if you only want to cache a piece of code, you have to use ESI. It's maybe more work than with symfony 1 but performances worth it.
Anyway, nothing stop you to use a memcached and store some stuff in it, look at this Bundle i.e.
If as your question state it, you only have data to store, that's perfect (and a memcache cache is much faster than a filesystem one).
Related
I am trying to bootstrap a simple microservice type of application with the Symfony router and DI component, but without using the symfony/skeleton package as a starting point.
Routing works, but DI does not. Looking at the documentation of the service container, it is unclear of how my app would actually use the config/services.yaml file. I don't understand when is it loaded, how it is loaded, and how I would tell Symfony to use a services.php instead?
Similarly, if I install new composer packages, they dump some .yaml config files in config/packages/. Would I then have to load these files manually, using the config component?
All this typically happens (in a Symfony application) in the application Kernel, which is instantiated and booted by the "front-controller" script (e.g. public/index.php).
In the default Symfony 5 provided kernel you'll find this:
protected function configureContainer(ContainerConfigurator $container): void
{
$container->import('../config/{packages}/*.yaml');
$container->import('../config/{packages}/'.$this->environment.'/*.yaml');
if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
$container->import('../config/services.yaml');
$container->import('../config/{services}_'.$this->environment.'.yaml');
} elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
(require $path)($container->withPath($path), $this);
}
}
This loads all *.yaml files in the config/packages directories, and load the file config/services.php but only if the file config/services.yaml does not exist.
If you are building your own application without using the framework, you'll have to load these files wherever it makes sense for you application.
With the reduced footprint of Symfony 5, building your own is likely not particularly cost-effective, you can use the minimal symfony/skeleton and you would already have a very "micro" starting point, without having to spend time deciding these things.
If you are set on "building your own", either because of specific requirements or as a learning exercise, I recommend you reading this part of the documentation: Create your own PHP Framework. It's a very useful way of learning how the many pieces fit together.
I am using SilverStripe 4.2.2.
Wondering how could I configure Redis for caching in SilverStripe 4?
Take a look at pstaender/silverstripe-redis-cache which should make it a lot easier to get started with SilverStripe 4 and Redis right off the bat (since it implements CacheFactory and offers some configuration for SilverStripe). Also nice that it also uses predis, similar to Symfony's cache component noted above.
This happens to be ideal for me as well since one of my primary reasons for using Redis would be for template caching distributed across a cluster. 🙂
In my Syfmony 4.1 project, I'm using Custom Annotations to apply some meta data to some Value Objects. I'm following the Doctrine Annotation Documentation, which (I think) is assuming that you're using Doctrine as a stand alone package.
However, it seems to me that Symfony is handing some of the Annotation Setup for you. As an example, the documentation talks about using its own autoloading mechanism rather than the one provided by PHP / Composer. Symfony seems to handle this automatically.
The documentation talks about Annotation Readers and recommends caching your Annotations with either a FileCacheReader or Doctrine AppCache. I'm assuming that Symfony is using one of these mechanisms (or it's own caching mechanism) to cache Symfony's built in Annotations.
My question is this: Does Symofny automatically handle the caching of Custom Annotations, or do I need to cache them manually?
Symfony takes care the caching of doctrine annotations through the doctrine bundle configuration:
doctrine:
orm:
metadata_cache_driver: array #this is the option to configure
query_cache_driver: array
result_cache_driver: array
The metadata_cache_driver options define the cache driver that symfony uses to cache the annotations.
All you have to do is to configure this option properly to have your annotations cached.
More on how to properly configure the cache driver options in documentation.
We are changing our hosting environment and they do not support apcu/xcache/wincache.
Currently we use apcu for Classloader cache in our Symfony PHP webapp as described here.
Because that component does not come with a MemcachedClassloader, does it even make sense to implement the Classloader cache in Memcached (non-distributed setup)?
As commented by COil and after reading the Symfony docs about performance I think we can just use the optimized flag for Composer autoload and tweak the settings for OPcache.
My question is are the Symfony 2 cache mechanism and Twig cache mechanism the same?
Lets say I decide to use Twig in my MVC framework, would I have the same cache mechanism Symfony 2 uses? I know both Symfony2 and Twig are created by Fabien. But I dont know is Symfony 2 using only Twig cache or is there something more?
So I hope you understand the question. Does Symfony 2 uses Twig for cache? And if not, what are the differences between Symfony 2 and Twig caching?
unsure of the full answer, but symfony and twig must use separate caching mechanisms as you are not forced to use twig in symfony and yet cache still works. for more info check http://symfony.com/doc/current/ and dig into the code.
Edit:
To expand on my non-answer above. Twig cache is really a compilation cache. Your templates are saved as php files. To quote F. Potencier "[...] Twig caches the compiled templates to avoid the parsing phase for sub-sequent requests. [...]" (source)
While Symfony cache is (as has already been mentioned) a HTTP cache and is extensively explained in Symfony Docs cache section
Hope this helps.
Symfony 2 uses HTTP cache for caching purposes. And Twig cache is joust caching of Twig templates, so they don't have to recompile on every request. So Twig caching only ensures templates are compiled once on first request.
And Symfony 2 HTTP cache is another beast :) It provides abstraction around HTTP Cache mechanism and so you can cache using HTTP Cache headers etc...
So the short answer is NO! They are not the same!
No, they are not the same. Symfony caches a lot of stuff like routes, translations, the container, etc; Twig just caches templates by compiling them down to PHP classes. Of course, Twig's caching system works in a Symfony app as well.