Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[2.7][Translation] added message cache.#13986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

aitboudad
Copy link
Contributor

QA
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Fixed tickets#13948,#2570,#13676
Tests pass?yes
LicenseMIT
  • tests
  • add MessageDoctrineCache
# app/config/config.ymlframework:translator:cache:translation.cache.doctrine# translation.cache.default

@@ -76,11 +70,15 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
*
* @api
*/
public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false)
public function __construct($locale, MessageSelector $selector = null, $cache = null, $debug = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The docblock needs to be changed to take into account that$cache can be an instance ofMessageCacheInterface.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

fixed

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch frome20e348 to40f2628CompareMarch 26, 2015 14:06
@aitboudadaitboudad changed the title[WIP][Translation] added message cache.[2.7][Translation] added message cache.Mar 26, 2015
@aitboudad
Copy link
ContributorAuthor

I think this could be a nice addition for 2.7, the default cache is useful in 80% of apps but when the size of catalogue became more than 10 MB it's consumes too much memory.
So the solution is to use DoctrineMessageCache, I tested with a catalogue(~100 mb) and the result look really interesting:

#config.ymlframework:translator:{ fallbacks: [en], cache: translation.cache.doctrine }doctrine_cache:providers:translation_cache:memcached:servers:localhost:11211
#services.ymltranslation.cache.doctrine:class:Symfony\Component\Translation\DoctrineMessageCachearguments:[%kernel.debug%, @translation_cache]

https://blackfire.io/profiles/compare/82c496fe-200a-451d-a2c8-e2fd458f0193/graph
selection_012

/cc@fabpot

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch 4 times, most recently fromb39dd88 to45882cdCompareMarch 26, 2015 14:42
@aitboudad
Copy link
ContributorAuthor

@Tobion can you review this PR ?

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch from45882cd tob17354fCompareMarch 28, 2015 02:30
@@ -152,5 +153,13 @@
<service id="translation.extractor" class="%translation.extractor.class%"/>

<service id="translation.writer" class="%translation.writer.class%"/>

<!-- Cache -->
<service id="translation.cache.default" class="Symfony\Component\Translation\MessageCache" public="false">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You can make this service private. Then it could be inlined or removed by the compiler.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

isn't it ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes it is already. Sorry.

@aitboudad
Copy link
ContributorAuthor

ping @symfony/deciders

@@ -652,6 +652,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder

$container->setParameter('translator.logging', $config['logging']);

if (isset($config['cache']) && $config['cache']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Isn'tisset($config['cache']) alone sufficient enough?

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch fromb17354f todfd828fCompareMarch 30, 2015 21:49
@aitboudad
Copy link
ContributorAuthor

any feedback, I really want it to be available in 2.7 :)

@xabbuh
Copy link
Member

👍

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch 2 times, most recently fromd375b6d to803e7d9CompareApril 1, 2015 09:06

<!-- Cache -->
<service id="translation.cache.default" class="Symfony\Component\Translation\MessageCache" public="false">
<argument key="cache_dir">%kernel.cache_dir%/translations</argument> <!-- cache_dir -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thekey can be removed as it's not used.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

fixed

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch from803e7d9 toeb33493CompareApril 2, 2015 07:00
* @param bool $debug Use cache in debug mode ?
* @param string $locale The locale
* @param MessageSelector|null $selector The message selector for pluralization
* @param string|null|MessageCacheInterface $cache The directory to use for the cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

the description is not accurate anymore

@fabpot
Copy link
Member

I tend to agree with@mpdude

@aitboudad
Copy link
ContributorAuthor

anyway the main thing here is to provide a doctrine cache for translation, what I didn't like for ConfigCache is it mainly related to resource file.

@aitboudad
Copy link
ContributorAuthor

any decision about this feature the current implementation look good to me :),
the#14178 look nice but didn't solve the issue at all,
another thing to consider here is that users can use the cache without the config component deps.

@mpdude
Copy link
Contributor

#14178 tries to solve a completely different problem.

But even with sticking to the current way of usingConfigCache, we should be able to improve theTranslator by possibly having several caches (loading smallerMessageCatalogues) and/or improved memory efficiency by keeping messages in a shared memory cache (which this PR is about, if I understand it correctly).

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch fromcbd6c79 tof5c9a3dCompareApril 4, 2015 12:44
@mpdude
Copy link
Contributor

If I got the code right (not sure about that), your goal is to keep messages in a key-value-store type cache.

That way, theTranslator does not have to load a possibly hugeMessageCatalogue (or even several of them) from theConfigCache only to return a single message. It might be able to retrieve the message directly from the key-value-cache.

Also, the message catalogue will mostly be kept in shared memory and reduce the memory footprint for a single PHP process. With the current implementation, the entire Catalogue will be read from theConfigCache into per-request memory.

So, if this understanding is correct, can't we find a way to make the Translator (key-value-)cache aware and use a lookup strategy based on (locale, domain, message-id) without having to touch theConfigCache related parts at all?

Here's the steps such a Translator would need to do:

  1. Figure out if the catalogue is still fresh/valid

    For this, we could stick with the currentConfigCache validation. If we detect that we need to rebuild theConfigCache, we subsequently also need to copy/map the Catalogue (after we loaded it) into the key-value-store/cache.

  2. Look into the cache

    Beforerequireing the Catalogue from theConfigCache, check whether the requested (locale, domain, message-id) is available in the (key-value-)cache. If so, return it from there.

  3. Cache miss

    If a requested translation cannot be found, it does either not exist or it may be missing because the key-value-cache has been purged.

    We could detect the latter by having an extra key that is written once the cache was loaded. If that key is missing, re-populate the key-value-cache with theMessageCatalogue loaded from theConfigCache (!).

Does that make sense?

@@ -659,6 +659,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder

$container->setParameter('translator.logging', $config['logging']);

if (isset($config['cache'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You should prefix cache entries with something specific with the current installation to prevent conflicts if several Symfony apps run on the same server.

The example for the validator:https://github.com/aitboudad/symfony/blob/translation_catalogue_cache/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L774

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

fixed.

@aitboudad
Copy link
ContributorAuthor

@mpdude you understand this topic now better than me :) , below my answers:

If I got the code right (not sure about that), your goal is to keep messages in a key-value-store type cache.

The main think here for me is to move the cache outside of Translator and adding an initial implementation of key-value-store(doctrine/cache).

can't we find a way to make the Translator (key-value-)cache aware

Right I think missed this part even if we use key-value-store we need ``ConfigCache` to make cache aware better.
I work now on this part so I'll fix it soon.

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch fromf5c9a3d toaf78b62CompareApril 6, 2015 21:50
@aitboudad
Copy link
ContributorAuthor

@mpdude Hi,
If we decide to use the Translator (key-value-) we don't need anymore to save cache in file !! and I think the current limitations ofConfigCache is it doesn't support key/value cache.

So what I made now with69225b8 is refresh cache when resources file change.

And to understand how I make the Translator (key-value-)cache aware:

1 - Figure out if the catalogue is still fresh/valid.

isFresh

2 - Look into the cache

check whether the requested (locale, domain, message-id) is available in the (key-value-)cache

3 - Cache miss

If that key is missing, re-populate the key-value-cache with the MessageCatalogue loaded from the Loaders and not ConfigCache !

@aitboudadaitboudadforce-pushed thetranslation_catalogue_cache branch from69225b8 to2860af4CompareApril 7, 2015 10:19
@mpdude
Copy link
Contributor

If you're trying to go withoutConfigCache, you will end up re-implementing the freshness check it provides (for examplehere). You also seem to need theDoctrineMessageCache as a replacement forConfigCache.

My suggestion was to keep theConfigCache part as-is. It's just a file sitting on the disk and does no harm. That way, you can still use the resource validation logic it provides. You will see from withinTranslator when the catalogue has to be rebuilt, that's when the cache-is-fresh check fails.

In other words, theDoctrineCache would just be another caching layer on top ofConfigCache: InTranslator, before you load the entire Catalogue from disk, see if you can find that particular message in the DoctrineCache first.

When you have the Catalogue at hand anyways (because you had to rebuild itor load it because the message was not in DoctrineCache), also save it to the DoctrineCache.

@aitboudad
Copy link
ContributorAuthor

@mpdude well I agree to some extent, but I didn't like the idea of writing cache twice in ConfigCache then DoctrineCache.

@fabpot what do you think?

@jakzal
Copy link
Contributor

I'd hold off and do not merge this into 2.7. Note that I didn't review this PR in details just yet, and I only scanned through comments. Forgive me if I'm repeating someone else here.

We need to think of how to organise doctrine integration more carefully and make it consistent across the framework. With the current implementation I don't think that doctrine cache classes belong to the Doctrine bridge, as they're only use is the translator.

Serializer and Validator have their own cache implementations, slightly different from each other - first relies on doctrine/cache directly, the later adapts its interface. This PR introduces yet another approach. All I'm saying we should stop and consider if we can make it a bit more consistent.

@aitboudad
Copy link
ContributorAuthor

The safe way is to split this PR into 2, let's discuss about addingmessage cache interface and once accepted I'll open another one for doctrine cache.

@aitboudadaitboudad deleted the translation_catalogue_cache branchMay 3, 2015 00:57
fabpot added a commit that referenced this pull requestJun 23, 2015
…ion for validator mapping (jakzal)This PR was merged into the 2.8 branch.Discussion----------[FrameworkBundle] Add a doctrine cache service definition for validator mapping| Q             | A| ------------- | ---| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        |symfony/symfony-docs#5409Following#12975, this PR only registers a new service so it's possible to use the new doctrine based cache implementation instead of the deprecated one. To use it, the end user would need to configure it in his `config.yml`:```yamlframework:    validation:        cache: validator.mapping.cache.doctrine.apc```In 3.0 we'll be able to replace the deprecated definition by aliasing `validator.mapping.cache.apc` to `validator.mapping.cache.doctrine.apc`.I thought of automatic wrapping of services which implement doctrine interface, but decided it would be too magic.I'm not convinced if APC is a good default anymore and hope for some discussion. I've used it as it's also used in serializer, and probably translation (see#13986). Since there's a built in opcache in more recent PHP versions, and apcu doesn't seem to be stable, there are better choices. Perhaps a better default would be a filesystem cache (not better performing, but it works anywhere).Commits-------0642911 [FrameworkBundle] Add a doctrine cache service definition for validator mapping
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

8 participants
@aitboudad@xabbuh@fabpot@mpdude@jakzal@dunglas@GromNaN@stof

[8]ページ先頭

©2009-2025 Movatter.jp