mirror of
https://github.com/getgrav/grav.git
synced 2026-07-10 16:43:29 +02:00
Clean up Utils class with minor fixes
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
1. [](#improved)
|
||||
* Objects: Add protected function `getElement()` to get serialized value for a single property
|
||||
* `ObjectPropertyTrait`: Added protected functions `isPropertyLoaded()`, `offsetLoad()`, `offsetPrepare()` and `offsetSerialize()`
|
||||
* `Grav\Framework\Cache`: Allow unlimited TTL
|
||||
|
||||
# v1.4.0-beta.3
|
||||
## 12/29/2017
|
||||
|
||||
@@ -58,6 +58,9 @@ class ChainCache extends AbstractCache
|
||||
$this->count = count($caches);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doGet($key, $miss)
|
||||
{
|
||||
foreach ($this->caches as $i => $cache) {
|
||||
@@ -75,6 +78,9 @@ class ChainCache extends AbstractCache
|
||||
return $miss;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doSet($key, $value, $ttl)
|
||||
{
|
||||
$success = true;
|
||||
@@ -87,6 +93,9 @@ class ChainCache extends AbstractCache
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doDelete($key)
|
||||
{
|
||||
$success = true;
|
||||
@@ -99,6 +108,9 @@ class ChainCache extends AbstractCache
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doClear()
|
||||
{
|
||||
$success = true;
|
||||
@@ -110,7 +122,9 @@ class ChainCache extends AbstractCache
|
||||
return $success;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doGetMultiple($keys, $miss)
|
||||
{
|
||||
$list = [];
|
||||
@@ -136,6 +150,9 @@ class ChainCache extends AbstractCache
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doSetMultiple($values, $ttl)
|
||||
{
|
||||
$success = true;
|
||||
@@ -148,6 +165,9 @@ class ChainCache extends AbstractCache
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doDeleteMultiple($keys)
|
||||
{
|
||||
$success = true;
|
||||
@@ -160,6 +180,9 @@ class ChainCache extends AbstractCache
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doHas($key)
|
||||
{
|
||||
foreach ($this->caches as $cache) {
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace Grav\Framework\Cache\Adapter;
|
||||
|
||||
use Doctrine\Common\Cache\CacheProvider;
|
||||
use Grav\Framework\Cache\AbstractCache;
|
||||
use Grav\Framework\Cache\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Cache class for PSR-16 compatible "Simple Cache" implementation using Doctrine Cache backend.
|
||||
@@ -28,6 +29,7 @@ class DoctrineCache extends AbstractCache
|
||||
* @param CacheProvider $doctrineCache
|
||||
* @param string $namespace
|
||||
* @param null|int|\DateInterval $defaultLifetime
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function __construct(CacheProvider $doctrineCache, $namespace = '', $defaultLifetime = null)
|
||||
{
|
||||
@@ -41,6 +43,9 @@ class DoctrineCache extends AbstractCache
|
||||
$this->driver = $doctrineCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doGet($key, $miss)
|
||||
{
|
||||
$value = $this->driver->fetch($key);
|
||||
@@ -49,31 +54,49 @@ class DoctrineCache extends AbstractCache
|
||||
return $value !== false || $this->driver->contains($key) ? $value : $miss;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doSet($key, $value, $ttl)
|
||||
{
|
||||
return $this->driver->save($key, $value, (int) $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doDelete($key)
|
||||
{
|
||||
return $this->driver->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doClear()
|
||||
{
|
||||
return $this->driver->deleteAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doGetMultiple($keys, $miss)
|
||||
{
|
||||
return $this->driver->fetchMultiple($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doSetMultiple($values, $ttl)
|
||||
{
|
||||
return $this->driver->saveMultiple($values, (int) $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doDeleteMultiple($keys)
|
||||
{
|
||||
// TODO: Remove when Doctrine Cache has been updated to support the feature.
|
||||
@@ -89,6 +112,9 @@ class DoctrineCache extends AbstractCache
|
||||
return $this->driver->deleteMultiple($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doHas($key)
|
||||
{
|
||||
return $this->driver->contains($key);
|
||||
|
||||
@@ -15,6 +15,8 @@ use Grav\Framework\Cache\Exception\InvalidArgumentException;
|
||||
/**
|
||||
* Cache class for PSR-16 compatible "Simple Cache" implementation using file backend.
|
||||
*
|
||||
* Defaults to 1 year TTL. Does not support unlimited TTL.
|
||||
*
|
||||
* @package Grav\Framework\Cache
|
||||
*/
|
||||
class FileCache extends AbstractCache
|
||||
@@ -22,6 +24,17 @@ class FileCache extends AbstractCache
|
||||
private $directory;
|
||||
private $tmp;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __construct($namespace = '', $defaultLifetime = null)
|
||||
{
|
||||
parent::__construct($namespace, $defaultLifetime ?: 31557600); // = 1 year
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doGet($key, $miss)
|
||||
{
|
||||
$now = time();
|
||||
@@ -47,9 +60,13 @@ class FileCache extends AbstractCache
|
||||
return $miss;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* @throws CacheException
|
||||
*/
|
||||
public function doSet($key, $value, $ttl)
|
||||
{
|
||||
$expiresAt = time() + ($ttl ?: 31557600); // = 1 year
|
||||
$expiresAt = time() + (int)$ttl;
|
||||
|
||||
$result = $this->write(
|
||||
$this->getFile($key, true),
|
||||
@@ -64,6 +81,9 @@ class FileCache extends AbstractCache
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doDelete($key)
|
||||
{
|
||||
$file = $this->getFile($key);
|
||||
@@ -71,6 +91,9 @@ class FileCache extends AbstractCache
|
||||
return (!file_exists($file) || @unlink($file) || !file_exists($file));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doClear()
|
||||
{
|
||||
$result = true;
|
||||
@@ -83,6 +106,9 @@ class FileCache extends AbstractCache
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function doHas($key)
|
||||
{
|
||||
$file = $this->getFile($key);
|
||||
@@ -90,6 +116,11 @@ class FileCache extends AbstractCache
|
||||
return file_exists($file) && (@filemtime($file) > time() || $this->doGet($key, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param bool $mkdir
|
||||
* @return string
|
||||
*/
|
||||
protected function getFile($key, $mkdir = false)
|
||||
{
|
||||
$hash = str_replace('/', '-', base64_encode(hash('sha256', static::class . $key, true)));
|
||||
@@ -102,6 +133,11 @@ class FileCache extends AbstractCache
|
||||
return $dir . substr($hash, 2, 20);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $namespace
|
||||
* @param string $directory
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private function init($namespace, $directory)
|
||||
{
|
||||
if (!isset($directory[0])) {
|
||||
@@ -129,6 +165,12 @@ class FileCache extends AbstractCache
|
||||
$this->directory = $directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @param string $data
|
||||
* @param int|null $expiresAt
|
||||
* @return bool
|
||||
*/
|
||||
private function write($file, $data, $expiresAt = null)
|
||||
{
|
||||
set_error_handler(__CLASS__.'::throwError');
|
||||
|
||||
@@ -12,6 +12,7 @@ use Grav\Framework\Cache\AbstractCache;
|
||||
|
||||
/**
|
||||
* Cache class for PSR-16 compatible "Simple Cache" implementation using in memory backend.
|
||||
*
|
||||
* Memory backend does not use namespace or default ttl as the cache is unique to each cache object and request.
|
||||
*
|
||||
* @package Grav\Framework\Cache
|
||||
@@ -23,14 +24,6 @@ class MemoryCache extends AbstractCache
|
||||
*/
|
||||
protected $cache = [];
|
||||
|
||||
/**
|
||||
* Memory Cache constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('', 300);
|
||||
}
|
||||
|
||||
public function doGet($key, $miss)
|
||||
{
|
||||
if (!array_key_exists($key, $this->cache)) {
|
||||
|
||||
@@ -29,7 +29,13 @@ class SessionCache extends AbstractCache
|
||||
|
||||
public function doSet($key, $value, $ttl)
|
||||
{
|
||||
$_SESSION[$this->getNamespace()][$key] = [self::VALUE => $value, self::LIFETIME => time() + $ttl];
|
||||
$stored = [self::VALUE => $value];
|
||||
if (null !== $ttl) {
|
||||
$stored[self::LIFETIME] = time() + $ttl;
|
||||
|
||||
}
|
||||
|
||||
$_SESSION[$this->getNamespace()][$key] = $stored;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -43,7 +49,7 @@ class SessionCache extends AbstractCache
|
||||
|
||||
public function doClear()
|
||||
{
|
||||
$_SESSION[$this->getNamespace()] = [];
|
||||
unset($_SESSION[$this->getNamespace()]);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -62,7 +68,7 @@ class SessionCache extends AbstractCache
|
||||
{
|
||||
$stored = isset($_SESSION[$this->getNamespace()][$key]) ? $_SESSION[$this->getNamespace()][$key] : null;
|
||||
|
||||
if ($stored && $stored[self::LIFETIME] < time()) {
|
||||
if (isset($stored[self::LIFETIME]) && $stored[self::LIFETIME] < time()) {
|
||||
unset($_SESSION[$this->getNamespace()][$key]);
|
||||
$stored = null;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ trait CacheTrait
|
||||
protected function init($namespace = '', $defaultLifetime = null)
|
||||
{
|
||||
$this->namespace = (string) $namespace;
|
||||
$this->defaultLifetime = $this->convertTtl($defaultLifetime, true);
|
||||
$this->defaultLifetime = $this->convertTtl($defaultLifetime);
|
||||
$this->miss = new \stdClass;
|
||||
}
|
||||
|
||||
@@ -220,6 +220,11 @@ trait CacheTrait
|
||||
abstract public function doDelete($key);
|
||||
abstract public function doClear();
|
||||
|
||||
/**
|
||||
* @param array $keys
|
||||
* @param mixed $miss
|
||||
* @return array
|
||||
*/
|
||||
public function doGetMultiple($keys, $miss)
|
||||
{
|
||||
$results = [];
|
||||
@@ -234,6 +239,11 @@ trait CacheTrait
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @param int $ttl
|
||||
* @return bool
|
||||
*/
|
||||
public function doSetMultiple($values, $ttl)
|
||||
{
|
||||
$success = true;
|
||||
@@ -245,6 +255,10 @@ trait CacheTrait
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $keys
|
||||
* @return bool
|
||||
*/
|
||||
public function doDeleteMultiple($keys)
|
||||
{
|
||||
$success = true;
|
||||
@@ -287,6 +301,10 @@ trait CacheTrait
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $keys
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function validateKeys($keys)
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
@@ -296,13 +314,12 @@ trait CacheTrait
|
||||
|
||||
/**
|
||||
* @param null|int|\DateInterval $ttl
|
||||
* @param bool $ignoreDefault Used internally inside $this->init().
|
||||
* @return int|null
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function convertTtl($ttl, $ignoreDefault = false)
|
||||
protected function convertTtl($ttl)
|
||||
{
|
||||
if (!$ignoreDefault && $ttl === null) {
|
||||
if ($ttl === null) {
|
||||
return $this->getDefaultLifetime();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user