Add FileCache driver
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Cache;
|
||||
|
||||
/**
|
||||
* Base class for cache drivers
|
||||
*
|
||||
* @package cache
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
abstract class Base
|
||||
{
|
||||
/**
|
||||
* Proxy cache
|
||||
*
|
||||
* Note: Arguments must be scalar types
|
||||
*
|
||||
* @access public
|
||||
* @param string $class Class instance
|
||||
* @param string $method Container method
|
||||
* @return mixed
|
||||
*/
|
||||
public function proxy($class, $method)
|
||||
{
|
||||
$args = func_get_args();
|
||||
array_shift($args);
|
||||
|
||||
$key = 'proxy:'.get_class($class).':'.implode(':', $args);
|
||||
$result = $this->get($key);
|
||||
|
||||
if ($result === null) {
|
||||
$result = call_user_func_array(array($class, $method), array_splice($args, 1));
|
||||
$this->set($key, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
71
app/Core/Cache/BaseCache.php
Normal file
71
app/Core/Cache/BaseCache.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Cache;
|
||||
|
||||
/**
|
||||
* Base Class for Cache Drivers
|
||||
*
|
||||
* @package Kanboard\Core\Cache
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
abstract class BaseCache
|
||||
{
|
||||
/**
|
||||
* Store an item in the cache
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*/
|
||||
abstract public function set($key, $value);
|
||||
|
||||
/**
|
||||
* Retrieve an item from the cache by key
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return mixed Null when not found, cached value otherwise
|
||||
*/
|
||||
abstract public function get($key);
|
||||
|
||||
/**
|
||||
* Remove all items from the cache
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
abstract public function flush();
|
||||
|
||||
/**
|
||||
* Remove an item from the cache
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
*/
|
||||
abstract public function remove($key);
|
||||
|
||||
/**
|
||||
* Proxy cache
|
||||
*
|
||||
* Note: Arguments must be scalar types
|
||||
*
|
||||
* @access public
|
||||
* @param string $class Class instance
|
||||
* @param string $method Container method
|
||||
* @return mixed
|
||||
*/
|
||||
public function proxy($class, $method)
|
||||
{
|
||||
$args = func_get_args();
|
||||
array_shift($args);
|
||||
|
||||
$key = 'proxy:'.get_class($class).':'.implode(':', $args);
|
||||
$result = $this->get($key);
|
||||
|
||||
if ($result === null) {
|
||||
$result = call_user_func_array(array($class, $method), array_splice($args, 1));
|
||||
$this->set($key, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Cache;
|
||||
|
||||
/**
|
||||
* Cache Interface
|
||||
*
|
||||
* @package cache
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
interface CacheInterface
|
||||
{
|
||||
/**
|
||||
* Save a new value in the cache
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*/
|
||||
public function set($key, $value);
|
||||
|
||||
/**
|
||||
* Fetch value from cache
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return mixed Null when not found, cached value otherwise
|
||||
*/
|
||||
public function get($key);
|
||||
|
||||
/**
|
||||
* Clear all cache
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function flush();
|
||||
|
||||
/**
|
||||
* Remove cached value
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
*/
|
||||
public function remove($key);
|
||||
}
|
||||
98
app/Core/Cache/FileCache.php
Normal file
98
app/Core/Cache/FileCache.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Cache;
|
||||
|
||||
use Kanboard\Core\Tool;
|
||||
use LogicException;
|
||||
|
||||
/**
|
||||
* Class FileCache
|
||||
*
|
||||
* @package Kanboard\Core\Cache
|
||||
*/
|
||||
class FileCache extends BaseCache
|
||||
{
|
||||
/**
|
||||
* Store an item in the cache
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->createCacheFolder();
|
||||
file_put_contents($this->getFilenameFromKey($key), serialize($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an item from the cache by key
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return mixed Null when not found, cached value otherwise
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$filename = $this->getFilenameFromKey($key);
|
||||
|
||||
if (file_exists($filename)) {
|
||||
return unserialize(file_get_contents($filename));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all items from the cache
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
$this->createCacheFolder();
|
||||
Tool::removeAllFiles(CACHE_DIR, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an item from the cache
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
*/
|
||||
public function remove($key)
|
||||
{
|
||||
$filename = $this->getFilenameFromKey($key);
|
||||
|
||||
if (file_exists($filename)) {
|
||||
unlink($filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get absolute filename from the key
|
||||
*
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function getFilenameFromKey($key)
|
||||
{
|
||||
return CACHE_DIR.DIRECTORY_SEPARATOR.$key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create cache folder if missing
|
||||
*
|
||||
* @access protected
|
||||
* @throws LogicException
|
||||
*/
|
||||
protected function createCacheFolder()
|
||||
{
|
||||
if (! is_dir(CACHE_DIR)) {
|
||||
if (! mkdir(CACHE_DIR, 0755)) {
|
||||
throw new LogicException('Unable to create cache directory: '.CACHE_DIR);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,12 @@
|
||||
namespace Kanboard\Core\Cache;
|
||||
|
||||
/**
|
||||
* Memory Cache
|
||||
* Memory Cache Driver
|
||||
*
|
||||
* @package cache
|
||||
* @package Kanboard\Core\Cache
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class MemoryCache extends Base implements CacheInterface
|
||||
class MemoryCache extends BaseCache
|
||||
{
|
||||
/**
|
||||
* Container
|
||||
@@ -19,7 +19,7 @@ class MemoryCache extends Base implements CacheInterface
|
||||
private $storage = array();
|
||||
|
||||
/**
|
||||
* Save a new value in the cache
|
||||
* Store an item in the cache
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
@@ -31,7 +31,7 @@ class MemoryCache extends Base implements CacheInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch value from cache
|
||||
* Retrieve an item from the cache by key
|
||||
*
|
||||
* @access public
|
||||
* @param string $key
|
||||
|
||||
Reference in New Issue
Block a user