Skip to content

Commit

Permalink
Added Grav\Framework\Cache classes providing PSR-16 Simple Cache
Browse files Browse the repository at this point in the history
…implementation
  • Loading branch information
mahagr committed May 11, 2017
1 parent 77c2e47 commit d1654a3
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 4 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
## mm/dd/2017

1. [](#new)
* Added `Grav\Framework\ContentBlock` classes which add better support for nested HTML blocks with assets
* Added `Grav\Framework\Object` classes to support general objects and their collections
* Added `Grav\Framework\Page` interfaces
* Added `Grav\Framework\Cache` classes providing PSR-16 `Simple Cache` implementation
* Added `Grav\Framework\ContentBlock` classes for nested HTML blocks with CSS/JS assets
* Added `Grav\Framework\Object` classes for creating collections of objects
* Added `Grav\Framework\Page` interfaces
* Deprecated GravTrait

# v1.2.5
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"symfony/polyfill-iconv": "~1.0",
"doctrine/cache": "~1.5",
"doctrine/collections": "1.3",
"psr/simple-cache": "^1.0",
"filp/whoops": "~2.0",
"matthiasmullie/minify": "^1.3",
"monolog/monolog": "~1.0",
Expand Down
50 changes: 49 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions system/src/Grav/Framework/Cache/AbstractCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @package Grav\Framework\Cache
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Framework\Cache;

use Grav\Framework\Cache\Exception\InvalidArgumentException;

/**
* Cache trait for PSR-16 compatible "Simple Cache" implementation
* @package Grav\Framework\Cache
*/
abstract class AbstractCache implements CacheInterface
{
use CacheTrait;

/**
* @param string $namespace
* @param null|int|\DateInterval $defaultLifetime
*/
public function __construct($namespace = '', $defaultLifetime = null)
{
$this->init($namespace, $defaultLifetime);
}
}
96 changes: 96 additions & 0 deletions system/src/Grav/Framework/Cache/Adapter/DoctrineCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* @package Grav\Framework\Cache
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Framework\Cache\Adapter;

use Doctrine\Common\Cache\CacheProvider;
use Grav\Framework\Cache\AbstractCache;

/**
* Cache class for PSR-16 compatible "Simple Cache" implementation using Doctrine Cache backend.
* @package Grav\Framework\Cache
*/
class DoctrineCache extends AbstractCache
{
/**
* @var CacheProvider
*/
protected $driver;

/**
* Doctrine Cache constructor.
*
* @param CacheProvider $doctrineCache
* @param string $namespace
* @param null|int|\DateInterval $defaultLifetime
*/
public function __construct(CacheProvider $doctrineCache, $namespace = '', $defaultLifetime = null)
{
// Do not use $namespace or $defaultLifetime directly, store them with constructor and fetch with methods.
parent::__construct($namespace, $defaultLifetime);

// Set namespace to Doctrine Cache provider if it was given.
$namespace = $this->getNamespace();
$namespace && $doctrineCache->setNamespace($namespace);

$this->driver = $doctrineCache;
}

protected function doGet($key, $default)
{
$value = $this->driver->fetch($key);

// Doctrine cache does not differentiate between no result and cached 'false'. Make sure that we do.
return $value !== false || $this->driver->contains($key) ? $value : $default;
}

protected function doSet($key, $value, $ttl)
{
return $this->driver->save($key, $value, (int) $ttl);
}

protected function doDelete($key)
{
return $this->driver->delete($key);
}

protected function doClear()
{
return $this->driver->deleteAll();
}

protected function doGetMultiple($keys, $default)
{
return $this->driver->fetchMultiple($keys);
}

protected function doSetMultiple($values, $ttl)
{
return $this->driver->saveMultiple($values, (int) $ttl);
}

protected function doDeleteMultiple($keys)
{
// TODO: Remove when Doctrine Cache has been updated to support the feature.
if (!method_exists($this->driver, 'deleteMultiple')) {
$success = true;
foreach ($keys as $key) {
$success = $this->delete($key) && $success;
}

return $success;
}

return $this->driver->deleteMultiple($keys);
}

protected function doHas($key)
{
return $this->driver->contains($key);
}
}
19 changes: 19 additions & 0 deletions system/src/Grav/Framework/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @package Grav\Framework\Cache
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Framework\Cache;

use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;

/**
* PSR-16 compatible "Simple Cache" interface.
* @package Grav\Framework\Object\Storage
*/
interface CacheInterface extends SimpleCacheInterface
{
}
Loading

0 comments on commit d1654a3

Please sign in to comment.