modified 规则全面缓存,提升Api请求速度

This commit is contained in:
zhaoxiang 2017-04-27 14:43:46 +08:00
parent 839a403f24
commit 7141eac9bb
3 changed files with 56 additions and 5 deletions

View File

@ -30,6 +30,7 @@ class ApiManageController extends BaseController {
$data = I('post.');
$res = D('ApiList')->where(array('id' => $data['id']))->save($data);
if( $res === false ) {
S('ApiInfo_' . $data['hash'], 0);
$this->ajaxError('操作失败');
} else {
$this->ajaxSuccess('添加成功');
@ -57,6 +58,8 @@ class ApiManageController extends BaseController {
if( IS_POST ) {
$id = I('post.id');
if( $id ) {
$hash = D('ApiList')->where(array('id' => $id))->getField('hash');
S('ApiInfo_' . $hash, 0);
D('ApiList')->open(array('id' => $id));
$this->ajaxSuccess('操作成功');
} else {
@ -69,6 +72,8 @@ class ApiManageController extends BaseController {
if( IS_POST ) {
$id = I('post.id');
if( $id ) {
$hash = D('ApiList')->where(array('id' => $id))->getField('hash');
S('ApiInfo_' . $hash, 0);
D('ApiList')->close(array('id' => $id));
$this->ajaxSuccess('操作成功');
} else {
@ -81,6 +86,8 @@ class ApiManageController extends BaseController {
if( IS_POST ) {
$id = I('post.id');
if( $id ) {
$hash = D('ApiList')->where(array('id' => $id))->getField('hash');
S('ApiInfo_' . $hash, 0);
D('ApiList')->del(array('id' => $id));
$this->ajaxSuccess('操作成功');
} else {

View File

@ -77,6 +77,11 @@ class FieldsManageController extends BaseController {
if ($res === false) {
$this->ajaxError('操作失败');
} else {
if ($data['type'] == 0) {
S('ApiRequest_' . $data['hash'], 0);
} else {
S('ApiResponse_' . $data['hash'], 0);
}
$this->ajaxSuccess('添加成功');
}
} else {
@ -94,6 +99,12 @@ class FieldsManageController extends BaseController {
if (IS_POST) {
$id = I('post.id');
if ($id) {
$detail = D('ApiFields')->where(array('id' => $id))->find();
if ($detail['type'] == 0) {
S('ApiRequest_' . $detail['hash'], 0);
} else {
S('ApiResponse_' . $detail['hash'], 0);
}
D('ApiFields')->where(array('id' => $id))->delete();
$this->ajaxSuccess('操作成功');
} else {
@ -105,6 +116,7 @@ class FieldsManageController extends BaseController {
public function upload() {
if (IS_POST) {
$hash = I('post.hash');
$type = I('post.type');
$jsonStr = I('post.jsonStr');
$jsonStr = html_entity_decode($jsonStr);
$data = json_decode($jsonStr, true);
@ -112,7 +124,7 @@ class FieldsManageController extends BaseController {
$this->handle($data['data'], $dataArr);
$old = D('ApiFields')->where(array(
'hash' => $hash,
'type' => I('post.type')
'type' => $type
))->select();
$oldArr = array_column($old, 'showName');
$newArr = array_column($dataArr, 'showName');
@ -130,6 +142,12 @@ class FieldsManageController extends BaseController {
}
D('ApiFields')->addAll($addData);
}
if ($type) {
S('ApiRequest_' . $hash, 0);
} else {
S('ApiResponse_' . $hash, 0);
}
S('ApiReturnType_' . $hash, 0);
$this->ajaxSuccess('操作成功');
} else {
$this->display();

View File

@ -25,16 +25,42 @@ class ApiController extends BaseController {
public function index() {
$getArr = I('get.');
$postArr = I('post.');
$this->apiDetail = M('ApiList')->where(array('hash' => $getArr['hash'], 'status' => 1))->find();
//获取ApiInfo根据
$this->apiDetail = S('ApiInfo_' . $getArr['hash']);
if (!$this->apiDetail) {
$this->apiDetail = M('ApiList')->where(array('hash' => $getArr['hash'], 'status' => 1))->find();
S('ApiInfo_' . $getArr['hash'], json_encode($this->apiDetail));
} else {
$this->apiDetail = json_decode($this->apiDetail, true);
}
if (empty($this->apiDetail)) {
Response::error(ReturnCode::NOT_EXISTS, '非法的API标识');
}
ApiLog::setApiInfo($this->apiDetail);
$this->apiRequest = M('ApiFields')->where(array('hash' => $getArr['hash'], 'type' => 0))->select();
$this->apiResponse = M('ApiFields')->where(array('hash' => $getArr['hash'], 'type' => 1))->select();
$returnType = M('ApiFields')->where(array('hash' => $getArr['hash'], 'showName' => 'data', 'type' => 1))->find();
$this->apiRequest = S('ApiRequest_' . $getArr['hash']);
if (!$this->apiRequest) {
$this->apiRequest = M('ApiFields')->where(array('hash' => $getArr['hash'], 'type' => 0))->select();
S('ApiRequest_' . $getArr['hash'], json_encode($this->apiRequest));
} else {
$this->apiRequest = json_decode($this->apiRequest, true);
}
$this->apiResponse = S('ApiResponse_' . $getArr['hash']);
if (!$this->apiResponse) {
$this->apiResponse = M('ApiFields')->where(array('hash' => $getArr['hash'], 'type' => 1))->select();
S('ApiResponse_' . $getArr['hash'], json_encode($this->apiResponse));
} else {
$this->apiResponse = json_decode($this->apiResponse, true);
}
$returnType = S('ApiReturnType_' . $getArr['hash']);
if (!$returnType) {
$returnType = M('ApiFields')->where(array('hash' => $getArr['hash'], 'showName' => 'data', 'type' => 1))->getField('dataType');
S('ApiReturnType_' . $getArr['hash'], $returnType);
}
Response::setDataType($returnType['dataType']);
$this->header = apache_request_headers();