Skip to content

Commit

Permalink
Add default expiry of 7 days
Browse files Browse the repository at this point in the history
  • Loading branch information
fredsted committed Apr 28, 2018
1 parent 94a1fa1 commit 79e022b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/Console/Commands/MigrateToRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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'));
}

/**
Expand Down
29 changes: 28 additions & 1 deletion app/Storage/Redis/RequestStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/Storage/Redis/TokenStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 79e022b

Please sign in to comment.