+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 103 additions & 10 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,125 @@

namespace Vectorface\Cache;

use Psr\SimpleCache\CacheInterface;
use DateInterval;
use Traversable;
use Vectorface\Cache\Exception\CacheException;
use Vectorface\Cache\Exception\InvalidArgumentException;

/**
* Cache: A common interface to various types of caches
*/
interface Cache extends CacheInterface
interface Cache
{
/**
* @inheritDoc
* @param array|\Traversable $keys A list of keys that can obtained in a single operation.
* @return array|\Traversable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
* Fetches a value from the cache.
*
* @param string $key The unique key of this item in the cache.
* @param mixed $default Default value to return if the key does not exist.
*
* @return mixed The value of the item from the cache, or $default in case of cache miss.
*
* @throws InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function get($key, $default = null);

/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*
* @throws InvalidArgumentException|CacheException
* MUST be thrown if the $key string is not a legal value.
*/
public function set($key, $value, $ttl = null);

/**
* Delete an item from the cache by its unique key.
*
* @param string $key The unique cache key of the item to delete.
*
* @return bool True if the item was successfully removed. False if there was an error.
*
* @throws InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function delete($key);

/**
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
*/
public function clear();

/**
* Obtains multiple cache items by their unique keys.
*
* @param array|Traversable $keys A list of keys that can obtained in a single operation.
* @param mixed $default Default value to return for keys that do not exist.
*
* @return array|Traversable A list of key => value pairs. Cache keys that do not exist or are stale will
* have $default as value.
*
* @throws InvalidArgumentException
* MUST be thrown if $keys is neither an array nor a Traversable,
* or if any of the $keys are not a legal value.
*/
public function getMultiple($keys, $default = null);

/**
* @inheritDoc
* @param array|\Traversable $values A list of keys that can obtained in a single operation.
* Persists a set of key => value pairs in the cache, with an optional TTL.
*
* @param array|Traversable $values A list of keys that can obtained in a single operation.
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*
* @throws InvalidArgumentException|CacheException
* MUST be thrown if $values is neither an array nor a Traversable,
* or if any of the $values are not a legal value.
*/
public function setMultiple($values, $ttl = null);

/**
* @inheritDoc
* @param array|\Traversable $keys A list of keys that can obtained in a single operation.
* Deletes multiple cache items in a single operation.
*
* @param array|Traversable $keys A list of keys that can obtained in a single operation.
*
* @return bool True if the items were successfully removed. False if there was an error.
*
* @throws InvalidArgumentException
* MUST be thrown if $keys is neither an array nor a Traversable,
* or if any of the $keys are not a legal value.
*/
public function deleteMultiple($keys);

/**
* Determines whether an item is present in the cache.
*
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
* and not to be used within your live applications operations for get/set, as this method
* is subject to a race condition where your has() will return true and immediately after,
* another script can remove it making the state of your app out of date.
*
* @param string $key The cache item key.
*
* @return bool
*
* @throws InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function has($key);

/**
* Manually clean out entries older than their TTL
*
Expand All @@ -36,7 +129,7 @@ public function deleteMultiple($keys);
public function clean();

/**
* Clear the cache. Equivalent to CacheInterface::clean()
* Clear the cache. Equivalent to CacheInterface::clear()
*
* @return bool True if successful, false otherwise.
*/
Expand Down
1 change: 1 addition & 0 deletions src/CacheHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CacheHelper
* @param mixed[] $args The arguments to be passed to the callback, if it needs to be called.
* @param int $ttl If a value is to be set in the cache, set this expiry time (in seconds).
* @return mixed The value stored in the cache, or returned by the callback.
* @throws Exception\CacheException
*/
public static function fetch(Cache $cache, string $key, callable $callback, array $args = [], $ttl = 300)
{
Expand Down
1 change: 0 additions & 1 deletion src/Common/PSR16Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Vectorface\Cache\Exception\CacheException;
use Vectorface\Cache\Exception\InvalidArgumentException as CacheArgumentException;


/**
* Utility methods common to many PSR-16 cache implementations
*/
Expand Down
3 changes: 2 additions & 1 deletion src/LogDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vectorface\Cache;

use InvalidArgumentException;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(Cache $cache, LoggerInterface $log = null, $level =
{
$levels = ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug'];
if (!in_array($level, $levels)) {
throw new \InvalidArgumentException("Incompatible log level: $level");
throw new InvalidArgumentException("Incompatible log level: $level");
}

$this->cache = $cache;
Expand Down
94 changes: 94 additions & 0 deletions src/SimpleCacheAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Vectorface\Cache;

use Psr\SimpleCache\CacheInterface;
use Traversable;

/**
* Adapts a Vectorface cache instance to the PSR SimpleCache interface.
*/
class SimpleCacheAdapter implements CacheInterface
{
/** @var Cache */
protected $cache;

/**
* Create an adapter over a Vectorface cache instance to the SimpleCache interface.
*
* @param Cache $cache
*/
public function __construct(Cache $cache)
{
$this->cache = $cache;
}

/**
* @inheritDoc
*/
public function get($key, $default = null)
{
return $this->cache->get($key, $default);
}

/**
* @inheritDoc
* @throws Exception\CacheException
*/
public function set($key, $value, $ttl = null)
{
return $this->cache->set($key, $value, $ttl);
}

/**
* @inheritDoc
*/
public function delete($key)
{
return $this->cache->delete($key);
}

/**
* @inheritDoc
*/
public function clear()
{
return $this->cache->clear();
}

/**
* @inheritDoc
* @param array|Traversable $keys
*/
public function getMultiple($keys, $default = null)
{
return $this->cache->getMultiple($keys, $default);
}

/**
* @inheritDoc
* @param array|Traversable $values
* @throws Exception\CacheException
*/
public function setMultiple($values, $ttl = null)
{
return $this->cache->setMultiple($values, $ttl);
}

/**
* @inheritDoc
* @param array|Traversable $keys
*/
public function deleteMultiple($keys)
{
return $this->cache->deleteMultiple($keys);
}

/**
* @inheritDoc
*/
public function has($key)
{
return $this->cache->has($key);
}
}
13 changes: 7 additions & 6 deletions src/TempFileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Vectorface\Cache;

use Exception;
use Psr\SimpleCache\InvalidArgumentException;
use Vectorface\Cache\Common\MultipleTrait;
use Vectorface\Cache\Common\PSR16Util;
Expand Down Expand Up @@ -34,7 +35,7 @@ class TempFileCache implements Cache
* - Without a directory argument, the system tempdir will be used (e.g. /tmp/TempFileCache/)
* - If given a relative path, it will create that directory within the system tempdir.
* - If given an absolute path, it will attempt to use that path as-is. Not recommended.
* @throws \Exception
* @throws Exception
*/
public function __construct($directory = null, $extension = '.tempcache')
{
Expand All @@ -43,7 +44,7 @@ public function __construct($directory = null, $extension = '.tempcache')

$realpath = realpath($this->directory); /* Get rid of extraneous symlinks, ..'s, etc. */
if (!$realpath) {
throw new \Exception("Could not get directory realpath");
throw new Exception("Could not get directory realpath");
}
$this->directory = $realpath;

Expand All @@ -54,20 +55,20 @@ public function __construct($directory = null, $extension = '.tempcache')
* Check for a directory's existence and writability, and create otherwise
*
* @param string $directory
* @throws \Exception
* @throws Exception
*/
private function checkAndCreateDir($directory)
{
if (!file_exists($directory)) {
if (!@mkdir($directory, 0700, true)) {
throw new \Exception("Directory does not exist, and could not be created: {$directory}");
throw new Exception("Directory does not exist, and could not be created: {$directory}");
}
} elseif (is_dir($directory)) {
if (!is_writable($directory)) {
throw new \Exception("Directory is not writable: {$directory}");
throw new Exception("Directory is not writable: {$directory}");
}
} else {
throw new \Exception("Not a directory: {$directory}");
throw new Exception("Not a directory: {$directory}");
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/TieredCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vectorface\Cache;

use InvalidArgumentException;
use Vectorface\Cache\Common\PSR16Util;

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ public function __construct($caches = null)
}
foreach ($caches as $i => $cache) {
if (!($cache instanceof Cache)) {
throw new \InvalidArgumentException("Argument $i is not of class Cache");
throw new InvalidArgumentException("Argument $i is not of class Cache");
}
$this->caches[] = $cache;
}
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载