Skip to content

Commit

Permalink
add cache func
Browse files Browse the repository at this point in the history
  • Loading branch information
amuluowin committed Jun 12, 2020
1 parent 67f80b3 commit 590d24b
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Psr\SimpleCache\CacheInterface;
use rabbit\parser\ParserInterface;
use Swoole\Table;

/**
* Class Cache
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 590d24b

Please sign in to comment.