Replace cache to Symfony/Cache component.

This commit is contained in:
Dmitry Khomutov 2018-02-07 21:33:22 +07:00
commit 1dc8acd263
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
14 changed files with 255 additions and 360 deletions

View file

@ -1,31 +0,0 @@
<?php
namespace b8;
class Cache
{
const TYPE_APC = 'ApcCache';
const TYPE_REQUEST = 'RequestCache';
/**
* @var array
*/
protected static $instance = [];
/**
* Get a cache object of a specified type.
*
* @param string $type
*
* @return mixed
*/
public static function getCache($type = self::TYPE_REQUEST)
{
if (!isset(self::$instance[$type])) {
$class = '\\b8\\Cache\\' . $type;
self::$instance[$type] = new $class();
}
return self::$instance[$type];
}
}

View file

@ -1,149 +0,0 @@
<?php
namespace b8\Cache;
use b8\Type;
class ApcCache implements Type\CacheInterface
{
/**
* Check if caching is enabled.
*
* @return boolean
*/
public function isEnabled()
{
$rtn = false;
$apcCli = ini_get('apc.enable_cli');
if (function_exists('apc_fetch') && (php_sapi_name() != 'cli' || in_array($apcCli, ['1', 1, true, 'On']))) {
$rtn = true;
}
return $rtn;
}
/**
* Get item from the cache:
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null)
{
if (!$this->isEnabled()) {
return $default;
}
$success = false;
$rtn = apc_fetch($key, $success);
if (!$success) {
$rtn = $default;
}
return $rtn;
}
/**
* Add an item to the cache:
*
* @param string $key
* @param mixed $value
* @param integer $ttl
*
* @return array|bool
*/
public function set($key, $value = null, $ttl = 0)
{
if (!$this->isEnabled()) {
return false;
}
return apc_store($key, $value, $ttl);
}
/**
* Remove an item from the cache:
*
* @param string $key
*
* @return bool|string[]
*/
public function delete($key)
{
if (!$this->isEnabled()) {
return false;
}
return apc_delete($key);
}
/**
* Check if an item is in the cache:
*
* @param string $key
*
* @return bool|string[]
*/
public function contains($key)
{
if (!$this->isEnabled()) {
return false;
}
return apc_exists($key);
}
/**
* Short-hand syntax for get()
*
* @see Config::get()
*
* @param string $key
*
* @return mixed
*/
public function __get($key)
{
return $this->get($key, null);
}
/**
* Short-hand syntax for set()
*
* @see Config::set()
*
* @param string $key
* @param mixed $value
*
* @return array|bool
*/
public function __set($key, $value = null)
{
return $this->set($key, $value);
}
/**
* Is set
*
* @param string $key
*
* @return bool|string[]
*/
public function __isset($key)
{
return $this->contains($key);
}
/**
* Unset
*
* @param string $key
*/
public function __unset($key)
{
$this->delete($key);
}
}

View file

@ -1,90 +0,0 @@
<?php
namespace b8\Cache;
use b8\Type;
class RequestCache implements Type\CacheInterface
{
protected $data = [];
/**
* Check if caching is enabled.
*/
public function isEnabled()
{
return true;
}
/**
* Get item from the cache:
*/
public function get($key, $default = null)
{
return $this->contains($key) ? $this->data[$key] : $default;
}
/**
* Add an item to the cache:
*/
public function set($key, $value = null, $ttl = 0)
{
$this->data[$key] = $value;
return $this;
}
/**
* Remove an item from the cache:
*/
public function delete($key)
{
if ($this->contains($key)) {
unset($this->data[$key]);
}
return $this;
}
/**
* Check if an item is in the cache:
*/
public function contains($key)
{
return array_key_exists($key, $this->data);
}
/**
* Short-hand syntax for get()
* @see Config::get()
*/
public function __get($key)
{
return $this->get($key, null);
}
/**
* Short-hand syntax for set()
* @see Config::set()
*/
public function __set($key, $value = null)
{
return $this->set($key, $value);
}
/**
* Is set
*/
public function __isset($key)
{
return $this->contains($key);
}
/**
* Unset
*/
public function __unset($key)
{
$this->delete($key);
}
}

View file

@ -3,6 +3,7 @@
namespace b8;
use b8\Exception\HttpException;
use Symfony\Component\Cache\Simple\ArrayCache;
class Model
{
@ -23,7 +24,7 @@ class Model
$this->data = array_merge($this->data, $initialData);
}
$this->cache = Cache::getCache(Cache::TYPE_REQUEST);
$this->cache = new ArrayCache();
}
/**

View file

@ -1,24 +0,0 @@
<?php
namespace b8\Type;
interface CacheInterface
{
public function get($key, $default = null);
public function set($key, $value = null, $ttl = 0);
public function delete($key);
public function contains($key);
public function isEnabled();
public function __get($key);
public function __set($key, $value = null);
public function __unset($key);
public function __isset($key);
}

View file

@ -2,9 +2,9 @@
namespace PHPCensor\Helper;
use b8\Cache;
use b8\Config;
use GuzzleHttp\Client;
use Symfony\Component\Cache\Simple\FilesystemCache;
/**
* The Github Helper class provides some Github API call functionality.
@ -26,10 +26,10 @@ class Github
$response = $client->get(('https://api.github.com' . $url), [
'query' => $params,
]);
$body = json_decode($response->getBody(), true);
$headers = $response->getHeaders();
foreach ($body as $item) {
$results[] = $item;
}
@ -62,8 +62,8 @@ class Github
return [];
}
$cache = Cache::getCache(Cache::TYPE_APC);
$rtn = $cache->get('php-censor-github-repos');
$cache = new FilesystemCache('', 0, RUNTIME_DIR . 'cache');
$rtn = $cache->get('php-censor.github-repos');
if (!$rtn) {
$client = new Client();
@ -90,7 +90,7 @@ class Github
}
}
$cache->set('php-censor-github-repos', $rtn);
$cache->set('php-censor.github-repos', $rtn, 3600);
}
return $rtn;

View file

@ -397,8 +397,8 @@ class BuildError extends Model
return null;
}
$cacheKey = 'Cache.Build.' . $key;
$rtn = $this->cache->get($cacheKey, null);
$cacheKey = 'php-censor.build-' . $key;
$rtn = $this->cache->get($cacheKey);
if (empty($rtn)) {
$rtn = Factory::getStore('Build', 'PHPCensor')->getById($key);

View file

@ -181,8 +181,8 @@ class BuildMeta extends Model
return null;
}
$cacheKey = 'Cache.Build.' . $key;
$rtn = $this->cache->get($cacheKey, null);
$cacheKey = 'php-censor.build-' . $key;
$rtn = $this->cache->get($cacheKey);
if (empty($rtn)) {
$rtn = Factory::getStore('Build', 'PHPCensor')->getById($key);

View file

@ -453,8 +453,8 @@ class Project extends Model
return null;
}
$cacheKey = 'Cache.ProjectGroup.' . $key;
$rtn = $this->cache->get($cacheKey, null);
$cacheKey = 'php-censor.project-group-' . $key;
$rtn = $this->cache->get($cacheKey);
if (empty($rtn)) {
$rtn = Factory::getStore('ProjectGroup', 'PHPCensor')->getById($key);
@ -741,8 +741,8 @@ class Project extends Model
return null;
}
$cacheKey = 'Cache.ProjectEnvironments.' . $key;
$rtn = $this->cache->get($cacheKey, null);
$cacheKey = 'php-censor.project-environments-' . $key;
$rtn = $this->cache->get($cacheKey);
if (empty($rtn)) {
$store = $this->getEnvironmentStore();