mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-09-02 21:41:36 +08:00
PSR规范调整
This commit is contained in:
92
library/think/cache/driver/memcache.php
vendored
92
library/think/cache/driver/memcache.php
vendored
@@ -10,21 +10,23 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* Memcache缓存驱动
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Memcache {
|
||||
protected $handler = null;
|
||||
protected $options = [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 11211,
|
||||
'expire' => 0,
|
||||
'timeout' => false,
|
||||
'persistent' => false,
|
||||
'length' => 0,
|
||||
class Memcache
|
||||
{
|
||||
protected $handler = null;
|
||||
protected $options = [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 11211,
|
||||
'expire' => 0,
|
||||
'timeout' => false,
|
||||
'persistent' => false,
|
||||
'length' => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -32,23 +34,24 @@ class Memcache {
|
||||
* @param array $options 缓存参数
|
||||
* @access public
|
||||
*/
|
||||
public function __construct($options=[]) {
|
||||
if ( !extension_loaded('memcache') ) {
|
||||
public function __construct($options = [])
|
||||
{
|
||||
if (!extension_loaded('memcache')) {
|
||||
throw new Exception('_NOT_SUPPERT_:memcache');
|
||||
}
|
||||
if(!empty($options)) {
|
||||
$this->options = array_merge($this->options,$options);
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
$this->handler = new \Memcache;
|
||||
$this->handler = new \Memcache;
|
||||
// 支持集群
|
||||
$hosts = explode(',', $this->options['host']);
|
||||
$ports = explode(',', $this->options['port']);
|
||||
$hosts = explode(',', $this->options['host']);
|
||||
$ports = explode(',', $this->options['port']);
|
||||
|
||||
foreach((array) $hosts as $i=>$host){
|
||||
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
|
||||
$options['timeout'] === false ?
|
||||
$this->handler->addServer($host, $port, $this->options['persistent'], 1) :
|
||||
$this->handler->addServer($host, $port, $this->options['persistent'], 1, $this->options['timeout']);
|
||||
foreach ((array) $hosts as $i => $host) {
|
||||
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
|
||||
false === $options['timeout'] ?
|
||||
$this->handler->addServer($host, $port, $this->options['persistent'], 1) :
|
||||
$this->handler->addServer($host, $port, $this->options['persistent'], 1, $this->options['timeout']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,8 +61,9 @@ class Memcache {
|
||||
* @param string $name 缓存变量名
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name) {
|
||||
return $this->handler->get($this->options['prefix'].$name);
|
||||
public function get($name)
|
||||
{
|
||||
return $this->handler->get($this->options['prefix'] . $name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,22 +74,26 @@ class Memcache {
|
||||
* @param integer $expire 有效时间(秒)
|
||||
* @return boolen
|
||||
*/
|
||||
public function set($name, $value, $expire = null) {
|
||||
if(is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
public function set($name, $value, $expire = null)
|
||||
{
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
$name = $this->options['prefix'].$name;
|
||||
if($this->handler->set($name, $value, 0, $expire)) {
|
||||
if($this->options['length']>0) {
|
||||
$name = $this->options['prefix'] . $name;
|
||||
if ($this->handler->set($name, $value, 0, $expire)) {
|
||||
if ($this->options['length'] > 0) {
|
||||
// 记录缓存队列
|
||||
$queue = $this->handler->get('__info__');
|
||||
if(!$queue) {
|
||||
$queue = [];
|
||||
$queue = $this->handler->get('__info__');
|
||||
if (!$queue) {
|
||||
$queue = [];
|
||||
}
|
||||
if(false===array_search($name, $queue)) array_push($queue,$name);
|
||||
if(count($queue) > $this->options['length']) {
|
||||
if (false === array_search($name, $queue)) {
|
||||
array_push($queue, $name);
|
||||
}
|
||||
|
||||
if (count($queue) > $this->options['length']) {
|
||||
// 出列
|
||||
$key = array_shift($queue);
|
||||
$key = array_shift($queue);
|
||||
// 删除缓存
|
||||
$this->handler->delete($key);
|
||||
}
|
||||
@@ -102,11 +110,12 @@ class Memcache {
|
||||
* @param string $name 缓存变量名
|
||||
* @return boolen
|
||||
*/
|
||||
public function rm($name, $ttl = false) {
|
||||
$name = $this->options['prefix'].$name;
|
||||
return $ttl === false ?
|
||||
$this->handler->delete($name) :
|
||||
$this->handler->delete($name, $ttl);
|
||||
public function rm($name, $ttl = false)
|
||||
{
|
||||
$name = $this->options['prefix'] . $name;
|
||||
return false === $ttl ?
|
||||
$this->handler->delete($name) :
|
||||
$this->handler->delete($name, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,7 +123,8 @@ class Memcache {
|
||||
* @access public
|
||||
* @return boolen
|
||||
*/
|
||||
public function clear() {
|
||||
public function clear()
|
||||
{
|
||||
return $this->handler->flush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user