From 79e022ba38c02b9fcf1cdbdb6d85f7d732273421 Mon Sep 17 00:00:00 2001 From: sfr Date: Sat, 28 Apr 2018 19:42:05 +0200 Subject: [PATCH] Add default expiry of 7 days --- app/Console/Commands/MigrateToRedis.php | 5 +++++ app/Storage/Redis/RequestStore.php | 29 ++++++++++++++++++++++++- app/Storage/Redis/TokenStore.php | 7 +++++- config/app.php | 3 +++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/MigrateToRedis.php b/app/Console/Commands/MigrateToRedis.php index c23cb88..f369df2 100644 --- a/app/Console/Commands/MigrateToRedis.php +++ b/app/Console/Commands/MigrateToRedis.php @@ -84,6 +84,8 @@ private function insertToken(Token $token) $requestsKey = sprintf('%s:%s', $tokenKey, 'requests'); if ($this->redis->exists($tokenKey)) { + $this->redis->expire($tokenKey, config('app.expiry')); + $this->redis->expire($requestsKey, config('app.expiry')); return; } @@ -99,6 +101,9 @@ private function insertToken(Token $token) $this->insertRequest($requestsKey, $request); } }); + + $this->redis->expire($tokenKey, config('app.expiry')); + $this->redis->expire($requestsKey, config('app.expiry')); } /** diff --git a/app/Storage/Redis/RequestStore.php b/app/Storage/Redis/RequestStore.php index 260ed7c..5bd1ead 100644 --- a/app/Storage/Redis/RequestStore.php +++ b/app/Storage/Redis/RequestStore.php @@ -24,6 +24,11 @@ public function __construct() $this->redis = Redis::connection(config('database.redis.connection')); } + /** + * @param Token $token + * @param string $requestId + * @return Request + */ public function find(Token $token, $requestId) { $result = $this->redis->hget(Request::getIdentifier($token->uuid), $requestId); @@ -32,9 +37,17 @@ public function find(Token $token, $requestId) throw new NotFoundHttpException('Request not found'); } + $this->redis->expire(Request::getIdentifier($token->uuid), config('app.expiry')); + return new Request(json_decode($result, true)); } + /** + * @param Token $token + * @param int $page + * @param int $perPage + * @return Collection|static + */ public function all(Token $token, $page = 0, $perPage = 50) { $keys = array_reverse( @@ -63,17 +76,31 @@ function ($item) { ); } + /** + * @param Token $token + * @param Request $request + * @return Request + */ public function store(Token $token, Request $request) { - return $this + $result = $this ->redis ->hmset( Request::getIdentifier($token->uuid), $request->uuid, json_encode($request->attributes()) ); + + $this->redis->expire(Request::getIdentifier($token->uuid), config('app.expiry')); + + return $result; } + /** + * @param Token $token + * @param Request $request + * @return Request + */ public function delete(Token $token, Request $request) { return $this diff --git a/app/Storage/Redis/TokenStore.php b/app/Storage/Redis/TokenStore.php index 712e0e1..f328a22 100644 --- a/app/Storage/Redis/TokenStore.php +++ b/app/Storage/Redis/TokenStore.php @@ -34,15 +34,20 @@ public function find($tokenId) throw new NotFoundHttpException('Token not found'); } + $this->redis->expire(Token::getIdentifier($tokenId), config('app.expiry')); + return new Token(json_decode($result, true)); } + /** + * @param Token $token + * @return int + */ public function countRequests(Token $token) { return $this->redis->hlen(Request::getIdentifier($token->uuid)); } - /** * @param Token $token * @return Token diff --git a/config/app.php b/config/app.php index 739af15..1839486 100644 --- a/config/app.php +++ b/config/app.php @@ -12,6 +12,9 @@ // will have to create a new URL. 'max_requests' => env('WEBHOOK_MAX_REQUESTS', 500), + // Requests and tokens will expire in 7 days (default) + 'expiry' => env('WEBHOOK_EXPIRY', 604800), + /* |-------------------------------------------------------------------------- | Application Environment