Skip to content

Commit

Permalink
Merge pull request #3 from idarex/master
Browse files Browse the repository at this point in the history
Add Cache
  • Loading branch information
lichunqiang authored Jul 18, 2016
2 parents b233ec4 + 41b8e80 commit 2d9a6d7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,31 @@ public function actions()
[Demo](https://github.com/lichunqiang/yii2-swagger-demo)


Caching
-------

```
public function actions()
{
return [
// ...
'api' => [
// ...
'cache' => 'cache',
'cacheKey' => 'api-swagger-cache', // default is 'api-swagger-cache'
],
];
}
```

#### Clear cache

Access clear cache url `YOUR_API_URL?clear-cache` or `YOUR_API_URL?api_key=YOUR_API_KEY&clear-cache`

Example: `curl 'http://localhost/v1/swagger/api?clear-cache'`

you will see: `Succeed clear swagger api cache.`

License
-------
![MIT](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)
57 changes: 55 additions & 2 deletions src/SwaggerApiAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Yii;
use yii\base\Action;
use yii\web\Response;
use yii\caching\Cache;

/**
* The api data output action.
Expand Down Expand Up @@ -53,6 +54,17 @@ class SwaggerApiAction extends Action
* @var array The options passed to `Swagger`, Please refer the `Swagger\scan` function for more information.
*/
public $scanOptions = [];
/**
* @var Cache|string|null the cache object or the ID of the cache application component that is used to store
* Cache the \Swagger\Scan
*/
public $cache = null;

/**
* @var string Cache key
* [[cache]] must not be null
*/
public $cacheKey = 'api-swagger-cache';

/**
* @inheritdoc
Expand All @@ -62,12 +74,53 @@ public function run()
Yii::$app->response->format = Response::FORMAT_JSON;

if (null !== $this->api_key
&& $this->api_key != Yii::$app->request->get($this->apiKeyParam)) {
&& $this->api_key != Yii::$app->getRequest()->get($this->apiKeyParam)
) {
return ['errcode' => 404, 'errmsg' => 'Permission denied'];
}

$swagger = \Swagger\scan($this->scanDir, $this->scanOptions);
$this->clearCache();

if ($this->cache !== null) {
$cache = $this->getCache();
if (($swagger = $cache->get($this->cacheKey)) === false) {
$swagger = $this->getSwagger();
$cache->set($this->cacheKey, $swagger);
}
} else {
$swagger = $this->getSwagger();
}

return $swagger;
}

/**
* Get swagger object
*
* @return \Swagger\Swagger
*/
protected function getSwagger()
{
return \Swagger\scan($this->scanDir, $this->scanOptions);
}

/**
* @return Cache
* @throws \yii\base\InvalidConfigException
*/
protected function getCache()
{
return is_string($this->cache) ? Yii::$app->get($this->cache, false) : $this->cache;
}

protected function clearCache()
{
$clearCache = Yii::$app->getRequest()->get('clear-cache', false);
if ($clearCache !== false) {
$this->getCache()->delete($this->cacheKey);

Yii::$app->response->content = 'Succeed clear swagger api cache.';
Yii::$app->end();
}
}
}

0 comments on commit 2d9a6d7

Please sign in to comment.