First Local Commit - After Clean up.
Signed-off-by: Rick Hays <rhays@haysgang.com>
This commit is contained in:
117
system/Cache/CacheFactory.php
Normal file
117
system/Cache/CacheFactory.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Cache;
|
||||
|
||||
use CodeIgniter\Cache\Exceptions\CacheException;
|
||||
use CodeIgniter\Exceptions\CriticalError;
|
||||
|
||||
/**
|
||||
* Class Cache
|
||||
*
|
||||
* A factory for loading the desired
|
||||
*
|
||||
* @package CodeIgniter\Cache
|
||||
*/
|
||||
class CacheFactory
|
||||
{
|
||||
/**
|
||||
* Attempts to create the desired cache handler, based upon the
|
||||
*
|
||||
* @param $config
|
||||
* @param string $handler
|
||||
* @param string $backup
|
||||
*
|
||||
* @return \CodeIgniter\Cache\CacheInterface
|
||||
*/
|
||||
public static function getHandler($config, string $handler = null, string $backup = null)
|
||||
{
|
||||
if (! isset($config->validHandlers) || ! is_array($config->validHandlers))
|
||||
{
|
||||
throw CacheException::forInvalidHandlers();
|
||||
}
|
||||
|
||||
if (! isset($config->handler) || ! isset($config->backupHandler))
|
||||
{
|
||||
throw CacheException::forNoBackup();
|
||||
}
|
||||
|
||||
$handler = ! empty($handler) ? $handler : $config->handler;
|
||||
$backup = ! empty($backup) ? $backup : $config->backupHandler;
|
||||
|
||||
if (! array_key_exists($handler, $config->validHandlers) || ! array_key_exists($backup, $config->validHandlers))
|
||||
{
|
||||
throw CacheException::forHandlerNotFound();
|
||||
}
|
||||
|
||||
// Get an instance of our handler.
|
||||
$adapter = new $config->validHandlers[$handler]($config);
|
||||
|
||||
if (! $adapter->isSupported())
|
||||
{
|
||||
$adapter = new $config->validHandlers[$backup]($config);
|
||||
|
||||
if (! $adapter->isSupported())
|
||||
{
|
||||
// Log stuff here, don't throw exception. No need to raise a fuss.
|
||||
// Fall back to the dummy adapter.
|
||||
$adapter = new $config->validHandlers['dummy']();
|
||||
}
|
||||
}
|
||||
|
||||
// If $adapter->initialization throws a CriticalError exception, we will attempt to
|
||||
// use the $backup handler, if that also fails, we resort to the dummy handler.
|
||||
try
|
||||
{
|
||||
$adapter->initialize();
|
||||
}
|
||||
catch (CriticalError $e)
|
||||
{
|
||||
// log the fact that an exception occurred as well what handler we are resorting to
|
||||
log_message('critical', $e->getMessage() . ' Resorting to using ' . $backup . ' handler.');
|
||||
|
||||
// get the next best cache handler (or dummy if the $backup also fails)
|
||||
$adapter = self::getHandler($config, $backup, 'dummy');
|
||||
}
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
||||
155
system/Cache/CacheInterface.php
Normal file
155
system/Cache/CacheInterface.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Cache;
|
||||
|
||||
/**
|
||||
* Cache interface
|
||||
*/
|
||||
|
||||
interface CacheInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Takes care of any handler-specific setup that must be done.
|
||||
*/
|
||||
public function initialize();
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attempts to fetch an item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key);
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Saves an item to the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
* @param mixed $value The data to save
|
||||
* @param integer $ttl Time To Live, in seconds (default 60)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(string $key, $value, int $ttl = 60);
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Deletes a specific item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(string $key);
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic incrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function increment(string $key, int $offset = 1);
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic decrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function decrement(string $key, int $offset = 1);
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Will delete all items in the entire cache.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function clean();
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns information on the entire cache.
|
||||
*
|
||||
* The information returned and the structure of the data
|
||||
* varies depending on the handler.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCacheInfo();
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns detailed information about the specific item in the cache.
|
||||
*
|
||||
* @param string $key Cache item name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMetaData(string $key);
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determines if the driver is supported on this system.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSupported(): bool;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
||||
36
system/Cache/Exceptions/CacheException.php
Normal file
36
system/Cache/Exceptions/CacheException.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php namespace CodeIgniter\Cache\Exceptions;
|
||||
|
||||
class CacheException extends \RuntimeException implements ExceptionInterface
|
||||
{
|
||||
/**
|
||||
* @return \CodeIgniter\Cache\Exceptions\CacheException
|
||||
*/
|
||||
public static function forUnableToWrite(string $path)
|
||||
{
|
||||
return new static(lang('Cache.unableToWrite', [$path]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \CodeIgniter\Cache\Exceptions\CacheException
|
||||
*/
|
||||
public static function forInvalidHandlers()
|
||||
{
|
||||
return new static(lang('Cache.invalidHandlers'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \CodeIgniter\Cache\Exceptions\CacheException
|
||||
*/
|
||||
public static function forNoBackup()
|
||||
{
|
||||
return new static(lang('Cache.noBackup'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \CodeIgniter\Cache\Exceptions\CacheException
|
||||
*/
|
||||
public static function forHandlerNotFound()
|
||||
{
|
||||
return new static(lang('Cache.handlerNotFound'));
|
||||
}
|
||||
}
|
||||
12
system/Cache/Exceptions/ExceptionInterface.php
Normal file
12
system/Cache/Exceptions/ExceptionInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php namespace CodeIgniter\Cache\Exceptions;
|
||||
|
||||
/**
|
||||
* Provides a domain-level interface for broad capture
|
||||
* of all framework-related exceptions.
|
||||
*
|
||||
* catch (\CodeIgniter\Cache\Exceptions\ExceptionInterface) { ... }
|
||||
*/
|
||||
|
||||
interface ExceptionInterface
|
||||
{
|
||||
}
|
||||
186
system/Cache/Handlers/DummyHandler.php
Normal file
186
system/Cache/Handlers/DummyHandler.php
Normal file
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Cache\Handlers;
|
||||
|
||||
use CodeIgniter\Cache\CacheInterface;
|
||||
|
||||
/**
|
||||
* Dummy cache handler
|
||||
*/
|
||||
|
||||
class DummyHandler implements CacheInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Takes care of any handler-specific setup that must be done.
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
// Nothing to see here...
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attempts to fetch an item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Saves an item to the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
* @param mixed $value The data to save
|
||||
* @param integer $ttl Time To Live, in seconds (default 60)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(string $key, $value, int $ttl = 60)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Deletes a specific item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(string $key)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic incrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function increment(string $key, int $offset = 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic decrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function decrement(string $key, int $offset = 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Will delete all items in the entire cache.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns information on the entire cache.
|
||||
*
|
||||
* The information returned and the structure of the data
|
||||
* varies depending on the handler.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCacheInfo()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns detailed information about the specific item in the cache.
|
||||
*
|
||||
* @param string $key Cache item name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMetaData(string $key)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determines if the driver is supported on this system.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
||||
531
system/Cache/Handlers/FileHandler.php
Normal file
531
system/Cache/Handlers/FileHandler.php
Normal file
@@ -0,0 +1,531 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Cache\Handlers;
|
||||
|
||||
use CodeIgniter\Cache\CacheInterface;
|
||||
use CodeIgniter\Cache\Exceptions\CacheException;
|
||||
|
||||
/**
|
||||
* File system cache handler
|
||||
*/
|
||||
class FileHandler implements CacheInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Prefixed to all cache names.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
* Where to store cached files on the disk.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $config
|
||||
* @throws type
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
$path = ! empty($config->storePath) ? $config->storePath : WRITEPATH . 'cache';
|
||||
if (! is_really_writable($path))
|
||||
{
|
||||
throw CacheException::forUnableToWrite($path);
|
||||
}
|
||||
|
||||
$this->prefix = $config->prefix ?: '';
|
||||
$this->path = rtrim($path, '/') . '/';
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Takes care of any handler-specific setup that must be done.
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
// Not to see here...
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attempts to fetch an item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$data = $this->getItem($key);
|
||||
|
||||
return is_array($data) ? $data['data'] : null;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Saves an item to the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
* @param mixed $value The data to save
|
||||
* @param integer $ttl Time To Live, in seconds (default 60)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(string $key, $value, int $ttl = 60)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$contents = [
|
||||
'time' => time(),
|
||||
'ttl' => $ttl,
|
||||
'data' => $value,
|
||||
];
|
||||
|
||||
if ($this->writeFile($this->path . $key, serialize($contents)))
|
||||
{
|
||||
chmod($this->path . $key, 0640);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Deletes a specific item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
return is_file($this->path . $key) ? unlink($this->path . $key) : false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic incrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function increment(string $key, int $offset = 1)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$data = $this->getItem($key);
|
||||
|
||||
if ($data === false)
|
||||
{
|
||||
$data = [
|
||||
'data' => 0,
|
||||
'ttl' => 60,
|
||||
];
|
||||
}
|
||||
elseif (! is_int($data['data']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$new_value = $data['data'] + $offset;
|
||||
|
||||
return $this->save($key, $new_value, $data['ttl']) ? $new_value : false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic decrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function decrement(string $key, int $offset = 1)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$data = $this->getItem($key);
|
||||
|
||||
if ($data === false)
|
||||
{
|
||||
$data = [
|
||||
'data' => 0,
|
||||
'ttl' => 60,
|
||||
];
|
||||
}
|
||||
elseif (! is_int($data['data']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$new_value = $data['data'] - $offset;
|
||||
|
||||
return $this->save($key, $new_value, $data['ttl']) ? $new_value : false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Will delete all items in the entire cache.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return $this->deleteFiles($this->path, false, true);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns information on the entire cache.
|
||||
*
|
||||
* The information returned and the structure of the data
|
||||
* varies depending on the handler.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCacheInfo()
|
||||
{
|
||||
return $this->getDirFileInfo($this->path);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns detailed information about the specific item in the cache.
|
||||
*
|
||||
* @param string $key Cache item name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMetaData(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
if (! is_file($this->path . $key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = @unserialize(file_get_contents($this->path . $key));
|
||||
|
||||
if (is_array($data))
|
||||
{
|
||||
$mtime = filemtime($this->path . $key);
|
||||
|
||||
if (! isset($data['ttl']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return [
|
||||
'expire' => $mtime + $data['ttl'],
|
||||
'mtime' => $mtime,
|
||||
'data' => $data['data'],
|
||||
];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determines if the driver is supported on this system.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSupported(): bool
|
||||
{
|
||||
return is_writable($this->path);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Does the heavy lifting of actually retrieving the file and
|
||||
* verifying it's age.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return boolean|mixed
|
||||
*/
|
||||
protected function getItem(string $key)
|
||||
{
|
||||
if (! is_file($this->path . $key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = unserialize(file_get_contents($this->path . $key));
|
||||
|
||||
if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
|
||||
{
|
||||
unlink($this->path . $key);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
//--------------------------------------------------------------------
|
||||
// SUPPORT METHODS FOR FILES
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Writes a file to disk, or returns false if not successful.
|
||||
*
|
||||
* @param $path
|
||||
* @param $data
|
||||
* @param string $mode
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function writeFile($path, $data, $mode = 'wb')
|
||||
{
|
||||
if (($fp = @fopen($path, $mode)) === false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
flock($fp, LOCK_EX);
|
||||
|
||||
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
|
||||
{
|
||||
if (($result = fwrite($fp, substr($data, $written))) === false)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
flock($fp, LOCK_UN);
|
||||
fclose($fp);
|
||||
|
||||
return is_int($result);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Delete Files
|
||||
*
|
||||
* Deletes all files contained in the supplied directory path.
|
||||
* Files must be writable or owned by the system in order to be deleted.
|
||||
* If the second parameter is set to TRUE, any directories contained
|
||||
* within the supplied base directory will be nuked as well.
|
||||
*
|
||||
* @param string $path File path
|
||||
* @param boolean $del_dir Whether to delete any directories found in the path
|
||||
* @param boolean $htdocs Whether to skip deleting .htaccess and index page files
|
||||
* @param integer $_level Current directory depth level (default: 0; internal use only)
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function deleteFiles(string $path, bool $del_dir = false, bool $htdocs = false, int $_level = 0): bool
|
||||
{
|
||||
// Trim the trailing slash
|
||||
$path = rtrim($path, '/\\');
|
||||
|
||||
if (! $current_dir = @opendir($path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
while (false !== ($filename = @readdir($current_dir)))
|
||||
{
|
||||
if ($filename !== '.' && $filename !== '..')
|
||||
{
|
||||
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
|
||||
{
|
||||
$this->deleteFiles($path . DIRECTORY_SEPARATOR . $filename, $del_dir, $htdocs, $_level + 1);
|
||||
}
|
||||
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
|
||||
{
|
||||
@unlink($path . DIRECTORY_SEPARATOR . $filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir($current_dir);
|
||||
|
||||
return ($del_dir === true && $_level > 0) ? @rmdir($path) : true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get Directory File Information
|
||||
*
|
||||
* Reads the specified directory and builds an array containing the filenames,
|
||||
* filesize, dates, and permissions
|
||||
*
|
||||
* Any sub-folders contained within the specified path are read as well.
|
||||
*
|
||||
* @param string $source_dir Path to source
|
||||
* @param boolean $top_level_only Look only at the top level directory specified?
|
||||
* @param boolean $_recursion Internal variable to determine recursion status - do not use in calls
|
||||
*
|
||||
* @return array|false
|
||||
*/
|
||||
protected function getDirFileInfo(string $source_dir, bool $top_level_only = true, bool $_recursion = false)
|
||||
{
|
||||
static $_filedata = [];
|
||||
$relative_path = $source_dir;
|
||||
|
||||
if ($fp = @opendir($source_dir))
|
||||
{
|
||||
// reset the array and make sure $source_dir has a trailing slash on the initial call
|
||||
if ($_recursion === false)
|
||||
{
|
||||
$_filedata = [];
|
||||
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
|
||||
while (false !== ($file = readdir($fp)))
|
||||
{
|
||||
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
|
||||
{
|
||||
$this->getDirFileInfo($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
|
||||
}
|
||||
elseif ($file[0] !== '.')
|
||||
{
|
||||
$_filedata[$file] = $this->getFileInfo($source_dir . $file);
|
||||
$_filedata[$file]['relative_path'] = $relative_path;
|
||||
}
|
||||
}
|
||||
|
||||
closedir($fp);
|
||||
|
||||
return $_filedata;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get File Info
|
||||
*
|
||||
* Given a file and path, returns the name, path, size, date modified
|
||||
* Second parameter allows you to explicitly declare what information you want returned
|
||||
* Options are: name, server_path, size, date, readable, writable, executable, fileperms
|
||||
* Returns FALSE if the file cannot be found.
|
||||
*
|
||||
* @param string $file Path to file
|
||||
* @param mixed $returned_values Array or comma separated string of information returned
|
||||
*
|
||||
* @return array|false
|
||||
*/
|
||||
protected function getFileInfo(string $file, $returned_values = ['name', 'server_path', 'size', 'date'])
|
||||
{
|
||||
if (! is_file($file))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_string($returned_values))
|
||||
{
|
||||
$returned_values = explode(',', $returned_values);
|
||||
}
|
||||
|
||||
foreach ($returned_values as $key)
|
||||
{
|
||||
switch ($key)
|
||||
{
|
||||
case 'name':
|
||||
$fileInfo['name'] = basename($file);
|
||||
break;
|
||||
case 'server_path':
|
||||
$fileInfo['server_path'] = $file;
|
||||
break;
|
||||
case 'size':
|
||||
$fileInfo['size'] = filesize($file);
|
||||
break;
|
||||
case 'date':
|
||||
$fileInfo['date'] = filemtime($file);
|
||||
break;
|
||||
case 'readable':
|
||||
$fileInfo['readable'] = is_readable($file);
|
||||
break;
|
||||
case 'writable':
|
||||
$fileInfo['writable'] = is_writable($file);
|
||||
break;
|
||||
case 'executable':
|
||||
$fileInfo['executable'] = is_executable($file);
|
||||
break;
|
||||
case 'fileperms':
|
||||
$fileInfo['fileperms'] = fileperms($file);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $fileInfo;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
||||
389
system/Cache/Handlers/MemcachedHandler.php
Normal file
389
system/Cache/Handlers/MemcachedHandler.php
Normal file
@@ -0,0 +1,389 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Cache\Handlers;
|
||||
|
||||
use CodeIgniter\Cache\CacheInterface;
|
||||
use CodeIgniter\Exceptions\CriticalError;
|
||||
|
||||
/**
|
||||
* Mamcached cache handler
|
||||
*/
|
||||
class MemcachedHandler implements CacheInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Prefixed to all cache names.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
* The memcached object
|
||||
*
|
||||
* @var \Memcached|\Memcache
|
||||
*/
|
||||
protected $memcached;
|
||||
|
||||
/**
|
||||
* Memcached Configuration
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 11211,
|
||||
'weight' => 1,
|
||||
'raw' => false,
|
||||
];
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $config
|
||||
* @throws type
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
$config = (array)$config;
|
||||
$this->prefix = $config['prefix'] ?? '';
|
||||
|
||||
if (! empty($config))
|
||||
{
|
||||
$this->config = array_merge($this->config, $config['memcached']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class destructor
|
||||
*
|
||||
* Closes the connection to Memcache(d) if present.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->memcached instanceof \Memcached)
|
||||
{
|
||||
$this->memcached->quit();
|
||||
}
|
||||
elseif ($this->memcached instanceof \Memcache)
|
||||
{
|
||||
$this->memcached->close();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Takes care of any handler-specific setup that must be done.
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
// Try to connect to Memcache or Memcached, if an issue occurs throw a CriticalError exception,
|
||||
// so that the CacheFactory can attempt to initiate the next cache handler.
|
||||
try
|
||||
{
|
||||
if (class_exists('\Memcached'))
|
||||
{
|
||||
// Create new instance of \Memcached
|
||||
$this->memcached = new \Memcached();
|
||||
if ($this->config['raw'])
|
||||
{
|
||||
$this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
|
||||
}
|
||||
|
||||
// Add server
|
||||
$this->memcached->addServer(
|
||||
$this->config['host'], $this->config['port'], $this->config['weight']
|
||||
);
|
||||
|
||||
// attempt to get status of servers
|
||||
$stats = $this->memcached->getStats();
|
||||
|
||||
// $stats should be an associate array with a key in the format of host:port.
|
||||
// If it doesn't have the key, we know the server is not working as expected.
|
||||
if (! isset($stats[$this->config['host'] . ':' . $this->config['port']]))
|
||||
{
|
||||
throw new CriticalError('Cache: Memcached connection failed.');
|
||||
}
|
||||
}
|
||||
elseif (class_exists('\Memcache'))
|
||||
{
|
||||
// Create new instance of \Memcache
|
||||
$this->memcached = new \Memcache();
|
||||
|
||||
// Check if we can connect to the server
|
||||
$can_connect = $this->memcached->connect(
|
||||
$this->config['host'], $this->config['port']
|
||||
);
|
||||
|
||||
// If we can't connect, throw a CriticalError exception
|
||||
if ($can_connect === false)
|
||||
{
|
||||
throw new CriticalError('Cache: Memcache connection failed.');
|
||||
}
|
||||
|
||||
// Add server, third parameter is persistence and defaults to TRUE.
|
||||
$this->memcached->addServer(
|
||||
$this->config['host'], $this->config['port'], true, $this->config['weight']
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new CriticalError('Cache: Not support Memcache(d) extension.');
|
||||
}
|
||||
}
|
||||
catch (CriticalError $e)
|
||||
{
|
||||
// If a CriticalError exception occurs, throw it up.
|
||||
throw $e;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
// If an \Exception occurs, convert it into a CriticalError exception and throw it.
|
||||
throw new CriticalError('Cache: Memcache(d) connection refused (' . $e->getMessage() . ').');
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attempts to fetch an item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
if ($this->memcached instanceof \Memcached)
|
||||
{
|
||||
$data = $this->memcached->get($key);
|
||||
|
||||
// check for unmatched key
|
||||
if ($this->memcached->getResultCode() === \Memcached::RES_NOTFOUND)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
elseif ($this->memcached instanceof \Memcache)
|
||||
{
|
||||
$flags = false;
|
||||
$data = $this->memcached->get($key, $flags);
|
||||
|
||||
// check for unmatched key (i.e. $flags is untouched)
|
||||
if ($flags === false)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return is_array($data) ? $data[0] : $data;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Saves an item to the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
* @param mixed $value The data to save
|
||||
* @param integer $ttl Time To Live, in seconds (default 60)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(string $key, $value, int $ttl = 60)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
if (! $this->config['raw'])
|
||||
{
|
||||
$value = [
|
||||
$value,
|
||||
time(),
|
||||
$ttl,
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->memcached instanceof \Memcached)
|
||||
{
|
||||
return $this->memcached->set($key, $value, $ttl);
|
||||
}
|
||||
elseif ($this->memcached instanceof \Memcache)
|
||||
{
|
||||
return $this->memcached->set($key, $value, 0, $ttl);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Deletes a specific item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
return $this->memcached->delete($key);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic incrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function increment(string $key, int $offset = 1)
|
||||
{
|
||||
if (! $this->config['raw'])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
return $this->memcached->increment($key, $offset, $offset, 60);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic decrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function decrement(string $key, int $offset = 1)
|
||||
{
|
||||
if (! $this->config['raw'])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
//FIXME: third parameter isn't other handler actions.
|
||||
return $this->memcached->decrement($key, $offset, $offset, 60);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Will delete all items in the entire cache.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return $this->memcached->flush();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns information on the entire cache.
|
||||
*
|
||||
* The information returned and the structure of the data
|
||||
* varies depending on the handler.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCacheInfo()
|
||||
{
|
||||
return $this->memcached->getStats();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns detailed information about the specific item in the cache.
|
||||
*
|
||||
* @param string $key Cache item name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMetaData(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$stored = $this->memcached->get($key);
|
||||
|
||||
// if not an array, don't try to count for PHP7.2
|
||||
if (! is_array($stored) || count($stored) !== 3)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
list($data, $time, $ttl) = $stored;
|
||||
|
||||
return [
|
||||
'expire' => $time + $ttl,
|
||||
'mtime' => $time,
|
||||
'data' => $data,
|
||||
];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determines if the driver is supported on this system.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSupported(): bool
|
||||
{
|
||||
return (extension_loaded('memcached') || extension_loaded('memcache'));
|
||||
}
|
||||
|
||||
}
|
||||
306
system/Cache/Handlers/PredisHandler.php
Normal file
306
system/Cache/Handlers/PredisHandler.php
Normal file
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Cache\Handlers;
|
||||
|
||||
use CodeIgniter\Cache\CacheInterface;
|
||||
use CodeIgniter\Exceptions\CriticalError;
|
||||
|
||||
/**
|
||||
* Predis cache handler
|
||||
*/
|
||||
class PredisHandler implements CacheInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Prefixed to all cache names.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
* Default config
|
||||
*
|
||||
* @static
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [
|
||||
'scheme' => 'tcp',
|
||||
'host' => '127.0.0.1',
|
||||
'password' => null,
|
||||
'port' => 6379,
|
||||
'timeout' => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* Predis connection
|
||||
*
|
||||
* @var Predis
|
||||
*/
|
||||
protected $redis;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $config
|
||||
* @throws type
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->prefix = $config->prefix ?: '';
|
||||
|
||||
if (isset($config->redis))
|
||||
{
|
||||
$this->config = array_merge($this->config, $config->redis);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Takes care of any handler-specific setup that must be done.
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
// Try to connect to Redis, if an issue occurs throw a CriticalError exception,
|
||||
// so that the CacheFactory can attempt to initiate the next cache handler.
|
||||
try
|
||||
{
|
||||
// Create a new instance of Predis\Client
|
||||
$this->redis = new \Predis\Client($this->config, ['prefix' => $this->prefix]);
|
||||
|
||||
// Check if the connection is valid by trying to get the time.
|
||||
$this->redis->time();
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
// thrown if can't connect to redis server.
|
||||
throw new CriticalError('Cache: Predis connection refused (' . $e->getMessage() . ').');
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attempts to fetch an item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
$data = array_combine([
|
||||
'__ci_type',
|
||||
'__ci_value',
|
||||
], $this->redis->hmget($key, ['__ci_type', '__ci_value'])
|
||||
);
|
||||
|
||||
if (! isset($data['__ci_type'], $data['__ci_value']) || $data['__ci_value'] === false)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
switch ($data['__ci_type'])
|
||||
{
|
||||
case 'array':
|
||||
case 'object':
|
||||
return unserialize($data['__ci_value']);
|
||||
case 'boolean':
|
||||
case 'integer':
|
||||
case 'double': // Yes, 'double' is returned and NOT 'float'
|
||||
case 'string':
|
||||
case 'NULL':
|
||||
return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null;
|
||||
case 'resource':
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Saves an item to the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
* @param mixed $value The data to save
|
||||
* @param integer $ttl Time To Live, in seconds (default 60)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(string $key, $value, int $ttl = 60)
|
||||
{
|
||||
switch ($data_type = gettype($value))
|
||||
{
|
||||
case 'array':
|
||||
case 'object':
|
||||
$value = serialize($value);
|
||||
break;
|
||||
case 'boolean':
|
||||
case 'integer':
|
||||
case 'double': // Yes, 'double' is returned and NOT 'float'
|
||||
case 'string':
|
||||
case 'NULL':
|
||||
break;
|
||||
case 'resource':
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->redis->hmset($key, ['__ci_type' => $data_type, '__ci_value' => $value]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->redis->expireat($key, time() + $ttl);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Deletes a specific item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(string $key)
|
||||
{
|
||||
return ($this->redis->del($key) === 1);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic incrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function increment(string $key, int $offset = 1)
|
||||
{
|
||||
return $this->redis->hincrby($key, 'data', $offset);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic decrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function decrement(string $key, int $offset = 1)
|
||||
{
|
||||
return $this->redis->hincrby($key, 'data', -$offset);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Will delete all items in the entire cache.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return $this->redis->flushdb();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns information on the entire cache.
|
||||
*
|
||||
* The information returned and the structure of the data
|
||||
* varies depending on the handler.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCacheInfo()
|
||||
{
|
||||
return $this->redis->info();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns detailed information about the specific item in the cache.
|
||||
*
|
||||
* @param string $key Cache item name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMetaData(string $key)
|
||||
{
|
||||
$data = array_combine(['__ci_value'], $this->redis->hmget($key, ['__ci_value']));
|
||||
|
||||
if (isset($data['__ci_value']) && $data['__ci_value'] !== false)
|
||||
{
|
||||
return [
|
||||
'expire' => time() + $this->redis->ttl($key),
|
||||
'data' => $data['__ci_value'],
|
||||
];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determines if the driver is supported on this system.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSupported(): bool
|
||||
{
|
||||
return class_exists('\Predis\Client');
|
||||
}
|
||||
|
||||
}
|
||||
355
system/Cache/Handlers/RedisHandler.php
Normal file
355
system/Cache/Handlers/RedisHandler.php
Normal file
@@ -0,0 +1,355 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Cache\Handlers;
|
||||
|
||||
use CodeIgniter\Cache\CacheInterface;
|
||||
use CodeIgniter\Exceptions\CriticalError;
|
||||
|
||||
/**
|
||||
* Redis cache handler
|
||||
*/
|
||||
class RedisHandler implements CacheInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Prefixed to all cache names.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
* Default config
|
||||
*
|
||||
* @static
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [
|
||||
'host' => '127.0.0.1',
|
||||
'password' => null,
|
||||
'port' => 6379,
|
||||
'timeout' => 0,
|
||||
'database' => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* Redis connection
|
||||
*
|
||||
* @var Redis
|
||||
*/
|
||||
protected $redis;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $config
|
||||
* @throws type
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
$config = (array)$config;
|
||||
$this->prefix = $config['prefix'] ?? '';
|
||||
|
||||
if (! empty($config))
|
||||
{
|
||||
$this->config = array_merge($this->config, $config['redis']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class destructor
|
||||
*
|
||||
* Closes the connection to Memcache(d) if present.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->redis)
|
||||
{
|
||||
$this->redis->close();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Takes care of any handler-specific setup that must be done.
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
$config = $this->config;
|
||||
|
||||
$this->redis = new \Redis();
|
||||
|
||||
// Try to connect to Redis, if an issue occurs throw a CriticalError exception,
|
||||
// so that the CacheFactory can attempt to initiate the next cache handler.
|
||||
try
|
||||
{
|
||||
// Note:: If Redis is your primary cache choice, and it is "offline", every page load will end up been delayed by the timeout duration.
|
||||
// I feel like some sort of temporary flag should be set, to indicate that we think Redis is "offline", allowing us to bypass the timeout for a set period of time.
|
||||
|
||||
if (! $this->redis->connect($config['host'], ($config['host'][0] === '/' ? 0 : $config['port']), $config['timeout']))
|
||||
{
|
||||
// Note:: I'm unsure if log_message() is necessary, however I'm not 100% comfortable removing it.
|
||||
log_message('error', 'Cache: Redis connection failed. Check your configuration.');
|
||||
throw new CriticalError('Cache: Redis connection failed. Check your configuration.');
|
||||
}
|
||||
|
||||
if (isset($config['password']) && ! $this->redis->auth($config['password']))
|
||||
{
|
||||
log_message('error', 'Cache: Redis authentication failed.');
|
||||
throw new CriticalError('Cache: Redis authentication failed.');
|
||||
}
|
||||
|
||||
if (isset($config['database']) && ! $this->redis->select($config['database']))
|
||||
{
|
||||
log_message('error', 'Cache: Redis select database failed.');
|
||||
throw new CriticalError('Cache: Redis select database failed.');
|
||||
}
|
||||
}
|
||||
catch (\RedisException $e)
|
||||
{
|
||||
// $this->redis->connect() can sometimes throw a RedisException.
|
||||
// We need to convert the exception into a CriticalError exception and throw it.
|
||||
throw new CriticalError('Cache: RedisException occurred with message (' . $e->getMessage() . ').');
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attempts to fetch an item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$data = $this->redis->hMGet($key, ['__ci_type', '__ci_value']);
|
||||
|
||||
if (! isset($data['__ci_type'], $data['__ci_value']) || $data['__ci_value'] === false)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
switch ($data['__ci_type'])
|
||||
{
|
||||
case 'array':
|
||||
case 'object':
|
||||
return unserialize($data['__ci_value']);
|
||||
case 'boolean':
|
||||
case 'integer':
|
||||
case 'double': // Yes, 'double' is returned and NOT 'float'
|
||||
case 'string':
|
||||
case 'NULL':
|
||||
return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null;
|
||||
case 'resource':
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Saves an item to the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
* @param mixed $value The data to save
|
||||
* @param integer $ttl Time To Live, in seconds (default 60)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(string $key, $value, int $ttl = 60)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
switch ($data_type = gettype($value))
|
||||
{
|
||||
case 'array':
|
||||
case 'object':
|
||||
$value = serialize($value);
|
||||
break;
|
||||
case 'boolean':
|
||||
case 'integer':
|
||||
case 'double': // Yes, 'double' is returned and NOT 'float'
|
||||
case 'string':
|
||||
case 'NULL':
|
||||
break;
|
||||
case 'resource':
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->redis->hMSet($key, ['__ci_type' => $data_type, '__ci_value' => $value]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
elseif ($ttl)
|
||||
{
|
||||
$this->redis->expireAt($key, time() + $ttl);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Deletes a specific item from the cache store.
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
return ($this->redis->del($key) === 1);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic incrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function increment(string $key, int $offset = 1)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
return $this->redis->hIncrBy($key, 'data', $offset);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic decrementation of a raw stored value.
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function decrement(string $key, int $offset = 1)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
return $this->redis->hIncrBy($key, 'data', -$offset);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Will delete all items in the entire cache.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return $this->redis->flushDB();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns information on the entire cache.
|
||||
*
|
||||
* The information returned and the structure of the data
|
||||
* varies depending on the handler.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCacheInfo()
|
||||
{
|
||||
return $this->redis->info();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns detailed information about the specific item in the cache.
|
||||
*
|
||||
* @param string $key Cache item name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMetaData(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$value = $this->get($key);
|
||||
|
||||
if ($value !== null)
|
||||
{
|
||||
$time = time();
|
||||
return [
|
||||
'expire' => $time + $this->redis->ttl($key),
|
||||
'mtime' => $time,
|
||||
'data' => $value,
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determines if the driver is supported on this system.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSupported(): bool
|
||||
{
|
||||
return extension_loaded('redis');
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
||||
263
system/Cache/Handlers/WincacheHandler.php
Normal file
263
system/Cache/Handlers/WincacheHandler.php
Normal file
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2019 British Columbia Institute of Technology
|
||||
* Copyright (c) 2019 CodeIgniter Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright 2019 CodeIgniter Foundation
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link https://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Cache\Handlers;
|
||||
|
||||
use CodeIgniter\Cache\CacheInterface;
|
||||
|
||||
/**
|
||||
* Cache handler for WinCache from Microsoft & IIS.
|
||||
* Windows-only, so not testable on travis-ci.
|
||||
* Unusable methods flagged for code coverage ignoring.
|
||||
*/
|
||||
class WincacheHandler implements CacheInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Prefixed to all cache names.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param type $config
|
||||
* @throws type
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->prefix = $config->prefix ?: '';
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Takes care of any handler-specific setup that must be done.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
// Nothing to see here...
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attempts to fetch an item from the cache store.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$success = false;
|
||||
$data = wincache_ucache_get($key, $success);
|
||||
|
||||
// Success returned by reference from wincache_ucache_get()
|
||||
return ($success) ? $data : null;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Saves an item to the cache store.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
* @param mixed $value The data to save
|
||||
* @param integer $ttl Time To Live, in seconds (default 60)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(string $key, $value, int $ttl = 60)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
return wincache_ucache_set($key, $value, $ttl);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Deletes a specific item from the cache store.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param string $key Cache item name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
return wincache_ucache_delete($key);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic incrementation of a raw stored value.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function increment(string $key, int $offset = 1)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$success = false;
|
||||
$value = wincache_ucache_inc($key, $offset, $success);
|
||||
|
||||
return ($success === true) ? $value : false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Performs atomic decrementation of a raw stored value.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param string $key Cache ID
|
||||
* @param integer $offset Step/value to increase by
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function decrement(string $key, int $offset = 1)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
$success = false;
|
||||
$value = wincache_ucache_dec($key, $offset, $success);
|
||||
|
||||
return ($success === true) ? $value : false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Will delete all items in the entire cache.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
return wincache_ucache_clear();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns information on the entire cache.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* The information returned and the structure of the data
|
||||
* varies depending on the handler.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCacheInfo()
|
||||
{
|
||||
return wincache_ucache_info(true);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns detailed information about the specific item in the cache.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @param string $key Cache item name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMetaData(string $key)
|
||||
{
|
||||
$key = $this->prefix . $key;
|
||||
|
||||
if ($stored = wincache_ucache_info(false, $key))
|
||||
{
|
||||
$age = $stored['ucache_entries'][1]['age_seconds'];
|
||||
$ttl = $stored['ucache_entries'][1]['ttl_seconds'];
|
||||
$hitcount = $stored['ucache_entries'][1]['hitcount'];
|
||||
|
||||
return [
|
||||
'expire' => $ttl - $age,
|
||||
'hitcount' => $hitcount,
|
||||
'age' => $age,
|
||||
'ttl' => $ttl,
|
||||
];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determines if the driver is supported on this system.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSupported(): bool
|
||||
{
|
||||
return (extension_loaded('wincache') && ini_get('wincache.ucenabled'));
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
||||
Reference in New Issue
Block a user