This demo shows your an examples of AOP usage.
Please, choose one of available examples from navigation menu.
You can also try to run this code with XDebug.
Ttl is: 10 Take 103.702ms to call method Demo\Example\CacheableDemo:getReport Result is: 12345 Take 0.009ms to call method Demo\Example\CacheableDemo:getReport Result is: 12345
<?php
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Demo\Aspect;
use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\Around;
/**
* Caching aspect
*/
class CachingAspect implements Aspect
{
/**
* This advice intercepts an execution of cacheable methods
*
* Logic is pretty simple: we look for the value in the cache and if it's not present here
* then invoke original method and store it's result in the cache.
*
* Real-life examples will use APC or Memcache to store value in the cache
*
* @param MethodInvocation $invocation Invocation
*
* @Around("@execution(Demo\Annotation\Cacheable)")
*/
public function aroundCacheable(MethodInvocation $invocation)
{
static $memoryCache = [];
$time = microtime(true);
$obj = $invocation->getThis();
$class = is_object($obj) ? get_class($obj) : $obj;
$key = $class . ':' . $invocation->getMethod()->name;
if (!isset($memoryCache[$key])) {
// We can use ttl value from annotation, but Doctrine annotations doesn't work under GAE
if (!isset($_SERVER['APPENGINE_RUNTIME'])) {
echo "Ttl is: ", $invocation->getMethod()->getAnnotation('Demo\Annotation\Cacheable')->time, PHP_EOL;
}
$memoryCache[$key] = $invocation->proceed();
}
echo "Take ", sprintf("%0.3f", (microtime(true) - $time) * 1e3), "ms to call method $key", PHP_EOL;
return $memoryCache[$key];
}
}
<?php
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Demo\Example;
use Demo\Annotation\Cacheable;
/**
* Example class to show how to use caching with AOP
*/
class CacheableDemo
{
/**
* Returns a report and explicitly cache a result for future use
*
* In this example we use "Cacheable" annotation to explicit mark a method
*
* @param string $from This can be any value
* @Cacheable(time=10)
*
* @return string
*/
public function getReport($from)
{
// long calculation for 100ms
usleep(0.1 * 1e6);
return $from;
}
}