first commit
This commit is contained in:
201
vendor/topthink/think-cache/LICENSE
vendored
Normal file
201
vendor/topthink/think-cache/LICENSE
vendored
Normal file
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
75
vendor/topthink/think-cache/README.md
vendored
Normal file
75
vendor/topthink/think-cache/README.md
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
# think-cache
|
||||
|
||||
用于PHP缓存管理(PHP 7.1+),支持`PSR-6`及`PSR-16`缓存规范。
|
||||
|
||||
## 安装
|
||||
~~~
|
||||
composer require topthink/think-cache
|
||||
~~~
|
||||
|
||||
## 用法:
|
||||
~~~php
|
||||
use think\facade\Cache;
|
||||
|
||||
// 缓存配置
|
||||
Cache::config([
|
||||
'default' => 'file',
|
||||
'stores' => [
|
||||
'file' => [
|
||||
'type' => 'File',
|
||||
// 缓存保存目录
|
||||
'path' => './cache/',
|
||||
// 缓存前缀
|
||||
'prefix' => '',
|
||||
// 缓存有效期 0表示永久缓存
|
||||
'expire' => 0,
|
||||
],
|
||||
'redis' => [
|
||||
'type' => 'redis',
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 6379,
|
||||
'prefix' => '',
|
||||
'expire' => 0,
|
||||
],
|
||||
],
|
||||
]);
|
||||
// 设置缓存
|
||||
Cache::set('val','value',600);
|
||||
// 判断缓存是否设置
|
||||
Cache::has('val');
|
||||
// 获取缓存
|
||||
Cache::get('val');
|
||||
// 删除缓存
|
||||
Cache::delete('val');
|
||||
// 清除缓存
|
||||
Cache::clear();
|
||||
// 读取并删除缓存
|
||||
Cache::pull('val');
|
||||
// 不存在则写入
|
||||
Cache::remember('val',10);
|
||||
|
||||
// 对于数值类型的缓存数据可以使用
|
||||
// 缓存增+1
|
||||
Cache::inc('val');
|
||||
// 缓存增+5
|
||||
Cache::inc('val',5);
|
||||
// 缓存减1
|
||||
Cache::dec('val');
|
||||
// 缓存减5
|
||||
Cache::dec('val',5);
|
||||
|
||||
// 使用缓存标签
|
||||
Cache::tag('tag_name')->set('val','value',600);
|
||||
// 删除某个标签下的缓存数据
|
||||
Cache::tag('tag_name')->clear();
|
||||
// 支持指定多个标签
|
||||
Cache::tag(['tag1','tag2'])->set('val2','value',600);
|
||||
// 删除多个标签下的缓存数据
|
||||
Cache::tag(['tag1','tag2'])->clear();
|
||||
|
||||
// 使用多种缓存类型
|
||||
$redis = Cache::store('redis');
|
||||
|
||||
$redis->set('var','value',600);
|
||||
$redis->get('var');
|
||||
~~~
|
||||
24
vendor/topthink/think-cache/composer.json
vendored
Normal file
24
vendor/topthink/think-cache/composer.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "topthink/think-cache",
|
||||
"description": "Cache Manager",
|
||||
"license": "Apache-2.0",
|
||||
"authors": [
|
||||
{
|
||||
"name": "liu21st",
|
||||
"email": "liu21st@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.1.0",
|
||||
"psr/cache": "~1.0",
|
||||
"topthink/think-container": "~2.0",
|
||||
"psr/simple-cache": "^1.0",
|
||||
"opis/closure": "^3.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"think\\": "src"
|
||||
},
|
||||
"files": []
|
||||
}
|
||||
}
|
||||
271
vendor/topthink/think-cache/src/CacheManager.php
vendored
Normal file
271
vendor/topthink/think-cache/src/CacheManager.php
vendored
Normal file
@@ -0,0 +1,271 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think;
|
||||
|
||||
use Psr\Cache\CacheItemInterface;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use think\cache\Driver;
|
||||
use think\Container;
|
||||
use think\exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* 缓存管理类
|
||||
* @mixin Driver
|
||||
*/
|
||||
class CacheManager implements CacheItemPoolInterface
|
||||
{
|
||||
/**
|
||||
* 缓存队列
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* 延期保存的缓存队列
|
||||
* @var array
|
||||
*/
|
||||
protected $deferred = [];
|
||||
|
||||
/**
|
||||
* 缓存实例
|
||||
* @var array
|
||||
*/
|
||||
protected $instance = [];
|
||||
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [];
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @access public
|
||||
* @param array $config 配置参数
|
||||
* @return void
|
||||
*/
|
||||
public function init(array $config = [])
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接或者切换缓存
|
||||
* @access public
|
||||
* @param string $name 连接配置名
|
||||
* @param bool $force 强制重新连接
|
||||
* @return Driver
|
||||
*/
|
||||
public function store(string $name = '', bool $force = false): Driver
|
||||
{
|
||||
if ('' == $name) {
|
||||
$name = $this->config['default'] ?? 'file';
|
||||
}
|
||||
|
||||
if ($force || !isset($this->instance[$name])) {
|
||||
if (!isset($this->config['stores'][$name])) {
|
||||
throw new InvalidArgumentException('Undefined cache config:' . $name);
|
||||
}
|
||||
|
||||
$options = $this->config['stores'][$name];
|
||||
|
||||
$this->instance[$name] = $this->connect($options);
|
||||
}
|
||||
|
||||
return $this->instance[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接缓存
|
||||
* @access public
|
||||
* @param array $options 连接参数
|
||||
* @param string $name 连接配置名
|
||||
* @return Driver
|
||||
*/
|
||||
public function connect(array $options, string $name = ''): Driver
|
||||
{
|
||||
if ($name && isset($this->instance[$name])) {
|
||||
return $this->instance[$name];
|
||||
}
|
||||
|
||||
$type = !empty($options['type']) ? $options['type'] : 'File';
|
||||
|
||||
$handler = Container::factory($type, '\\think\\cache\\driver\\', $options);
|
||||
|
||||
if ($name) {
|
||||
$this->instance[$name] = $handler;
|
||||
}
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置
|
||||
* @access public
|
||||
* @param array $config 配置参数
|
||||
* @return void
|
||||
*/
|
||||
public function config(array $config): void
|
||||
{
|
||||
$this->config = array_merge($this->config, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回「键」对应的一个缓存项。
|
||||
* @access public
|
||||
* @param string $key 缓存标识
|
||||
* @return CacheItemInterface
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getItem($key): CacheItem
|
||||
{
|
||||
if (isset($this->data[$key])) {
|
||||
return $this->data[$key];
|
||||
}
|
||||
|
||||
$cacheItem = new CacheItem($key);
|
||||
|
||||
if ($this->has($key)) {
|
||||
$cacheItem->set($this->get($key));
|
||||
}
|
||||
|
||||
$this->data[$key] = $cacheItem;
|
||||
|
||||
return $cacheItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个可供遍历的缓存项集合。
|
||||
* @access public
|
||||
* @param array $keys
|
||||
* @return array|\Traversable
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getItems(array $keys = []): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($keys as $key) {
|
||||
$result[] = $this->getItem($key);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查缓存系统中是否有「键」对应的缓存项。
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return bool
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function hasItem($key): bool
|
||||
{
|
||||
return $this->store()->has($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓冲池
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
return $this->store()->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓冲池里移除某个缓存项
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return bool
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function deleteItem($key): bool
|
||||
{
|
||||
return $this->store()->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓冲池里移除多个缓存项
|
||||
* @access public
|
||||
* @param array $keys
|
||||
* @return bool
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function deleteItems(array $keys): bool
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->store()->delete($key);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 立刻为「CacheItemInterface」对象做数据持久化。
|
||||
* @access public
|
||||
* @param CacheItemInterface $item
|
||||
* @return bool
|
||||
*/
|
||||
public function save(CacheItemInterface $item): bool
|
||||
{
|
||||
if ($item->getKey()) {
|
||||
return $this->store()->set($item->getKey(), $item->get(), $item->getExpire());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 稍后为「CacheItemInterface」对象做数据持久化。
|
||||
* @access public
|
||||
* @param CacheItemInterface $item
|
||||
* @return bool
|
||||
*/
|
||||
public function saveDeferred(CacheItemInterface $item): bool
|
||||
{
|
||||
$this->deferred[$item->getKey()] = $item;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交所有的正在队列里等待的请求到数据持久层,配合 `saveDeferred()` 使用
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function commit(): bool
|
||||
{
|
||||
foreach ($this->deferred as $key => $item) {
|
||||
$result = $this->save($item);
|
||||
unset($this->deferred[$key]);
|
||||
|
||||
if (false === $result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function __call($method, $args)
|
||||
{
|
||||
return call_user_func_array([$this->store(), $method], $args);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if (!empty($this->deferred)) {
|
||||
$this->commit();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
210
vendor/topthink/think-cache/src/cache/CacheItem.php
vendored
Normal file
210
vendor/topthink/think-cache/src/cache/CacheItem.php
vendored
Normal file
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think\cache;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Psr\Cache\CacheItemInterface;
|
||||
use think\cache\exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* CacheItem实现类
|
||||
*/
|
||||
class CacheItem implements CacheItemInterface
|
||||
{
|
||||
/**
|
||||
* 缓存Key
|
||||
* @var string
|
||||
*/
|
||||
protected $key;
|
||||
|
||||
/**
|
||||
* 缓存内容
|
||||
* @var mixed
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
/**
|
||||
* 过期时间
|
||||
* @var int|DateTimeInterface
|
||||
*/
|
||||
protected $expire;
|
||||
|
||||
/**
|
||||
* 缓存tag
|
||||
* @var string
|
||||
*/
|
||||
protected $tag;
|
||||
|
||||
/**
|
||||
* 缓存是否命中
|
||||
* @var bool
|
||||
*/
|
||||
protected $isHit = false;
|
||||
|
||||
public function __construct(string $key = null)
|
||||
{
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为此缓存项设置「键」
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @return $this
|
||||
*/
|
||||
public function setKey(string $key)
|
||||
{
|
||||
$this->key = $key;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回当前缓存项的「键」
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getKey()
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回当前缓存项的有效期
|
||||
* @access public
|
||||
* @return DateTimeInterface|int|null
|
||||
*/
|
||||
public function getExpire()
|
||||
{
|
||||
if ($this->expire instanceof DateTimeInterface) {
|
||||
return $this->expire;
|
||||
}
|
||||
|
||||
return $this->expire ? $this->expire - time() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存Tag
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getTag()
|
||||
{
|
||||
return $this->tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 凭借此缓存项的「键」从缓存系统里面取出缓存项
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认缓存项的检查是否命中
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function isHit(): bool
|
||||
{
|
||||
return $this->isHit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为此缓存项设置「值」
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
public function set($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->isHit = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为此缓存项设置所属标签
|
||||
* @access public
|
||||
* @param string $tag
|
||||
* @return $this
|
||||
*/
|
||||
public function tag(string $tag = null)
|
||||
{
|
||||
$this->tag = $tag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存项的有效期
|
||||
* @access public
|
||||
* @param mixed $expire
|
||||
* @return $this
|
||||
*/
|
||||
public function expire($expire)
|
||||
{
|
||||
if (is_null($expire)) {
|
||||
$this->expire = null;
|
||||
} elseif (is_numeric($expire) || $expire instanceof DateInterval) {
|
||||
$this->expiresAfter($expire);
|
||||
} elseif ($expire instanceof DateTimeInterface) {
|
||||
$this->expire = $expire;
|
||||
} else {
|
||||
throw new InvalidArgumentException('not support datetime');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存项的准确过期时间点
|
||||
* @access public
|
||||
* @param DateTimeInterface $expiration
|
||||
* @return $this
|
||||
*/
|
||||
public function expiresAt($expiration)
|
||||
{
|
||||
if ($expiration instanceof DateTimeInterface) {
|
||||
$this->expire = $expiration;
|
||||
} else {
|
||||
throw new InvalidArgumentException('not support datetime');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存项的过期时间
|
||||
* @access public
|
||||
* @param int|DateInterval $timeInterval
|
||||
* @return $this
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function expiresAfter($timeInterval)
|
||||
{
|
||||
if ($timeInterval instanceof DateInterval) {
|
||||
$this->expire = (int) DateTime::createFromFormat('U', (string) time())->add($timeInterval)->format('U');
|
||||
} elseif (is_numeric($timeInterval)) {
|
||||
$this->expire = $timeInterval + time();
|
||||
} else {
|
||||
throw new InvalidArgumentException('not support datetime');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
349
vendor/topthink/think-cache/src/cache/Driver.php
vendored
Normal file
349
vendor/topthink/think-cache/src/cache/Driver.php
vendored
Normal file
@@ -0,0 +1,349 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think\cache;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Opis\Closure\SerializableClosure;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use think\cache\exception\InvalidArgumentException;
|
||||
use think\Container;
|
||||
|
||||
/**
|
||||
* 缓存基础类
|
||||
*/
|
||||
abstract class Driver implements CacheInterface
|
||||
{
|
||||
/**
|
||||
* 驱动句柄
|
||||
* @var object
|
||||
*/
|
||||
protected $handler = null;
|
||||
|
||||
/**
|
||||
* 缓存读取次数
|
||||
* @var integer
|
||||
*/
|
||||
protected $readTimes = 0;
|
||||
|
||||
/**
|
||||
* 缓存写入次数
|
||||
* @var integer
|
||||
*/
|
||||
protected $writeTimes = 0;
|
||||
|
||||
/**
|
||||
* 缓存参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* 缓存标签
|
||||
* @var array
|
||||
*/
|
||||
protected $tag = [];
|
||||
|
||||
/**
|
||||
* 获取有效期
|
||||
* @access protected
|
||||
* @param integer|DateTimeInterface|DateInterval $expire 有效期
|
||||
* @return int
|
||||
*/
|
||||
protected function getExpireTime($expire): int
|
||||
{
|
||||
if ($expire instanceof DateTimeInterface) {
|
||||
$expire = $expire->getTimestamp() - time();
|
||||
} elseif ($expire instanceof DateInterval) {
|
||||
$expire = DateTime::createFromFormat('U', (string) time())
|
||||
->add($expire)
|
||||
->format('U') - time();
|
||||
}
|
||||
|
||||
return (int) $expire;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实际的缓存标识
|
||||
* @access public
|
||||
* @param string $name 缓存名
|
||||
* @return string
|
||||
*/
|
||||
public function getCacheKey(string $name): string
|
||||
{
|
||||
return $this->options['prefix'] . $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存并删除
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return mixed
|
||||
*/
|
||||
public function pull(string $name)
|
||||
{
|
||||
$result = $this->get($name, false);
|
||||
|
||||
if ($result) {
|
||||
$this->delete($name);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加(数组)缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @return void
|
||||
*/
|
||||
public function push(string $name, $value): void
|
||||
{
|
||||
$item = $this->get($name, []);
|
||||
|
||||
if (!is_array($item)) {
|
||||
throw new InvalidArgumentException('only array cache can be push');
|
||||
}
|
||||
|
||||
$item[] = $value;
|
||||
|
||||
if (count($item) > 1000) {
|
||||
array_shift($item);
|
||||
}
|
||||
|
||||
$item = array_unique($item);
|
||||
|
||||
$this->set($name, $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不存在则写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int $expire 有效时间 0为永久
|
||||
* @return mixed
|
||||
*/
|
||||
public function remember(string $name, $value, $expire = null)
|
||||
{
|
||||
if ($this->has($name)) {
|
||||
return $this->get($name);
|
||||
}
|
||||
|
||||
$time = time();
|
||||
|
||||
while ($time + 5 > time() && $this->has($name . '_lock')) {
|
||||
// 存在锁定则等待
|
||||
usleep(200000);
|
||||
}
|
||||
|
||||
try {
|
||||
// 锁定
|
||||
$this->set($name . '_lock', true);
|
||||
|
||||
if ($value instanceof \Closure) {
|
||||
// 获取缓存数据
|
||||
$value = Container::getInstance()->invokeFunction($value);
|
||||
}
|
||||
|
||||
// 缓存数据
|
||||
$this->set($name, $value, $expire);
|
||||
|
||||
// 解锁
|
||||
$this->delete($name . '_lock');
|
||||
} catch (\Exception | \throwable $e) {
|
||||
$this->delete($name . '_lock');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存标签
|
||||
* @access public
|
||||
* @param string|array $name 标签名
|
||||
* @return $this
|
||||
*/
|
||||
public function tag($name)
|
||||
{
|
||||
$name = (array) $name;
|
||||
$key = implode('-', $name);
|
||||
|
||||
if (!isset($this->tag[$key])) {
|
||||
$name = array_map(function ($val) {
|
||||
return $this->getTagKey($val);
|
||||
}, $name);
|
||||
$this->tag[$key] = new TagSet($name, $this);
|
||||
}
|
||||
|
||||
return $this->tag[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标签包含的缓存标识
|
||||
* @access public
|
||||
* @param string $tag 标签标识
|
||||
* @return array
|
||||
*/
|
||||
public function getTagItems(string $tag): array
|
||||
{
|
||||
$name = $this->getTagKey($tag);
|
||||
return $this->get($name, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实际标签名
|
||||
* @access public
|
||||
* @param string $tag 标签名
|
||||
* @return string
|
||||
*/
|
||||
public function getTagKey(string $tag): string
|
||||
{
|
||||
return $this->options['tag_prefix'] . md5($tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化数据
|
||||
* @access protected
|
||||
* @param mixed $data 缓存数据
|
||||
* @return string
|
||||
*/
|
||||
protected function serialize($data): string
|
||||
{
|
||||
$serialize = $this->options['serialize'][0] ?? function ($data) {
|
||||
SerializableClosure::enterContext();
|
||||
SerializableClosure::wrapClosures($data);
|
||||
$data = \serialize($data);
|
||||
SerializableClosure::exitContext();
|
||||
return $data;
|
||||
};
|
||||
|
||||
return $serialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化数据
|
||||
* @access protected
|
||||
* @param string $data 缓存数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function unserialize(string $data)
|
||||
{
|
||||
$unserialize = $this->options['serialize'][1] ?? function ($data) {
|
||||
SerializableClosure::enterContext();
|
||||
$data = \unserialize($data);
|
||||
SerializableClosure::unwrapClosures($data);
|
||||
SerializableClosure::exitContext();
|
||||
return $data;
|
||||
};
|
||||
|
||||
return $unserialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回句柄对象,可执行其它高级方法
|
||||
*
|
||||
* @access public
|
||||
* @return object
|
||||
*/
|
||||
public function handler()
|
||||
{
|
||||
return $this->handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回缓存读取次数
|
||||
* @access public
|
||||
* @return int
|
||||
*/
|
||||
public function getReadTimes(): int
|
||||
{
|
||||
return $this->readTimes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回缓存写入次数
|
||||
* @access public
|
||||
* @return int
|
||||
*/
|
||||
public function getWriteTimes(): int
|
||||
{
|
||||
return $this->writeTimes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param iterable $keys 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return iterable
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getMultiple($keys, $default = null): iterable
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$result[$key] = $this->get($key, $default);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param iterable $values 缓存数据
|
||||
* @param null|int|\DateInterval $ttl 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function setMultiple($values, $ttl = null): bool
|
||||
{
|
||||
foreach ($values as $key => $val) {
|
||||
$result = $this->set($key, $val, $ttl);
|
||||
|
||||
if (false === $result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param iterable $keys 缓存变量名
|
||||
* @return bool
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function deleteMultiple($keys): bool
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$result = $this->delete($key);
|
||||
|
||||
if (false === $result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function __call($method, $args)
|
||||
{
|
||||
return call_user_func_array([$this->handler, $method], $args);
|
||||
}
|
||||
}
|
||||
130
vendor/topthink/think-cache/src/cache/TagSet.php
vendored
Normal file
130
vendor/topthink/think-cache/src/cache/TagSet.php
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think\cache;
|
||||
|
||||
/**
|
||||
* 标签集合
|
||||
*/
|
||||
class TagSet
|
||||
{
|
||||
/**
|
||||
* 标签的缓存Key
|
||||
* @var array
|
||||
*/
|
||||
protected $tag;
|
||||
|
||||
/**
|
||||
* 缓存句柄
|
||||
* @var Driver
|
||||
*/
|
||||
protected $handler;
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $tag 缓存标签
|
||||
* @param Driver $cache 缓存对象
|
||||
*/
|
||||
public function __construct(array $tag, Driver $cache)
|
||||
{
|
||||
$this->tag = $tag;
|
||||
$this->handler = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param integer|\DateTime $expire 有效时间(秒)
|
||||
* @return bool
|
||||
*/
|
||||
public function set($name, $value, $expire = null): bool
|
||||
{
|
||||
$this->handler->set($name, $value, $expire);
|
||||
|
||||
$this->append($name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加缓存标识到标签
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return void
|
||||
*/
|
||||
public function append(string $name): void
|
||||
{
|
||||
$name = $this->handler->getCacheKey($name);
|
||||
|
||||
foreach ($this->tag as $tag) {
|
||||
$this->handler->push($tag, $name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param iterable $values 缓存数据
|
||||
* @param null|int|\DateInterval $ttl 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function setMultiple($values, $ttl = null): bool
|
||||
{
|
||||
foreach ($values as $key => $val) {
|
||||
$result = $this->set($key, $val, $ttl);
|
||||
|
||||
if (false === $result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不存在则写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int $expire 有效时间 0为永久
|
||||
* @return mixed
|
||||
*/
|
||||
public function remember(string $name, $value, $expire = null)
|
||||
{
|
||||
$result = $this->handler->remember($name, $value, $expire);
|
||||
|
||||
$this->append($name);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
// 指定标签清除
|
||||
foreach ($this->tag as $tag) {
|
||||
$names = $this->handler->get($tag, []);
|
||||
|
||||
$this->handler->clearTag($names);
|
||||
$this->handler->delete($tag);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
286
vendor/topthink/think-cache/src/cache/driver/File.php
vendored
Normal file
286
vendor/topthink/think-cache/src/cache/driver/File.php
vendored
Normal file
@@ -0,0 +1,286 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* 文件缓存类
|
||||
*/
|
||||
class File extends Driver
|
||||
{
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'expire' => 0,
|
||||
'cache_subdir' => true,
|
||||
'prefix' => '',
|
||||
'path' => '',
|
||||
'hash_type' => 'md5',
|
||||
'data_compress' => false,
|
||||
'tag_prefix' => 'tag:',
|
||||
'serialize' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* 有效期
|
||||
* @var int|\DateTime
|
||||
*/
|
||||
protected $expire;
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @param array $options 参数
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
if (substr($this->options['path'], -1) != DIRECTORY_SEPARATOR) {
|
||||
$this->options['path'] .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得变量的存储文件名
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return string
|
||||
*/
|
||||
public function getCacheKey(string $name): string
|
||||
{
|
||||
$name = hash($this->options['hash_type'], $name);
|
||||
|
||||
if ($this->options['cache_subdir']) {
|
||||
// 使用子目录
|
||||
$name = substr($name, 0, 2) . DIRECTORY_SEPARATOR . substr($name, 2);
|
||||
}
|
||||
|
||||
if ($this->options['prefix']) {
|
||||
$name = $this->options['prefix'] . DIRECTORY_SEPARATOR . $name;
|
||||
}
|
||||
|
||||
return $this->options['path'] . $name . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name): bool
|
||||
{
|
||||
return false !== $this->get($name) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$filename = $this->getCacheKey($name);
|
||||
|
||||
if (!is_file($filename)) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$content = file_get_contents($filename);
|
||||
$this->expire = null;
|
||||
|
||||
if (false !== $content) {
|
||||
$expire = (int) substr($content, 8, 12);
|
||||
if (0 != $expire && time() > filemtime($filename) + $expire) {
|
||||
//缓存过期删除缓存文件
|
||||
$this->unlink($filename);
|
||||
return $default;
|
||||
}
|
||||
|
||||
$this->expire = $expire;
|
||||
$content = substr($content, 32);
|
||||
|
||||
if ($this->options['data_compress'] && function_exists('gzcompress')) {
|
||||
//启用数据压缩
|
||||
$content = gzuncompress($content);
|
||||
}
|
||||
return $this->unserialize($content);
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int|\DateTime $expire 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function set($name, $value, $expire = null): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
|
||||
$expire = $this->getExpireTime($expire);
|
||||
$filename = $this->getCacheKey($name);
|
||||
|
||||
$dir = dirname($filename);
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
try {
|
||||
mkdir($dir, 0755, true);
|
||||
} catch (\Exception $e) {
|
||||
// 创建失败
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->serialize($value);
|
||||
|
||||
if ($this->options['data_compress'] && function_exists('gzcompress')) {
|
||||
//数据压缩
|
||||
$data = gzcompress($data, 3);
|
||||
}
|
||||
|
||||
$data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
|
||||
$result = file_put_contents($filename, $data);
|
||||
|
||||
if ($result) {
|
||||
clearstatcache();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function inc(string $name, int $step = 1)
|
||||
{
|
||||
if ($this->has($name)) {
|
||||
$value = $this->get($name) + $step;
|
||||
$expire = $this->expire;
|
||||
} else {
|
||||
$value = $step;
|
||||
$expire = 0;
|
||||
}
|
||||
|
||||
return $this->set($name, $value, $expire) ? $value : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function dec(string $name, int $step = 1)
|
||||
{
|
||||
if ($this->has($name)) {
|
||||
$value = $this->get($name) - $step;
|
||||
$expire = $this->expire;
|
||||
} else {
|
||||
$value = -$step;
|
||||
$expire = 0;
|
||||
}
|
||||
|
||||
return $this->set($name, $value, $expire) ? $value : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($name): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
try {
|
||||
return $this->unlink($this->getCacheKey($name));
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$files = (array) glob($this->options['path'] . ($this->options['prefix'] ? $this->options['prefix'] . DIRECTORY_SEPARATOR : '') . '*');
|
||||
|
||||
foreach ($files as $path) {
|
||||
if (is_dir($path)) {
|
||||
$matches = glob($path . DIRECTORY_SEPARATOR . '*.php');
|
||||
if (is_array($matches)) {
|
||||
array_map('unlink', $matches);
|
||||
}
|
||||
rmdir($path);
|
||||
} else {
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存标签
|
||||
* @access public
|
||||
* @param array $keys 缓存标识列表
|
||||
* @return void
|
||||
*/
|
||||
public function clearTag(array $keys): void
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->unlink($key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否存在后,删除
|
||||
* @access private
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
private function unlink(string $path): bool
|
||||
{
|
||||
return is_file($path) && unlink($path);
|
||||
}
|
||||
|
||||
}
|
||||
208
vendor/topthink/think-cache/src/cache/driver/Memcache.php
vendored
Normal file
208
vendor/topthink/think-cache/src/cache/driver/Memcache.php
vendored
Normal file
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* Memcache缓存类
|
||||
*/
|
||||
class Memcache extends Driver
|
||||
{
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 11211,
|
||||
'expire' => 0,
|
||||
'timeout' => 0, // 超时时间(单位:毫秒)
|
||||
'persistent' => true,
|
||||
'prefix' => '',
|
||||
'tag_prefix' => 'tag:',
|
||||
'serialize' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $options 缓存参数
|
||||
* @throws \BadFunctionCallException
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!extension_loaded('memcache')) {
|
||||
throw new \BadFunctionCallException('not support: memcache');
|
||||
}
|
||||
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
$this->handler = new \Memcache;
|
||||
|
||||
// 支持集群
|
||||
$hosts = (array) $this->options['host'];
|
||||
$ports = (array) $this->options['port'];
|
||||
|
||||
if (empty($ports[0])) {
|
||||
$ports[0] = 11211;
|
||||
}
|
||||
|
||||
// 建立连接
|
||||
foreach ($hosts as $i => $host) {
|
||||
$port = $ports[$i] ?? $ports[0];
|
||||
$this->options['timeout'] > 0 ?
|
||||
$this->handler->addServer($host, (int) $port, $this->options['persistent'], 1, $this->options['timeout']) :
|
||||
$this->handler->addServer($host, (int) $port, $this->options['persistent'], 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name): bool
|
||||
{
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return false !== $this->handler->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$result = $this->handler->get($this->getCacheKey($name));
|
||||
|
||||
return false !== $result ? $this->unserialize($result) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int|\DateTime $expire 有效时间(秒)
|
||||
* @return bool
|
||||
*/
|
||||
public function set($name, $value, $expire = null): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
$expire = $this->getExpireTime($expire);
|
||||
$value = $this->serialize($value);
|
||||
|
||||
if ($this->handler->set($key, $value, 0, $expire)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function inc(string $name, int $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
if ($this->handler->get($key)) {
|
||||
return $this->handler->increment($key, $step);
|
||||
}
|
||||
|
||||
return $this->handler->set($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function dec(string $name, int $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
$value = $this->handler->get($key) - $step;
|
||||
$res = $this->handler->set($key, $value);
|
||||
|
||||
return !$res ? false : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param bool|false $ttl
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($name, $ttl = false): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return false === $ttl ?
|
||||
$this->handler->delete($key) :
|
||||
$this->handler->delete($key, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
return $this->handler->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存标签
|
||||
* @access public
|
||||
* @param array $keys 缓存标识列表
|
||||
* @return void
|
||||
*/
|
||||
public function clearTag(array $keys): void
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->handler->delete($key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
220
vendor/topthink/think-cache/src/cache/driver/Memcached.php
vendored
Normal file
220
vendor/topthink/think-cache/src/cache/driver/Memcached.php
vendored
Normal file
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* Memcached缓存类
|
||||
*/
|
||||
class Memcached extends Driver
|
||||
{
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 11211,
|
||||
'expire' => 0,
|
||||
'timeout' => 0, // 超时时间(单位:毫秒)
|
||||
'prefix' => '',
|
||||
'username' => '', //账号
|
||||
'password' => '', //密码
|
||||
'option' => [],
|
||||
'tag_prefix' => 'tag:',
|
||||
'serialize' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $options 缓存参数
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!extension_loaded('memcached')) {
|
||||
throw new \BadFunctionCallException('not support: memcached');
|
||||
}
|
||||
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
$this->handler = new \Memcached;
|
||||
|
||||
if (!empty($this->options['option'])) {
|
||||
$this->handler->setOptions($this->options['option']);
|
||||
}
|
||||
|
||||
// 设置连接超时时间(单位:毫秒)
|
||||
if ($this->options['timeout'] > 0) {
|
||||
$this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']);
|
||||
}
|
||||
|
||||
// 支持集群
|
||||
$hosts = (array) $this->options['host'];
|
||||
$ports = (array) $this->options['port'];
|
||||
if (empty($ports[0])) {
|
||||
$ports[0] = 11211;
|
||||
}
|
||||
|
||||
// 建立连接
|
||||
$servers = [];
|
||||
foreach ($hosts as $i => $host) {
|
||||
$servers[] = [$host, $ports[$i] ?? $ports[0], 1];
|
||||
}
|
||||
|
||||
$this->handler->addServers($servers);
|
||||
|
||||
if ('' != $this->options['username']) {
|
||||
$this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
|
||||
$this->handler->setSaslAuthData($this->options['username'], $this->options['password']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name): bool
|
||||
{
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return $this->handler->get($key) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$result = $this->handler->get($this->getCacheKey($name));
|
||||
|
||||
return false !== $result ? $this->unserialize($result) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param integer|\DateTime $expire 有效时间(秒)
|
||||
* @return bool
|
||||
*/
|
||||
public function set($name, $value, $expire = null): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
$expire = $this->getExpireTime($expire);
|
||||
$value = $this->serialize($value);
|
||||
|
||||
if ($this->handler->set($key, $value, $expire)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function inc(string $name, int $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
if ($this->handler->get($key)) {
|
||||
return $this->handler->increment($key, $step);
|
||||
}
|
||||
|
||||
return $this->handler->set($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function dec(string $name, int $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
$value = $this->handler->get($key) - $step;
|
||||
$res = $this->handler->set($key, $value);
|
||||
|
||||
return !$res ? false : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param bool|false $ttl
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($name, $ttl = false): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return false === $ttl ?
|
||||
$this->handler->delete($key) :
|
||||
$this->handler->delete($key, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
return $this->handler->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存标签
|
||||
* @access public
|
||||
* @param array $keys 缓存标识列表
|
||||
* @return void
|
||||
*/
|
||||
public function clearTag(array $keys): void
|
||||
{
|
||||
$this->handler->deleteMulti($keys);
|
||||
}
|
||||
|
||||
}
|
||||
244
vendor/topthink/think-cache/src/cache/driver/Redis.php
vendored
Normal file
244
vendor/topthink/think-cache/src/cache/driver/Redis.php
vendored
Normal file
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
|
||||
* 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
|
||||
*
|
||||
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
|
||||
* @author 尘缘 <130775@qq.com>
|
||||
*/
|
||||
class Redis extends Driver
|
||||
{
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 6379,
|
||||
'password' => '',
|
||||
'select' => 0,
|
||||
'timeout' => 0,
|
||||
'expire' => 0,
|
||||
'persistent' => false,
|
||||
'prefix' => '',
|
||||
'tag_prefix' => 'tag:',
|
||||
'serialize' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $options 缓存参数
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
if (extension_loaded('redis')) {
|
||||
$this->handler = new \Redis;
|
||||
|
||||
if ($this->options['persistent']) {
|
||||
$this->handler->pconnect($this->options['host'], (int) $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
|
||||
} else {
|
||||
$this->handler->connect($this->options['host'], (int) $this->options['port'], $this->options['timeout']);
|
||||
}
|
||||
|
||||
if ('' != $this->options['password']) {
|
||||
$this->handler->auth($this->options['password']);
|
||||
}
|
||||
} elseif (class_exists('\Predis\Client')) {
|
||||
$params = [];
|
||||
foreach ($this->options as $key => $val) {
|
||||
if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
|
||||
$params[$key] = $val;
|
||||
unset($this->options[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
if ('' == $this->options['password']) {
|
||||
unset($this->options['password']);
|
||||
}
|
||||
|
||||
$this->handler = new \Predis\Client($this->options, $params);
|
||||
|
||||
$this->options['prefix'] = '';
|
||||
} else {
|
||||
throw new \BadFunctionCallException('not support: redis');
|
||||
}
|
||||
|
||||
if (0 != $this->options['select']) {
|
||||
$this->handler->select($this->options['select']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name): bool
|
||||
{
|
||||
return $this->handler->exists($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$value = $this->handler->get($this->getCacheKey($name));
|
||||
|
||||
if (is_null($value) || false === $value) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $this->unserialize($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param integer|\DateTime $expire 有效时间(秒)
|
||||
* @return bool
|
||||
*/
|
||||
public function set($name, $value, $expire = null): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
$expire = $this->getExpireTime($expire);
|
||||
$value = $this->serialize($value);
|
||||
|
||||
if ($expire) {
|
||||
$this->handler->setex($key, $expire, $value);
|
||||
} else {
|
||||
$this->handler->set($key, $value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function inc(string $name, int $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return $this->handler->incrby($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function dec(string $name, int $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return $this->handler->decrby($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($name): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$this->handler->del($this->getCacheKey($name));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$this->handler->flushDB();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存标签
|
||||
* @access public
|
||||
* @param array $keys 缓存标识列表
|
||||
* @return void
|
||||
*/
|
||||
public function clearTag(array $keys): void
|
||||
{
|
||||
// 指定标签清除
|
||||
$this->handler->del($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加(数组)缓存数据
|
||||
* @access public
|
||||
* @param string $name 缓存标识
|
||||
* @param mixed $value 数据
|
||||
* @return void
|
||||
*/
|
||||
public function push(string $name, $value): void
|
||||
{
|
||||
$this->handler->sAdd($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标签包含的缓存标识
|
||||
* @access public
|
||||
* @param string $tag 缓存标签
|
||||
* @return array
|
||||
*/
|
||||
public function getTagItems(string $tag): array
|
||||
{
|
||||
return $this->handler->sMembers($tag);
|
||||
}
|
||||
|
||||
}
|
||||
174
vendor/topthink/think-cache/src/cache/driver/Wincache.php
vendored
Normal file
174
vendor/topthink/think-cache/src/cache/driver/Wincache.php
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\cache\driver;
|
||||
|
||||
use think\cache\Driver;
|
||||
|
||||
/**
|
||||
* Wincache缓存驱动
|
||||
*/
|
||||
class Wincache extends Driver
|
||||
{
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'prefix' => '',
|
||||
'expire' => 0,
|
||||
'tag_prefix' => 'tag:',
|
||||
'serialize' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $options 缓存参数
|
||||
* @throws \BadFunctionCallException
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!function_exists('wincache_ucache_info')) {
|
||||
throw new \BadFunctionCallException('not support: WinCache');
|
||||
}
|
||||
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name): bool
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return wincache_ucache_exists($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = false)
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return wincache_ucache_exists($key) ? $this->unserialize(wincache_ucache_get($key)) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param integer|\DateTime $expire 有效时间(秒)
|
||||
* @return bool
|
||||
*/
|
||||
public function set($name, $value, $expire = null): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
$expire = $this->getExpireTime($expire);
|
||||
$value = $this->serialize($value);
|
||||
|
||||
if (wincache_ucache_set($key, $value, $expire)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function inc(string $name, int $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return wincache_ucache_inc($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function dec(string $name, int $step = 1)
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$key = $this->getCacheKey($name);
|
||||
|
||||
return wincache_ucache_dec($key, $step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($name): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
return wincache_ucache_delete($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
return wincache_ucache_clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存标签
|
||||
* @access public
|
||||
* @param array $keys 缓存标识列表
|
||||
* @return void
|
||||
*/
|
||||
public function clearTag(array $keys): void
|
||||
{
|
||||
wincache_ucache_delete($keys);
|
||||
}
|
||||
|
||||
}
|
||||
22
vendor/topthink/think-cache/src/exception/InvalidArgumentException.php
vendored
Normal file
22
vendor/topthink/think-cache/src/exception/InvalidArgumentException.php
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
namespace think\exception;
|
||||
|
||||
use Psr\Cache\InvalidArgumentException as Psr6CacheInvalidArgumentInterface;
|
||||
use Psr\SimpleCache\InvalidArgumentException as SimpleCacheInvalidArgumentInterface;
|
||||
|
||||
/**
|
||||
* 非法数据异常
|
||||
*/
|
||||
class InvalidArgumentException extends \InvalidArgumentException implements Psr6CacheInvalidArgumentInterface, SimpleCacheInvalidArgumentInterface
|
||||
{
|
||||
}
|
||||
31
vendor/topthink/think-cache/src/facade/Cache.php
vendored
Normal file
31
vendor/topthink/think-cache/src/facade/Cache.php
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
namespace think\facade;
|
||||
|
||||
use think\Facade;
|
||||
|
||||
/**
|
||||
* @see \think\CacheManager
|
||||
* @mixin \think\CacheManager
|
||||
*/
|
||||
class Cache extends Facade
|
||||
{
|
||||
/**
|
||||
* 获取当前Facade对应类名(或者已经绑定的容器对象标识)
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeClass()
|
||||
{
|
||||
return 'think\CacheManager';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user