Robert
Robert

Reputation: 1276

Symfony 3.3 memcached

I've tried diffrent ways of using memcached. I did not find complete explanation, solution, good practice on how to use it with Symfony 3.3.

Question 1: https://symfony.com/blog/new-in-symfony-3-3-memcached-cache-adapter.

Maybe I'm not seeing something here but how do I tell Symfony what I would like to cache(metadata, query, results - I am using doctrine)? Where I can find more configuration options? Is there something I can read about it?

Question 2: http://www.inanzzz.com/index.php/post/7rum/using-doctrine-memcached-caching-with-query-builder-in-symfony

Running stats commands showed some items in memory but there's no performance increase. Nothing at all. I followed steps very carefully. Does any one know do I have to do something more to see the actual results?

I don't think it matters but as example I am using simple endpoint which is getting list of posts from database.

Upvotes: 1

Views: 795

Answers (1)

Stephan Vierkant
Stephan Vierkant

Reputation: 10144

Question 1: how to use cache?

Please see this example from the blog you referred to (New in Symfony 3.3: Memcached Cache Adapter):

$cacheProduct = $this->get('app.cache.products')->getItem($productId);
if (!$cacheProduct->isHit()) {
    $product = ...
    $cacheProduct->set($product);
    $this->get('app.cache.products')->save($cacheProduct);
} else {
    $product = $cacheProduct->get();
}

This cache adapter will allow you to store data in memory. This example uses memcache, but you can also use Redis for example. You have to decide which part of your code is too 'expensive' to perform at every request and can be cached. It's also up to you when the cache should be invalidated, which can be a real pain:

There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors. https://twitter.com/codinghorror/status/506010907021828096

The blog is about Symfony's Cache Component, but there is DoctrineCacheBundle too. The difference between them can cause some confusion, because you can use Doctrine Cache without Doctrine ORM and vice versa.

If you want Doctrine to use metadata_cache, result_cache or query_cache take a look at the configuration reference. Unfortunately I couldn't find any useful documentation about it's usage on Symfony.com.

Question 2: no performance increase?

I think you're not using any caching mechanisms right now, so try to do so.

You can open a separate question with more details if you still encounter problems after you implemented caching mechanisms.

Upvotes: 1

Related Questions