Skip to content
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

RFC: Cache trait #1908

Merged
merged 1 commit into from
Apr 30, 2018
Merged

RFC: Cache trait #1908

merged 1 commit into from
Apr 30, 2018

Conversation

bendavies
Copy link
Contributor

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

cc @dunglas as discussed.

I noticed we were doing the same thing in quite a few places.
Thoughts please?

P.S. i've put very little thought into the naming of the trait and function. Please suggest something better!

@bendavies bendavies changed the base branch from master to 2.2 April 30, 2018 10:57
@teohhanhui
Copy link
Contributor

-1 as it's unnecessary and only makes the code more confusing.

{
public function getOrSave(string $cacheKey, CacheItemPoolInterface $pool, array &$localCache, callable $callback)
{
if (isset($localCache[$cacheKey])) {
Copy link
Member

Choose a reason for hiding this comment

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

What about introducing the localCache variable inside the trait instead of passing a reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh nice idea!

@soyuka
Copy link
Member

soyuka commented Apr 30, 2018

I'd have done a parent abstract class instead. It'd remove cacheItemPool and localCache variables. Not sure if we need this, though to me the less code the better :p.

@teohhanhui
Copy link
Contributor

If we really need to simplify the cache part, we should think about switching to PSR-16 (SimpleCache). But I think it's simple enough as it is.

@dunglas
Copy link
Member

dunglas commented Apr 30, 2018

I'm +1 to factorize this code, it's an error prone one, better not duplicate it everywhere.

@bendavies
Copy link
Contributor Author

@dunglas preference on trait vs abstract class?

@teohhanhui
Copy link
Contributor

In that case I'm -1 for something confusing to the reader of the code like doAOrB, in this case getOrSave.

@bendavies
Copy link
Contributor Author

@teohhanhui did you even read the PR description?

@teohhanhui
Copy link
Contributor

Oh, I don't mean -1 for this PR. Sorry for the confusion.

}

return $this->localCache[$cacheKey] = $routeName;
return $this->getOrSave($cacheKey, $this->cacheItemPool, function () use ($resourceClass, $operationType, $context) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps getCached? I also agree with @soyuka that we shouldn't need to pass the $cacheItemPool here, it should be in the trait.

@@ -13,7 +13,7 @@

namespace ApiPlatform\Core\Bridge\Symfony\Routing;

use Psr\Cache\CacheException;
use ApiPlatform\Core\Cache\CacheTrait;
Copy link
Contributor

Choose a reason for hiding this comment

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

CachedTrait?

{
private $localCache = [];

private function getOrSave(string $cacheKey, CacheItemPoolInterface $pool, callable $callback)
Copy link
Member

Choose a reason for hiding this comment

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

naming proposal:

<?php
private function cache(string $cacheKey, callable $getValue) {

}

With a parent class we can remove CacheItemPoolInterface from the method signature

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need a parent class. The property could be declared in the trait as we already do elsewhere?

Copy link
Member

Choose a reason for hiding this comment

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

also works yes, I just think that parent class > trait if we can :) (ability to use instanceof parent etc.)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think using inheritance here is... wrong. It's the kind of use case which traits are exactly designed to solve - horizontal code reuse.

use Psr\Cache\CacheException;
use Psr\Cache\CacheItemPoolInterface;

trait CacheTrait
Copy link
Member

Choose a reason for hiding this comment

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

Can you make it @internal?

@bendavies
Copy link
Contributor Author

comments addressed.
Note that has this has not been added to CachedIdentifiersExtractor as it looks a little more complicated, but might be possible.

//do nothing
}

$routeName = $getValue();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean $value? 😆


private function getCached(string $cacheKey, callable $getValue)
{
if (isset($this->localCache[$cacheKey])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I think we should use array_key_exists here, because we should support storing null values in the cache.

@teohhanhui teohhanhui merged commit dcdea86 into api-platform:2.2 Apr 30, 2018
@teohhanhui
Copy link
Contributor

Thanks @bendavies! 🎉

teohhanhui added a commit to teohhanhui/api-platform-core that referenced this pull request May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants