Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Semantic config for app/system/pool caches #18667

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2016

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Apr 28, 2016

Q A
Branch? 3.1
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #18625
License MIT
Doc PR -

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Apr 28, 2016

I'd happily accept some help to fix the XSD please, that should be the only remaining thing left.

->scalarNode('adapter')
->info('The cache pool adapter service to use as template definition.')
->defaultValue('cache.adapter.shared')
->info('The create a new pool based on this adapter service.')
Copy link
Contributor

@GuilhemN GuilhemN Apr 29, 2016

Choose a reason for hiding this comment

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

should be To create

@nicolas-grekas
Copy link
Member Author

Hold on, I'm going to change a few things after some discussions with @javiereguiluz

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Apr 29, 2016

Following suggestions from @javiereguiluz and @fabpot, configuration is now the following:

Pre-configured cache pool services are now: cache.app, cache.system, cache.serializer and cache.validator. Cache adapters (abstract pools) are prefixed by cache.adapter.*. cache.adapter.local is replaced by cache.system. cache.adapter.shared is replaced by cache.app. They both can be reused as a parent to define new "app" or "system" pools reusing the same base adapter. They both default to cache.adapter.filesystem.

Configuration is made easy for those two, using full service names to define which adapter should be used for each of them.

E.g. to use APCu for all system caches:

framework:
  cache:
    system: cache.adapter.apcu

To use Redis for all app related caches:

framework:
  cache:
    app: cache.adapter.redis
    default_redis_provider: redis://1.2.3.4:5678

As you can see, this PR also adds a factory for creating Redis connection based on a DSN.

Then of course you can go to more advanced configurations to redefine either custom adapters or regular pools (pre-defined or not).

Adapter and pool configurations are merged in a single framework.cache.pools option. Note that pool names should be given as full service names.

E.g. to define a new system cache pool accessible with $container->get('my_local_cache_pool'):

framework:
  cache:
    pools:
      my_local_cache_pool:
        adapter: cache.system # overwrite the default which is "cache.app"
        public: true # new cache pools are private by default

E.g. to use Redis for all system pools and use https://github.com/snc/SncRedisBundle to configure the connection:

framework:
  cache:
    system: cache.adapter.redis
    default_redis_provider: snc_redis_connection_service

a verbose variant of this could be:

framework:
  cache:
    system: app.redis_cache
    pools:
      app.redis_cache:
        adapter: cache.adapter.redis
        provider: snc_redis_connection_service

Or to use a Doctrine Cache provider for all app related caches:

framework:
  cache:
    app: cache.adapter.doctrine
    default_doctrine_provider: doctrine_cache_provider_service

or verbose variant:

framework:
  cache:
    app: app.doctrine_cache
    pools:
      app.doctrine_cache:
        adapter: cache.adapter.doctrine
        provider: doctrine_cache_provider_service

The same would go for configuring a pool on top of a PSR-6 cache adapter.

@nicolas-grekas nicolas-grekas force-pushed the cache-config branch 4 times, most recently from 22bd298 to b80a8cc Compare April 29, 2016 21:58
@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Apr 29, 2016

Last tweaks pushed, green, ready for review+merge:

  • cache.system is now a public pool by default (alongside with cache.app)
  • those two special pools are reserved names in the pools config section: they must be configured through their dedicated options under cache (see examples above)
  • new pools are private by default to allow easily changing the adapter of predefined pools (cache.serializer & cache.validation today). IMHO creating custom pools is an even more advanced use case that won't suffer from having this requirement of adding public: true when needed.

@nicolas-grekas nicolas-grekas force-pushed the cache-config branch 4 times, most recently from eeb561e to f84e588 Compare April 30, 2016 08:57
@nicolas-grekas nicolas-grekas changed the title [FrameworkBundle] Semantic configuration for redis/apcu/pools/adapters caches [FrameworkBundle] Semantic config for app/system/pool caches Apr 30, 2016
@nicolas-grekas nicolas-grekas force-pushed the cache-config branch 2 times, most recently from 2318540 to a34a005 Compare April 30, 2016 09:20
private $redis;

