-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCacheCurl.php
147 lines (126 loc) · 3.71 KB
/
CacheCurl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace lspbupt\curl;
use lspbupt\common\helpers\ArrayHelper;
use yii\caching\Cache;
use yii\di\Instance;
/**
* Class CacheCurl
* 这个Curl主要设计用来缓存重复查询的curl请求结果
* 两种cache策略 一种走redis 一种放内存里,随request结束而释放
*
* @package lspbupt\curl
*/
class CacheCurl extends CurlHttp
{
public $defaultPrefix = 'CacheUrl_';
public $cache = 'cache';
/* 默认缓存时间 10min */
public $cacheTime = 600;
/* 是否打开cache缓存 默认关闭 */
public $enableCache = false;
public $cacheKey = '';
/* 获取缓存的时候从params里要排除的参数, 比如 ['_ts', '_nonce', '_sign'] */
public $excludes = [];
public $useMemoryCache = false;
/* 当启用内部数组缓存时候, 用来存放缓存数据的数组 */
private $_cacheArr = [];
public function init()
{
parent::init();
$this->cache = Instance::ensure($this->cache, Cache::class);
}
/**
* @deprecated 推荐使用send方法
*
* @param string $action
* @param array $params
*/
public function httpExec($action = '/', $params = [])
{
return $this->send($action, $params);
}
public function send($action = '/', $params = [])
{
if ($this->enableCache) {
$this->cacheKey = $this->getKey($action, $params);
$data = $this->getCacheData($this->cacheKey);
if ($this->isDebug()) {
echo "\n注意cache开启中:" . "\n";
echo "\n请求结果:".$data."\n";
}
if ($data !== false) {
return json_decode($data, true);
}
if ($this->isDebug()) {
echo "\ncache没有命中 走正常请求流程:" . "\n";
}
}
return parent::send($action, $params);
}
//请求之后的操作
protected function afterCurl($data)
{
if ($this->enableCache && $this->cacheKey) {
$this->setCacheData($this->cacheKey, $data, $this->cacheTime);
}
return parent::afterCurl($data);
}
private function getCacheData($cacheKey) {
if ($this->useMemoryCache) {
$data = ArrayHelper::getValue($this->_cacheArr, $cacheKey, false);
} else {
$data = $this->cache->get($cacheKey);
}
return $data;
}
private function setCacheData($cacheKey, $data, $cacheTime) {
if ($this->useMemoryCache) {
$this->_cacheArr[$cacheKey] = $data;
} else {
$this->cache->set($cacheKey, $data, $cacheTime);
}
}
public function enableCache()
{
$this->enableCache = true;
$this->useMemoryCache = false;
return $this;
}
public function disableCache()
{
$this->enableCache = false;
$this->useMemoryCache = false;
return $this;
}
public function enableMemCache()
{
$this->enableCache = true;
$this->useMemoryCache = true;
return $this;
}
public function disableMemCache()
{
$this->enableCache = false;
$this->useMemoryCache = false;
return $this;
}
/**
* 根据action params获取redis key
*
* @return string
*/
private function getKey($action = '/', $params = [])
{
if ($params) {
if ($this->excludes) {
foreach ($this->excludes as $exclude) {
unset($params[$exclude]);
}
}
sort($params);
}
$arr = [$this->getUrl(), $action, md5(json_encode($params))];
$key = $this->defaultPrefix . implode('_', $arr);
return $key;
}
}