Skip to content

Commit

Permalink
Merge pull request #5 from maurobonfietti/develop
Browse files Browse the repository at this point in the history
Use redis cache.
  • Loading branch information
maurobonfietti committed Aug 12, 2018
2 parents 4a4466f + 2e1c68c commit 1554dec
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -17,7 +17,8 @@
"monolog/monolog": "^1.17",
"respect/validation": "^1.1",
"palanik/corsslim": "dev-slim3",
"vlucas/phpdotenv": "^2.4"
"vlucas/phpdotenv": "^2.4",
"predis/predis": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
Expand Down
52 changes: 51 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Controller/User/BaseUser.php
Expand Up @@ -21,16 +21,16 @@ abstract class BaseUser extends BaseController
*/
public function __construct(Container $container)
{
$this->container = $container;
$this->logger = $container->get('logger');
$this->userService = $container->get('user_service');
}

/**
* @return UserService
*/
protected function getUserService()
{
return $this->userService;
return $this->container->get('user_service');
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Controller/User/DeleteUser.php
Expand Up @@ -23,6 +23,10 @@ public function __invoke($request, $response, $args)
$this->setParams($request, $response, $args);
$result = $this->getUserService()->deleteUser($this->args['id']);

$client = new \Predis\Client();
$key = 'api-rest-slimphp:user:'.$this->args['id'];
$client->del($key);

return $this->jsonResponse('success', $result, 200);
}
}
10 changes: 9 additions & 1 deletion src/Controller/User/GetOneUser.php
Expand Up @@ -21,7 +21,15 @@ class GetOneUser extends BaseUser
public function __invoke($request, $response, $args)
{
$this->setParams($request, $response, $args);
$result = $this->getUserService()->getUser($this->args['id']);
$client = new \Predis\Client();
$key = 'api-rest-slimphp:user:'.$this->args['id'];
$value = $client->get($key);
if (!is_null($value)) {
$result = json_decode($value);
} else {
$result = $this->getUserService()->getUser($this->args['id']);
$client->set($key, json_encode($result));
}

return $this->jsonResponse('success', $result, 200);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Controller/User/UpdateUser.php
Expand Up @@ -24,6 +24,10 @@ public function __invoke($request, $response, $args)
$input = $this->getInput();
$result = $this->getUserService()->updateUser($input, $this->args['id']);

$client = new \Predis\Client();
$key = 'api-rest-slimphp:user:'.$this->args['id'];
$client->set($key, json_encode($result));

return $this->jsonResponse('success', $result, 200);
}
}

0 comments on commit 1554dec

Please sign in to comment.