Skip to content

Commit ac8f56a

Browse files
author
Bogdan Rancichi
committed
added README
1 parent 6196890 commit ac8f56a

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ You have to configure the name of the service that is PSR6 compliant, that means
6767
6868
## How to use
6969
70+
EmagCacheBundle comes with 2 different ways you can add annotation cache to your service:
71+
72+
1. @Cache annotation
73+
7074
Add @Cache annotation to the methods you want to be cached:
7175
7276
@@ -106,6 +110,60 @@ Here is an example from a service:
106110
}
107111
```
108112

113+
2. @CacheExpression annotation witch uses [Symfony ExpressionLanguage](http://symfony.com/doc/current/components/expression_language.html)
114+
component:
115+
116+
```php
117+
118+
use Emag\CacheBundle\Annotation\CacheExpression;
119+
120+
/**
121+
* @CacheExpression(cache="<put your expression language code>", [key="<name of argument to include in cache key separated by comma>", [ttl=600, [reset=true ]]])
122+
*/
123+
```
124+
125+
126+
Here is an example from a service:
127+
128+
```php
129+
130+
namespace AppCacheBundle\Service;
131+
132+
use Emag\CacheBundle\Annotation as eMAG;
133+
134+
class AppService
135+
{
136+
/** @var string */
137+
private $prefix;
138+
139+
public function __construct(string $prefix)
140+
{
141+
$this->prefix = $prefix;
142+
}
143+
144+
/**
145+
* @eMAG\CacheExpression(cache="this.buildCachePrefix()")
146+
*
147+
* @return int
148+
*/
149+
public function getIntenseResult() : int
150+
{
151+
// 'Simulate a time consuming operation';
152+
sleep(20);
153+
154+
return rand();
155+
}
156+
157+
/**
158+
* @return string
159+
*/
160+
public function buildCachePrefix() : string
161+
{
162+
return sprintf('_expr[%s]', $this->prefix);
163+
}
164+
}
165+
```
166+
109167
## Want to contribute?
110168

111169
Submit a PR and join the fun.

tests/Helpers/CacheableExpressionClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CacheableExpressionClass
99
/** @var string */
1010
private $prefix;
1111

12-
public function __construct($prefix)
12+
public function __construct(string $prefix)
1313
{
1414
$this->prefix = $prefix;
1515
}

0 commit comments

Comments
 (0)