there4 / slim-stash-middleware
Slim Stash Cache 中间件
v0.1.2
2014-11-01 17:11 UTC
Requires
- tedivm/stash: 0.12.*
This package is auto-updated.
Last update: 2024-09-20 04:47:34 UTC
README
使用 Stash 的缓存中间件层,为 Slim 提供
关于
一个简单的 Slim 中间件层,为端点提供缓存机制。
示例设置
以下是一个中间件设置的示例。它使用配置设置来启用缓存。
<?php // Stash Page Cache Middleware // ----------------------------------------------------------------------------- // A generalized way of caching the output of an endpoint $app->container->singleton('stash', function () use ($app) { if (!is_dir($app->config('caches.path'))) { mkdir($app->config('caches.path'), 0777, true); } $stashFileSystem = new \Stash\Driver\FileSystem(array( 'path' => $app->config('caches.path') )); return new \Stash\Pool($stashFileSystem); }); if ($app->config('enable_cache')) { $app->add(new \There4\Slim\Middleware\StashCache($app->stash)); }
并使用缓存的服务端点
<?php // Root of the site // ----------------------------------------------------------------------------- // Simple index page - no data $app->get('/', function () use ($app) { $app->response->allowCache = true; $app->response->cacheExpiration = 3600; $app->render('index.html'); }); // User Profile Page // ----------------------------------------------------------------------------- // This would need to be coupled with a cache invalidation on a user change $app->get('/profile', function () use ($app) { $user = $app->currentUser; $app->response->allowCache = true; $app->response->cacheExpiration = 3600; $app->response->signature = 'userProfile' . $user->id; $app->render('index.html'); });
快速 API 参考
- $app->response->allowCache
bool
启用缓存 - $app->response->cacheExpiration
int
缓存中保存数据的时间(秒) - $app->response->signature
mixed
留空以自动基于 URL。简单签名字符串。回调函数将被执行。也接受call_user_func
格式的数组。
待办事项
- 缓存预热脚本
- 测试