Skip to content

Commit 7a67d25

Browse files
authored
添加了静态文件缓存
1 parent 8b4d689 commit 7a67d25

10 files changed

Lines changed: 687 additions & 554 deletions

File tree

src/Controller/Auth.php

Lines changed: 96 additions & 214 deletions
Large diffs are not rendered by default.

src/Controller/Base.php

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Base{
1010
public $ctx;
1111
public $token;
1212

13-
public function get_query($string) {
13+
public function get_query($string) { //获取url参数
1414
$ctx = $this->ctx;
1515
if (empty($string)) {
1616
return false;
@@ -27,13 +27,13 @@ public function get_query($string) {
2727
return false;
2828
}
2929

30-
public function error($str = "失败") {
30+
public function error($str = "失败") { //自定义返回异常
3131
$array['Ok'] = false;
3232
$array['Msg'] = $str;
3333
$this->ctx->JSONP(200, $array);
3434
$this->ctx->abort();
3535
}
36-
public function new_token() {
36+
public function new_token() { //新建一个token
3737
$array = 'q.w.e.r.t.y.u.i.o.p.a.s.d.f.g.h.j.k.l.z.x.c.v.b.n.m.0.1.2.3.4.5.6.7.8.9';
3838
$array = explode('.', $array);
3939
$num = count($array);
@@ -48,27 +48,37 @@ public function new_token() {
4848
}
4949
return $res.date("ymdhis");
5050
}
51-
public function check_logined($token) {
51+
public function check_logined($token) {//判断是否登录
52+
$token_cache = new Cache($token);
53+
if($token_cache->exists and !$token_cache->expired){
54+
if($token_cache->value){
55+
return true;
56+
}
57+
}
58+
if(!$this->istoken($token)){
59+
return false;
60+
}
5261
$array = DB::instance()->table('User_Pool')-> where('token = ?', addslashes($token))->first();
5362
if (isset($array->user)) {
63+
$token_cache->Set(true);
5464
return true;
5565
} else {
5666
return false;
5767
}
5868
}
59-
public function get_userid($token) {
69+
public function get_userid($token) {//通过token获取用户id
6070
$array = DB::instance()->table('User_Pool')->where('token = ?', $token)->first();
6171
return $array->id;
6272
}
63-
public function strsafe($str) {
73+
public function strsafe($str) {//使用正则判断字符串是否含有非法字符
6474
$pattern = "#['!`~\/\\\%^&*()+=\$\#:;<>\]\[{}]#";
6575
if (preg_match($pattern, $str)) {
6676
return false;
6777
} else {
6878
return true;
6979
}
7080
}
71-
public function istoken($str) {
81+
public function istoken($str) {//使用正则判断是否为token
7282
$ereg = '/^[[:alnum:]]{25}$/';
7383
//$str='a!@#$%^';
7484
$num = preg_match($ereg, $str);
@@ -79,7 +89,7 @@ public function istoken($str) {
7989
return true;
8090
}
8191
}
82-
public function isdate($str){
92+
public function isdate($str){//使用正则判断是否为指定格式的日期
8393
$ereg = '/^[0-9]{4}(-[0-9]{2}){2}.[0-9]{2}(:[0-9]{2}){2}$/';
8494
//$str='a!@#$%^';
8595
$num = preg_match($ereg, $str);
@@ -90,11 +100,11 @@ public function isdate($str){
90100
return true;
91101
}
92102
}
93-
/***100000 2000 3000
94-
* 笔记id 笔记本id 标签id
95-
*
96-
* lastsysnctime = time()
97-
***/
103+
104+
/***
105+
* usn功能尚未开发
106+
***/
107+
98108
public function isusn($usn) {
99109
/***
100110
if (!isset($usn) or empty($usn)) {
@@ -208,49 +218,49 @@ public function tag_usn_update($id,$usn,$token){
208218
return $this->updateusn($new_usn,$token);
209219
***/
210220
}
211-
public function check_notebook_exist($title,$token){
221+
public function check_notebook_exist($title,$token){//通过笔记本名称判断笔记本是否存在
212222

213-
$userid = $this->get_userid($token);
223+
$userid = $this->get_userid($token);//通过token过去用户id
214224
$tb_name = 'User_'.$userid.'_Notebooks';
215-
$array = DB::instance()->table($tb_name)->where('title = ?',addslashes($title))->first();
216-
if(isset($array->title) and isset($array->uuid)){
225+
$array = DB::instance()->table($tb_name)->where('title = ?',addslashes($title))->first();//查询用户笔记本数据表
226+
if(isset($array->title) and isset($array->uuid)){//判断指定笔记本是否存在
217227
return true;
218228
}
219229
return false;
220230
}
221-
public function check_notebookid_exist($id,$token){
231+
public function check_notebookid_exist($id,$token){//通过笔记本id判断笔记本是否存在
222232

223-
if(!$this->istoken($id)){
233+
if(!$this->istoken($id)){//判断id是否非法
224234
return false;
225235
}
226-
$userid = $this->get_userid($token);
236+
$userid = $this->get_userid($token);//通过token获取用户id
227237
$tb_name = 'User_'.$userid.'_Notebooks';
228-
$array = DB::instance()->table($tb_name)->where('uuid = ?',$id)->first();
229-
if(isset($array->title) and isset($array->uuid)){
238+
$array = DB::instance()->table($tb_name)->where('uuid = ?',$id)->first();//查询用户笔记本数据表
239+
if(isset($array->title) and isset($array->uuid)){//判断指定笔记本是否存在
230240
return true;
231241
}
232242
return false;
233243
}
234-
public function check_noteid_exist($id,$token){
244+
public function check_noteid_exist($id,$token){//通过笔记id判断笔记本是否存在
235245

236-
if(!$this->istoken($id)){
246+
if(!$this->istoken($id)){//判断id是否非法
237247
return false;
238248
}
239-
$userid = $this->get_userid($token);
249+
$userid = $this->get_userid($token);//通过token获取用户id
240250
$tb_name = 'User_'.$userid.'_Notes';
241-
$array = DB::instance()->table($tb_name)->where('uuid = ?',$id)->first();
242-
if(isset($array->title) and isset($array->uuid)){
251+
$array = DB::instance()->table($tb_name)->where('uuid = ?',$id)->first();//查询用户笔记数据表
252+
if(isset($array->title) and isset($array->uuid)){//判断指定笔记是否存在
243253
return true;
244254
}
245255
return false;
246256
}
247-
public function firstload(Context $ctx){
257+
public function firstload(Context $ctx){//笔记,笔记本,标签类统一初始化方法
248258
$this->ctx = $ctx;
249259

250-
if(!$this->check_logined($this->get_query('token'))){
260+
if(!$this->check_logined($this->get_query('token'))){//判断是否登录
251261
$this->error('token错误');
252262
}
253263
$token = $this->get_query('token');
254-
$this->token = $token;
264+
$this->token = $token;//传递局部变量
255265
}
256266
}

src/Controller/Cache.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
/***
4+
* 静态文件缓存
5+
***/
6+
7+
namespace App\Controller;
8+
use Mix\Vega\Context;
9+
10+
class Cache{
11+
12+
public $key;
13+
public $value;
14+
public $CreatedTime;
15+
public $LastUpdateTime;
16+
public $ExpiredTime;
17+
18+
private $path;
19+
private $lock;
20+
21+
22+
23+
24+
public $expired;
25+
public $exists = false;
26+
27+
public function __construct($key){
28+
if(empty($key)){
29+
$this->lock = true;
30+
return false;
31+
}
32+
$key = md5($key);
33+
$path = $this->cachePath().$key.'.cache';
34+
$this->path = $path;
35+
36+
if(file_exists($path)){
37+
//文件存在
38+
$this->exists = true;
39+
$array = json_decode(file_get_contents($path),true);
40+
$this->value = $array['Value'];
41+
42+
$ExpiredTime = $array['ExpiredTime'];
43+
$LastUpdateTime = $array['LastUpdateTime'];
44+
45+
if($ExpiredTime<0){
46+
$this->expired = false;
47+
}else{
48+
if(time() - $LastUpdateTime >= $ExpiredTime){
49+
$this->expired = true;
50+
}else{
51+
$this->expired = false;
52+
}
53+
}
54+
55+
56+
}else{
57+
$this->exists = false;
58+
$this->expired = true;
59+
//文件不存在
60+
61+
62+
63+
}
64+
65+
}
66+
67+
public function Set($value,$time=-1){
68+
if($this->lock){
69+
return false;
70+
}
71+
72+
$new_time = time();
73+
74+
$data = array(
75+
'Md5-Key' => $this->key,
76+
'CreatedTime' => $new_time,
77+
'LastUpdateTime' => $new_time,
78+
'ExpiredTime' => $time,
79+
'Value' => $value,
80+
);
81+
82+
$this->CreatedTime = $new_time;
83+
$this->LastUpdateTime = $new_time;
84+
$this->flushExpired();
85+
$this->value = $value;
86+
87+
$json = json_encode($data);
88+
$this->write($json);
89+
}
90+
public function Read(){
91+
if($this->lock){
92+
return false;
93+
}
94+
return $this->value;
95+
}
96+
public function Status(){
97+
if($this->lock){
98+
return false;
99+
}
100+
$array = array(
101+
'$key' => $this->key,
102+
'$value' =>$this->value,
103+
'$path' => $this->path,
104+
'$lock' =>$this->lock,
105+
'$expired'=>$this->expired,
106+
'$exists'=>$this->exists,
107+
'$CreatedTime'=>$this->CreatedTime,
108+
'$LastUpdateTime'=>$this->LastUpdateTime,
109+
'$ExpiredTime'=>$this->ExpiredTime
110+
);
111+
return $array;
112+
}
113+
public function Delete(){
114+
if($this->lock){
115+
return false;
116+
}
117+
118+
if(unlink($this->path)){
119+
$this->exists = false;
120+
unset($this->value);
121+
unset($this->ExpiredTime);
122+
unset($this->CreatedTime);
123+
unset($this->LastUpdateTime);
124+
unset($this->expired);
125+
return true;
126+
}else{
127+
return false;
128+
}
129+
}
130+
131+
132+
private function cachePath(){
133+
$path = dirname(__FILE__);
134+
$path = str_replace('/src/Controller','/cache',$path).'/';
135+
if(!is_dir($path)){
136+
mkdir($path);
137+
}
138+
return $path;
139+
}
140+
private function write($data){
141+
$myfile = fopen($this->path, "w");
142+
fwrite($myfile, $data);
143+
fclose($myfile);
144+
$this->exists = true;
145+
}
146+
private function flushExpired(){
147+
if($this->ExpiredTime<0){
148+
$this->expired = false;
149+
}else{
150+
if(time() - $this->LastUpdateTime >= $this->ExpiredTime){
151+
$this->expired = true;
152+
}else{
153+
$this->expired = false;
154+
}
155+
}
156+
}
157+
}

src/Controller/Hello.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ class Hello
1212
*/
1313
public function index(Context $ctx)
1414
{
15-
$ctx->string(200, 'hello, world!');
15+
//$ctx->string(200, 'hello, world!');
16+
$mycache = new Cache('Hello World');
17+
if($mycache->exists and !$mycache->expired){
18+
$data = $mycache->value;
19+
$ctx->JSONP(200,$data);
20+
}else{
21+
$mycache->Set('这是缓存里的数据',10);
22+
$ctx->JSONP(200,'这不是缓存里的数据');
23+
}
24+
1625
}
1726

1827
}

0 commit comments

Comments
 (0)