slimframwork 使用 middleware缓存页面

slim cache middleware

中间件 Core\Middleware\Cache

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
<?php
namespace Core\Middleware;
class Cache
{
protected $db;
//Constructor
public function __construct($db) {
$this->db = $db;
}
/**
* Example middleware invokable class
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke($request, $response, $next)
{
$path = $request->getUri()->getPath();
$query= $request->getUri()->getQuery();
if(!empty($query)){
$path.='?'.$query;
}
$cachetime=time()-60; //获取60s内的缓存
$cache=$this->db->get('cache','*',['AND'=>['path'=>$path,'created_at[>]'=>$cachetime]]);
if($cache){
$t2=microtime(true);
$response->getBody()->write(htmlspecialchars_decode($cache['body']).
'<!--cached at'.date("Y/m/d H:i:s",$cache['created_at']).'-->
<!--app cost time '.(round(($t2-APP_START),5)*1000).'ms-->');
return $response;
}
$response = $next($request, $response);
if ($response->getStatusCode() == 200) {
$id=$this->db->get('cache','id',['path'=>$path]);
$cachebody=htmlspecialchars($response->getBody());
$data=['path'=>$path,'body'=>$cachebody,'created_at'=>time()];
if($id){
$this->db->update('cache',$data,['id'=>$id]);
}else{
$this->db->insert('cache',$data);
}
}
$t2=microtime(true);
$response->getBody()->write('<!--no cache app cost time '
.(round(($t2-APP_START),5)*1000).'ms-->');
return $response;
}
}

###入口文件index.php调用cache缓存中间件

1
2
3
4
5
6
7
8
9
10
11
12
<?php
define('APP_START',microtime(true));
require '../vendor/autoload.php';
$app = new Slim\App(
new \Slim\Container(
include '../core/container.config.php'
)
);
$app->group('/blog', function () {
$this->get('[/{slug}]', 'BlogController')->setName('blog');
$this->get('/article/{id:[0-9]+}', 'BlogController:article')->setName('blog.post');
})->add(new Core\Middleware\Cache());

###缓存的话,建议使用redis,memcache 下面是使用memcache来缓存的代码

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
<?php
namespace Core\Middleware;
class Cache
{
/**
* Example middleware invokable class
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke($request, $response, $next)
{
$path = $request->getUri()->getPath();
$query= $request->getUri()->getQuery();
if(!empty($query)){
$path.='?'.$query;
}
$memcache_obj = memcache_connect("127.0.0.1", 11211);
$cache=$memcache_obj->get($path);
if($cache){
$t2=microtime(true);
$response->getBody()->write(htmlspecialchars_decode($cache));
return $response;
}
$response = $next($request, $response);
if ($response->getStatusCode() == 200) {
$cachebody=htmlspecialchars($response->getBody());
$memcache_obj->set($path,$cachebody,false,60);
}
$t2=microtime(true);
$response->getBody()->write('<!--with no memcache app cost time
'.(round(($t2-APP_START),5)*1000).'ms-->');
return $response;
}
}

没怎么使用过memcache,不过看说明是使用connect连接的,程序执行完,连接会自动关闭.