diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/Core/Cache.php | 58 | ||||
-rw-r--r-- | app/Core/Cache/Base.php | 38 | ||||
-rw-r--r-- | app/Core/Cache/CacheInterface.php | 45 | ||||
-rw-r--r-- | app/Core/Cache/MemoryCache.php | 65 | ||||
-rw-r--r-- | app/Core/MemoryCache.php | 32 | ||||
-rw-r--r-- | app/Helper/User.php | 4 | ||||
-rw-r--r-- | app/ServiceProvider/ClassProvider.php | 4 |
7 files changed, 153 insertions, 93 deletions
diff --git a/app/Core/Cache.php b/app/Core/Cache.php deleted file mode 100644 index 670a76e0..00000000 --- a/app/Core/Cache.php +++ /dev/null @@ -1,58 +0,0 @@ -<?php - -namespace Core; - -use Pimple\Container; - -abstract class Cache -{ - /** - * Container instance - * - * @access protected - * @var \Pimple\Container - */ - protected $container; - - abstract public function init(); - abstract public function set($key, $value); - abstract public function get($key); - abstract public function flush(); - abstract public function remove($key); - - /** - * Constructor - * - * @access public - * @param \Pimple\Container $container - */ - public function __construct(Container $container) - { - $this->container = $container; - $this->init(); - } - - /** - * Proxy cache - * - * Note: Arguments must be scalar types - * - * @access public - * @param string $container Container name - * @param string $method Container method - * @return mixed - */ - public function proxy($container, $method) - { - $args = func_get_args(); - $key = 'proxy_'.implode('_', $args); - $result = $this->get($key); - - if ($result === null) { - $result = call_user_func_array(array($this->container[$container], $method), array_splice($args, 2)); - $this->set($key, $result); - } - - return $result; - } -} diff --git a/app/Core/Cache/Base.php b/app/Core/Cache/Base.php new file mode 100644 index 00000000..a16c12f0 --- /dev/null +++ b/app/Core/Cache/Base.php @@ -0,0 +1,38 @@ +<?php + +namespace 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; + } +} diff --git a/app/Core/Cache/CacheInterface.php b/app/Core/Cache/CacheInterface.php new file mode 100644 index 00000000..8675ef8e --- /dev/null +++ b/app/Core/Cache/CacheInterface.php @@ -0,0 +1,45 @@ +<?php + +namespace 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); +} diff --git a/app/Core/Cache/MemoryCache.php b/app/Core/Cache/MemoryCache.php new file mode 100644 index 00000000..b1de96bc --- /dev/null +++ b/app/Core/Cache/MemoryCache.php @@ -0,0 +1,65 @@ +<?php + +namespace Core\Cache; + +/** + * Memory Cache + * + * @package cache + * @author Frederic Guillot + */ +class MemoryCache extends Base implements CacheInterface +{ + /** + * Container + * + * @access private + * @var array + */ + private $storage = array(); + + /** + * Save a new value in the cache + * + * @access public + * @param string $key + * @param string $value + */ + public function set($key, $value) + { + $this->storage[$key] = $value; + } + + /** + * Fetch value from cache + * + * @access public + * @param string $key + * @return mixed Null when not found, cached value otherwise + */ + public function get($key) + { + return isset($this->storage[$key]) ? $this->storage[$key] : null; + } + + /** + * Clear all cache + * + * @access public + */ + public function flush() + { + $this->storage = array(); + } + + /** + * Remove cached value + * + * @access public + * @param string $key + */ + public function remove($key) + { + unset($this->storage[$key]); + } +} diff --git a/app/Core/MemoryCache.php b/app/Core/MemoryCache.php deleted file mode 100644 index f80a66ef..00000000 --- a/app/Core/MemoryCache.php +++ /dev/null @@ -1,32 +0,0 @@ -<?php - -namespace Core; - -class MemoryCache extends Cache -{ - private $storage = array(); - - public function init() - { - } - - public function set($key, $value) - { - $this->storage[$key] = $value; - } - - public function get($key) - { - return isset($this->storage[$key]) ? $this->storage[$key] : null; - } - - public function flush() - { - $this->storage = array(); - } - - public function remove($key) - { - unset($this->storage[$key]); - } -} diff --git a/app/Helper/User.php b/app/Helper/User.php index cb596fb0..3108a79a 100644 --- a/app/Helper/User.php +++ b/app/Helper/User.php @@ -99,7 +99,7 @@ class User extends \Core\Base return true; } - return $this->memoryCache->proxy('acl', 'handleProjectAdminPermissions', $project_id); + return $this->memoryCache->proxy($this->container['acl'], 'handleProjectAdminPermissions', $project_id); } /** @@ -114,7 +114,7 @@ class User extends \Core\Base return true; } - return $this->memoryCache->proxy('acl', 'handleProjectManagerPermissions', $project_id); + return $this->memoryCache->proxy($this->container['acl'], 'handleProjectManagerPermissions', $project_id); } /** diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index a5677948..899574e9 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -72,12 +72,14 @@ class ClassProvider implements ServiceProviderInterface 'Helper', 'HttpClient', 'Lexer', - 'MemoryCache', 'Request', 'Router', 'Session', 'Template', ), + 'Core\Cache' => array( + 'MemoryCache', + ), 'Integration' => array( 'BitbucketWebhook', 'GithubWebhook', |