Skip to content

Commit

Permalink
Fixed Annotation @CacheEvict not work expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed May 24, 2019
1 parent bbd757c commit 1342fb0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/cache/src/AnnotationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getCacheableValue(string $className, string $method, array $argu
$group = $annotation->group;
$ttl = $annotation->ttl ?? $this->config->get("cache.{$group}.ttl", 3600);

return [$key, $ttl, $group];
return [$key, $ttl, $group, $annotation];
}

public function getCacheEvictValue(string $className, string $method, array $arguments): array
Expand All @@ -67,7 +67,7 @@ public function getCacheEvictValue(string $className, string $method, array $arg
$key = $prefix . ':';
}

return [$key, $all, $group];
return [$key, $all, $group, $annotation];
}

public function getCachePutValue(string $className, string $method, array $arguments): array
Expand All @@ -79,7 +79,7 @@ public function getCachePutValue(string $className, string $method, array $argum
$group = $annotation->group;
$ttl = $annotation->ttl ?? $this->config->get("cache.{$group}.ttl", 3600);

return [$key, $ttl, $group];
return [$key, $ttl, $group, $annotation];
}

public function getFailCacheValue(string $className, string $method, array $arguments): array
Expand All @@ -92,7 +92,7 @@ public function getFailCacheValue(string $className, string $method, array $argu
$group = $annotation->group;
$ttl = $annotation->ttl ?? $this->config->get("cache.{$group}.ttl", 3600);

return [$key, $ttl, $group];
return [$key, $ttl, $group, $annotation];
}

protected function getAnnotation(string $annotation, string $className, string $method): AbstractAnnotation
Expand Down
12 changes: 10 additions & 2 deletions src/cache/src/Aspect/CacheEvictAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Hyperf\Cache\Annotation\CacheEvict;
use Hyperf\Cache\AnnotationManager;
use Hyperf\Cache\CacheManager;
use Hyperf\Cache\Driver\KeyCollectorInterface;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand Down Expand Up @@ -50,12 +51,19 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$method = $proceedingJoinPoint->methodName;
$arguments = $proceedingJoinPoint->arguments['keys'];

[$key, $all, $group] = $this->annotationManager->getCacheEvictValue($className, $method, $arguments);
[$key, $all, $group, $annotation] = $this->annotationManager->getCacheEvictValue($className, $method, $arguments);

$driver = $this->manager->getDriver($group);

if ($all) {
$driver->clearPrefix($key);
if ($driver instanceof KeyCollectorInterface && $annotation instanceof CacheEvict) {
$collector = $annotation->prefix . 'MEMBERS';
$keys = $driver->keys($collector);
$driver->deleteMultiple($keys);
$driver->delKey($collector, ...$keys);
} else {
$driver->clearPrefix($key);
}
} else {
$driver->delete($key);
}
Expand Down
6 changes: 5 additions & 1 deletion src/cache/src/Aspect/CacheableAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Cache\AnnotationManager;
use Hyperf\Cache\CacheManager;
use Hyperf\Cache\Driver\KeyCollectorInterface;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$method = $proceedingJoinPoint->methodName;
$arguments = $proceedingJoinPoint->arguments['keys'];

[$key, $ttl, $group] = $this->annotationManager->getCacheableValue($className, $method, $arguments);
[$key, $ttl, $group, $annotation] = $this->annotationManager->getCacheableValue($className, $method, $arguments);

$driver = $this->manager->getDriver($group);

Expand All @@ -65,6 +66,9 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
$result = $proceedingJoinPoint->process();

$driver->set($key, $result, $ttl);
if ($driver instanceof KeyCollectorInterface && $annotation instanceof Cacheable) {
$driver->addKey($annotation->prefix . 'MEMBERS', $key);
}

return $result;
}
Expand Down
22 changes: 22 additions & 0 deletions src/cache/src/Driver/KeyCollectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace Hyperf\Cache\Driver;

interface KeyCollectorInterface
{
public function addKey(string $collector, string $key): bool;

public function keys(string $collector): array;

public function delKey(string $collector, ...$key): bool;
}
19 changes: 17 additions & 2 deletions src/cache/src/Driver/RedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Hyperf\Cache\Exception\InvalidArgumentException;
use Psr\Container\ContainerInterface;

class RedisDriver extends Driver
class RedisDriver extends Driver implements KeyCollectorInterface
{
/**
* @var \Redis
Expand Down Expand Up @@ -125,10 +125,25 @@ public function clearPrefix(string $prefix): bool
{
$iterator = null;
$key = $prefix . '*';
while ($keys = $this->redis->scan($iterator, $this->getCacheKey($key), 100)) {
while ($keys = $this->redis->scan($iterator, $this->getCacheKey($key), 10000)) {
$this->redis->delete(...$keys);
}

return true;
}

public function addKey(string $collector, string $key): bool
{
return (bool) $this->redis->sAdd($this->getCacheKey($collector), $key);
}

public function keys(string $collector): array
{
return $this->redis->sMembers($this->getCacheKey($collector)) ?? [];
}

public function delKey(string $collector, ...$key): bool
{
return (bool) $this->redis->sRem($this->getCacheKey($collector), ...$key);
}
}
8 changes: 7 additions & 1 deletion src/cache/src/Listener/DeleteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

namespace Hyperf\Cache\Listener;

use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Cache\AnnotationManager;
use Hyperf\Cache\CacheManager;
use Hyperf\Cache\Driver\DriverInterface;
use Hyperf\Cache\Driver\KeyCollectorInterface;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

Expand Down Expand Up @@ -52,10 +54,14 @@ public function process(object $event)
$method = $event->getMethod();
$arguments = $event->getArguments();

[$key, , $group] = $this->annotationManager->getCacheableValue($className, $method, $arguments);
[$key, , $group, $annotation] = $this->annotationManager->getCacheableValue($className, $method, $arguments);

/** @var DriverInterface $driver */
$driver = $this->manager->getDriver($group);
$driver->delete($key);

if ($driver instanceof KeyCollectorInterface && $annotation instanceof Cacheable) {
$driver->delKey($annotation->prefix . 'MEMBERS', $key);
}
}
}

0 comments on commit 1342fb0

Please sign in to comment.