Skip to content

Commit 703211e

Browse files
committed
新增缓存累,完善其他功能
1 parent 860743f commit 703211e

File tree

11 files changed

+194
-15
lines changed

11 files changed

+194
-15
lines changed

cache/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

composer.lock

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/cache.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
require "../vendor/autoload.php";
4+
5+
use Siam\Cache;
6+
var_dump(Cache::getInstance()->del('name'));// true
7+
var_dump(Cache::getInstance()->has('name'));// false
8+
$res = Cache::getInstance()->set('name', 'siam' , 1);
9+
var_dump(Cache::getInstance()->get('name'));// siam
10+
sleep(1);
11+
var_dump(Cache::getInstance()->get('name'));// null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* 缓存处理器
4+
* User: Siam
5+
* Date: 2020/7/2 0002
6+
* Time: 23:07
7+
*/
8+
9+
namespace Siam\AbstractInterface;
10+
11+
12+
interface CacheInterface
13+
{
14+
public function set($key, $value, $ex);
15+
public function get($key);
16+
public function del($key);
17+
public function has($key);
18+
}

src/siam/Api.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Api
1717
* @param string $code
1818
* @param array $data
1919
* @param string $msg
20+
* @return string
2021
*/
2122
public static function json($code, $data = [], $msg = '')
2223
{
@@ -36,8 +37,7 @@ public static function json($code, $data = [], $msg = '')
3637
# 记录log 会把所有输出的json记录起来 方便对接安卓调试
3738
Logger::getInstance()->log($json, "echoJson");
3839
}
39-
echo $json;
40-
exit;
40+
return $json;
4141
}
4242

4343
public static function send($code, $data = [], $msg = '')

src/siam/Cache.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* 缓存器
4+
* User: Siam
5+
* Date: 2020/7/2 0002
6+
* Time: 23:13
7+
*/
8+
9+
namespace Siam;
10+
11+
12+
use Siam\AbstractInterface\CacheInterface;
13+
use Siam\Component\Di;
14+
use Siam\Component\Singleton;
15+
use Siam\Trace\CacheFile;
16+
17+
class Cache implements CacheInterface
18+
{
19+
use Singleton;
20+
21+
/**
22+
* @var CacheInterface
23+
*/
24+
private $cacheDriver;
25+
26+
function __construct(CacheInterface $cache = null)
27+
{
28+
if ($cache === null) {
29+
$cache = new CacheFile(Di::getInstance()->get('CONFIG_CACHE_DIR'));
30+
}
31+
$this->cacheDriver = $cache;
32+
}
33+
34+
public function set($key, $value, $ex)
35+
{
36+
return $this->cacheDriver->set($key, $value, $ex);
37+
}
38+
39+
public function get($key)
40+
{
41+
return $this->cacheDriver->get($key);
42+
}
43+
44+
public function del($key)
45+
{
46+
return $this->cacheDriver->del($key);
47+
}
48+
49+
public function has($key)
50+
{
51+
return $this->cacheDriver->has($key);
52+
}
53+
}

src/siam/Curl.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function setTimeout($s)
4949
* @return mixed
5050
* @throws \Exception
5151
*/
52-
public function send($url, $data = null, $cert = [], $post = true)
52+
public function send($url, $data = null, $cert = [], $post = true, $callable = null)
5353
{
5454
# 初始化一个cURL会话
5555
$curl = curl_init();
@@ -96,6 +96,11 @@ public function send($url, $data = null, $cert = [], $post = true)
9696
}
9797

9898
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
99+
100+
// 执行自定义
101+
if (!empty($callable) && is_callable($callable)){
102+
call_user_func($callable, $curl);
103+
}
99104
$response = curl_exec($curl); // 执行一个cURL会话并且获取相关回复
100105

101106
if ($response === false) {

src/siam/Trace/CacheFile.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* 基于文件的缓存驱动
4+
* User: Siam
5+
* Date: 2020/7/2 0002
6+
* Time: 23:07
7+
*/
8+
9+
namespace Siam\Trace;
10+
11+
12+
use Siam\AbstractInterface\CacheInterface;
13+
14+
class CacheFile implements CacheInterface
15+
{
16+
protected $path = "../cache/";
17+
public function __construct($path = null)
18+
{
19+
if ($path !== null) $this->path = $path;
20+
}
21+
22+
public function set($key, $value, $ex = 0)
23+
{
24+
$data = [
25+
'key' => $key,
26+
'value' => $value,
27+
'ex' => time() + $ex,
28+
];
29+
$filename = $this->path . $key .".cache";
30+
$cache = json_encode($data,256);
31+
$int = file_put_contents($filename, $cache);
32+
33+
return $int === strlen($cache);
34+
}
35+
36+
public function get($key)
37+
{
38+
$filename = $this->path . $key .".cache";
39+
if (!is_file($filename)) return null;
40+
$data = file_get_contents($filename);
41+
$data = json_decode($data, true);
42+
if ($data['ex'] !== 0 && time() >= $data['ex']){
43+
$this->del($key);
44+
return null;
45+
}
46+
47+
return $data['value'];
48+
}
49+
50+
public function del($key)
51+
{
52+
$filename = $this->path . $key .".cache";
53+
if (!is_file($filename)) return false;
54+
unlink($filename);
55+
return true;
56+
}
57+
58+
public function has($key)
59+
{
60+
$filename = $this->path . $key .".cache";
61+
if (!is_file($filename)) return false;
62+
return true;
63+
}
64+
}

src/siam/Wechat/AccessToken.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
use Siam\Api;
13+
use Siam\Cache;
1314
use Siam\Component\Singleton;
1415
use Siam\Curl;
1516

@@ -66,8 +67,7 @@ public function getToken()
6667
*/
6768
private function getByCache()
6869
{
69-
// $cache = Cache::tag('token')->get($this->data['mism_appid'] . "_token");
70-
$cache = false;
70+
$cache = Cache::getInstance()->get($this->data['mism_appid'] . "_token");
7171
return $cache;
7272
}
7373

@@ -77,8 +77,8 @@ private function getByCache()
7777
*/
7878
private function getByHttp()
7979
{
80-
if (empty($this->data['mism_appid'])) Api::json('500', [], 'mism_appid为空');
81-
if (empty($this->data['mism_appsecret'])) Api::json('500', [], 'mism_appsecret为空');
80+
if (empty($this->data['mism_appid'])) exit(Api::json('500', [], 'mism_appid为空'));
81+
if (empty($this->data['mism_appsecret'])) exit(Api::json('500', [], 'mism_appsecret为空'));
8282

8383
$url = str_replace("__APPID__", $this->data['mism_appid'], self::$GET_TOKEN);
8484
$url = str_replace("__SECRET__", $this->data['mism_appsecret'], $url);
@@ -87,14 +87,14 @@ private function getByHttp()
8787
try {
8888
$res = $CURL->send($url);
8989
} catch (\Exception $e) {
90-
Api::json('500', [], '请求失败');
90+
exit(Api::json('500', [], '请求失败'));
9191
}
9292
$resA = json_decode($res, true);
9393

9494
$ticket = isset($resA['access_token']) ? $resA['access_token'] : false;
9595
if ($ticket) {
9696
$cacheName = $this->data['mism_appid'] . "_token";
97-
// Cache::tag('token')->set($cacheName, $ticket, 7200);
97+
Cache::getInstance()->set($cacheName, $ticket, 7200);
9898
return $ticket;
9999
}else{
100100
# 没有正常获取到那肯定有问题,输出提供给调试

src/siam/Wechat/ApiTicket.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Siam\Wechat;
1010

1111
use Siam\Api;
12+
use Siam\Cache;
1213
use Siam\Component\Singleton;
1314
use Siam\Curl;
1415

@@ -65,9 +66,8 @@ public function getTicket()
6566
*/
6667
private function getByCache()
6768
{
68-
if (empty($this->data['mism_appid'])) Api::json('500', [], 'mism_appid为空');
69-
// $cache = Cache::tag('ticket')->get($this->data['mism_appid'] . "_ticket");
70-
$cache = false;
69+
if (empty($this->data['mism_appid'])) exit(Api::json('500', [], 'mism_appid为空'));
70+
$cache = Cache::getInstance()->get($this->data['mism_appid'] . "_ticket");
7171
return $cache;
7272
}
7373

@@ -77,22 +77,22 @@ private function getByCache()
7777
*/
7878
private function getByHttp()
7979
{
80-
if (empty($this->token)) Api::json('500', [], 'token为空');
80+
if (empty($this->token)) exit(Api::json('500', [], 'token为空'));
8181

8282
$url = str_replace("__TOKEN__", $this->token, self::$GET_TICKET);
8383

8484
$CURL = Curl::getInstance();
8585
try {
8686
$res = $CURL->send($url);
8787
} catch (\Exception $e) {
88-
Api::json('500', [], '请求ticket失败');
88+
exit(Api::json('500', [], '请求ticket失败'));
8989
}
9090
$res = json_decode($res, true);
9191

9292
$ticket = $res['ticket'];
9393
if ($ticket) {
9494
$cacheName = $this->data['mism_appid'] . "_ticket";
95-
// Cache::tag('ticket')->set($cacheName, $ticket, 7200);
95+
Cache::getInstance()->set($cacheName, $ticket, 7200);
9696
return $ticket;
9797
}
9898
return false;

src/siam/Wechat/Work.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Work
1515
use Singleton;
1616

1717
private $lastData;
18+
private $key;
1819

1920
private function __construct($config = [])
2021
{

0 commit comments

Comments
 (0)