dev-master
9999999-devA Kdyby\Aop aspect for caching method return values
The Requires
The Development Requires
by Radek Ježdík
Wallogit.com
2017 © Pedro Peláez
A Kdyby\Aop aspect for caching method return values
A Nette framework extension providing a Kdyby\Aop aspect for caching method return values., (*1)
First, install the Kdyby\Aop extension., (*2)
Require the extension files using composer:, (*3)
$ composer require redhead/cached:@dev
In your Nette application configuration, add the extension to be registered and the caching aspect:, (*4)
extensions:
cached: Cached\CachedExtension
aspects:
- Cached\CachingAspect(@cacheStorage)
where @cacheStorage is a reference to a service implementing Nette\Caching\IStorage (you can specify your own storage, or remove the argument so the dependecy will be autowired), (*5)
Now, you can put annotation @Cached on methods in your services like this:, (*6)
class MyService {
/**
* @Cached\Cached
*/
public function getNumber() {
return rand(1, 1000);
}
}
Note: due to a bug in Kdyby\Aop, you can't import the annotation with use statements at this moment. Until it's fixed you have to provide fully qualified name of the annotation (@Cached\Cached), (*7)
After the first call of the method above, the return value is cached. Every other call will not execute the method and will return the cached value instead., (*8)
There are few properties you can use in the annotation. They specify the caching options. See the example:, (*9)
...
/**
* @Cached\Cached(key="myService.number", sliding=true, expire="1 hour")
*/
public function getNumber() {
return rand(1, 1000);
}
...
Annotation options are following:, (*10)
You can specify the settings for the above options that are shared across many advised methods, so you don't need to write them all the time. The settings will be in one place - in your config file!, (*11)
To create a profile, add section 'cached' and subsection 'profiles' to your configuration. Then add another subsection bearing the name of your profile and set the profile options. See the example:, (*12)
cached:
profiles:
myProfile:
sliding: true
expire: 1 hour
Then add annotation property 'profile' with the name of the profile as a value to the advised methods you want to share the options with., (*13)
... /** * @Cached\Cached(profile="myProfile") */ ...
You can set additional options and/or override the options of the profile by adding properties to the annotation., (*14)
When no profile is specified, it defaults to profile 'default' the options of which you can set in the configuration. The options for this profile will be used for every method annotated without property 'profile'., (*15)
Profile options are almost the same as annotation properties. There are few differences. You can specify these options: - enabled (boolean) - if false, no caching is performed and the original method will get called every time (caching is completely disabled for this profile), defaults to true - namespace (same as above) - expire (same as above) - sliding (same as above) - tags (same as above) - files (same as above) - priority (same as above), (*16)
Profile options are missing 'key' and 'profile' annotation property counterparts (as they don't make any sense here)., (*17)
You can specify options for the whole extension in the extension's config section:, (*18)
Here is an example of the whole configuration setup and usage of every config option:, (*19)
extensions:
aop: Kdyby\Aop\DI\AopExtension
annotations: Kdyby\Annotations\DI\AnnotationsExtension
cached: Cached\CachedExtension
aspects:
- Cached\CachingAspect
cached:
enabled: true
profiles:
default:
enabled: true
namespace: myNamespace
expire: 1 hour
sliding: true
files: [file1.php, file2.php]
tags: [tag1, tag2]
priority: 1
myProfile:
...
A Kdyby\Aop aspect for caching method return values