-
Notifications
You must be signed in to change notification settings - Fork 5
Cache expression #16
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
base: master
Are you sure you want to change the base?
Cache expression #16
Changes from 4 commits
619b728
e1b9786
837a498
0c8645c
fa5dba9
69c081f
1aeedbe
48aea7e
968484a
db875ca
6196890
ac8f56a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CacheBundle\Annotation; | ||
|
||
use CacheBundle\Exception\CacheException; | ||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
*/ | ||
class CacheExpression extends Cache | ||
{ | ||
/** | ||
* @var ExpressionLanguage | ||
*/ | ||
protected $expressionLanguage; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $hasEvaluation = false; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getCache() : string | ||
{ | ||
if (!$this->hasEvaluation) { | ||
$this->cache = $this->getExpressionLanguage()->evaluate($this->cache, ['this' => $this->context]); | ||
$this->hasEvaluation = true; | ||
} | ||
|
||
return $this->cache; | ||
} | ||
|
||
/** | ||
* @param object $context | ||
* | ||
* @return CacheExpression | ||
*/ | ||
public function setContext($context) : self | ||
{ | ||
$this->context = $context; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return ExpressionLanguage | ||
* | ||
* @throws CacheException | ||
*/ | ||
private function getExpressionLanguage() : ExpressionLanguage | ||
{ | ||
if (null === $this->expressionLanguage) { | ||
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { | ||
throw new CacheException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); | ||
} | ||
|
||
$this->expressionLanguage = new ExpressionLanguage(new FilesystemAdapter('expr_cache')); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we convert this into service based? This way it will be easier if someone wants to use anything else here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your talkin' about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, but also maybe classes that extend the base ExpressionLanguage... |
||
|
||
return $this->expressionLanguage; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
<?php | ||
|
||
|
||
namespace CacheBundle\Tests\Helpers; | ||
|
||
|
||
use CacheBundle\Annotation\Cache; | ||
use CacheBundle\Annotation\CacheExpression; | ||
|
||
class CacheableClass | ||
{ | ||
|
@@ -110,4 +109,22 @@ protected function protectedMethod() | |
{ | ||
return time(); | ||
} | ||
|
||
/** | ||
* @CacheExpression(cache="this.calculateCachePrefix()") | ||
* | ||
* @return int | ||
*/ | ||
public function getCachePrefixFromExpression() : int | ||
{ | ||
return rand(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function calculateCachePrefix() : string | ||
{ | ||
return 'xyz'; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we also update the readme ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep I will. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expression-language can be put as suggested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you put it as suggested you can't use
@CacheExpression
annotation I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can put it in both require-dev (so you can test it) and in suggests.
Who requires usage of expression should include the package explictly