public function __construct(\Redis $redisConnection, $namespace = '', $defaultLifetime = 0)
{
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
}
if (!$redisConnection->isConnected()) {
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure that it is a good idea to require an established connection in the constructor?

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch, check removed

@xabbuh
Copy link
Member

xabbuh commented Apr 30, 2016

@nicolas-grekas I'd be happy to split the PR in two (one for the Redis related changes and one for the semantic config). That would make reviewing a bit easier.

@@ -107,7 +107,7 @@ FrameworkBundle
framework:
cache:
pools:
serializer:
cache.serializer:
Copy link
Member

Choose a reason for hiding this comment

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

Why do we repeat the cache. prefix here. Imo this looks weird as it repeats what we already know from the config tree (I mean we are configuring the cache, why do we want to reflect that in the name here too?)?

Copy link
Member Author

Choose a reason for hiding this comment

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

because I've been told that Symfony core prefers explicit over implicit & "magic" conventions. This in fact is much more flexible and doesn't force any service prefix. See examples above where an app.doctrine_cache pool is created. We don't have to teach any special convention here. Just give a service name.

Copy link
Member

Choose a reason for hiding this comment

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

I understand that we want the service to be cache.serializer. But imo we should then add the cache. prefix here: https://github.com/nicolas-grekas/symfony/blob/cache-config/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L1059

Otherwise, the full config path we use here is framework.cache.pools.cache.serializer which imo is not really clear why I should use the cache part twice here.

Copy link
Member

Choose a reason for hiding this comment

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

And by the way, adding the cache. prefix in the extension when registering the definition will also prevent us from potentially having conflicting service names because when people configure their own cache pools, they will not always think of adding the prefix here explicitly.

Copy link
Member

Choose a reason for hiding this comment

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

I think I prefer to be explicit. Whenever possible we try to use the full service name in configuration and that's what is done here. If the end user does not want to prefix their service name with cache., it's not a problem.

Copy link
Member

Choose a reason for hiding this comment

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

But we then still have the point that the configured names are directly used as service ids which can easily lead to conflicts (we don't do that anywhere else, do we?).

Copy link
Member

Choose a reason for hiding this comment

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

After re-reading #18667 (comment) think we can stop the discussion here as it was based on a misunderstand on my end.

👍 for how the PR is right now.

@nicolas-grekas
Copy link
Member Author

To be rebased on top of #18681 (which is included for now)

@nicolas-grekas nicolas-grekas force-pushed the cache-config branch 2 times, most recently from 51bfb86 to 493c337 Compare May 1, 2016 08:49
fabpot added a commit that referenced this pull request May 1, 2016
…s-grekas)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[Cache] Add DSN based Redis connection factory

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Required by #18667

Commits
-------

3073efb [Cache] Add DSN based Redis connection factory
@fabpot
Copy link
Member

fabpot commented May 1, 2016

#18681 merged now

@nicolas-grekas
Copy link
Member Author

rebased

<argument /> <!-- PSR-6 provider service -->
<argument /> <!-- namespace -->
<argument /> <!-- default lifetime -->
</service>

<service id="cache.adapter.redis" class="Symfony\Component\Cache\Adapter\RedisAdapter" abstract="true">
<service id="cache.adapter.redis" class="Symfony\Component\Cache\Adapter\RedisAdapter" abstract="true" lazy="true">
Copy link
Member Author

Choose a reason for hiding this comment

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

I just made cache.adapter.redis lazy so that no connection is opened unless needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

but I'm not sure this is required, wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

we don't have any lazy service in Symfony core as it depends on a deps that is not a hard dep. Lazy means that the service is wrapped with a proxy, which adds its own layer of complexity, so I would avoid that if possible.

@nicolas-grekas nicolas-grekas force-pushed the cache-config branch 2 times, most recently from e4abdc8 to 8c5e9ae Compare May 2, 2016 06:42
@xabbuh
Copy link
Member

xabbuh commented May 2, 2016

Apart from how cache pool names are now treated I am 👍 for the changes.

@dunglas
Copy link
Member

dunglas commented May 2, 2016

👍

@fabpot
Copy link
Member

fabpot commented May 4, 2016

Thank you @nicolas-grekas.

@fabpot fabpot merged commit a2a567d into symfony:master May 4, 2016
fabpot added a commit that referenced this pull request May 4, 2016
…caches (tgalopin, nicolas-grekas)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[FrameworkBundle] Semantic config for app/system/pool caches

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18625
| License       | MIT
| Doc PR        | -

Commits
-------

a2a567d [FrameworkBundle] Simplify config for app/system/pool caches
80a5508 [FrameworkBundle] Add cache adapters in semantic configuration
@nicolas-grekas nicolas-grekas deleted the cache-config branch May 4, 2016 19:16
@fabpot fabpot mentioned this pull request May 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants