diff --git a/src/Cache.php b/src/Cache.php index 3b8e92c..4fd25c3 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -10,7 +10,6 @@ use Psr\SimpleCache\CacheInterface; use rabbit\parser\ParserInterface; -use Swoole\Table; /** * Class Cache @@ -57,6 +56,25 @@ public function getDriver(string $driver = null): CacheInterface return $drivers[$currentDriver]; } + /** + * @param string $key + * @param callable $function + * @param int $duration + * @param string $driver + * @return mixed + * @throws \Psr\SimpleCache\InvalidArgumentException + */ + public function cache(string $key, callable $function, int $duration = 0, string $driver = 'memory') + { + $driver = $this->getDriver($driver); + if ($driver->has($key)) { + return \igbinary_unserialize($driver->get($key)); + } + $result = $function(); + $driver->set($key, \igbinary_serialize($result), $driver); + return $result; + } + /** * @return array */