Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
$invocation->getArguments():// ...
$key = $class . ':' . $invocation->getMethod()->name . join(':', $invocation->getArguments());
// ...
update($id, $value) можно использовать аннотацию @CacheInvalidator(get($id)), по которой совет поймет, что ему нужно сбрасывать кэш для метода get($id) с нужным значением $id.Note that PHP versions before 5.4.0 will not work completely, if you try to use aspects for code that uses Late Static Binding (LSB) feature.
$this. Поэтому нам не нужно указывать scope. bindTo()def get(key):
print 'get'
return 'get(%s)' % key
print get(42)
print get(42)
get
get(42)
get
get(42)
def cache(func):
_dict = {}
def _cache(*args, **kawrgs):
if func not in _dict:
_dict[func] = func(*args, **kawrgs)
return _dict[func]
return _cache
@cache
def get(key):
print 'get'
return 'get(%s)' % key
print get(42)
print get(42)
get
get(42)
get(42)
Canonical uses of function decorators are for creating class methods or static methods, adding function attributes, tracing, setting pre- and postconditions, and synchronisation, but can be used for far more besides, including tail recursion elimination, memoization and even improving the writing of decorators.
(new Class())->methodName() — опять отдыхает. У меня будет еще Introduction и перехват финальных методов, чем JMSAopBundle еще не скоро обзаведется.Если попытаться завернуть кэширующий прокси поверх логирующего, а тот, в свою очередь, поверх основного класса, то скорость упадет на порядок.Странно, потому как на моей практике профилирования кешируюших/логируюших прокси классов, время вызова этих __call & __get в среднем на порядки меньше вызова самих кеширующих/логирующих методов. Ну т.е. издержки от их использования составляют 0.1-0.5%.
$obj = $invocation->getThis();
$class = is_object($obj) ? get_class($obj) : $obj;
$key = $class . ':' . $invocation->getMethod()->name;
class MyInvocation extends InvocationClass
{
public function foo()
{
$obj = $this->getThis();
$class = is_object($obj) ? get_class($obj) : $obj;
$key = $class . ':' . $this->getMethod()->name;
return array($obj, $class, $key);
}
}
...
/**
* @var MyInvocation $invocation
*/
list($obj, $class, $key) = $invocation->foo();
Избавляемся от дублирования сквозного кода в PHP: рефакторинг кода с АОП