Skip to content

Commit

Permalink
Merge pull request #35 from luyadev/status200caching
Browse files Browse the repository at this point in the history
[WIP] cache only status 200
  • Loading branch information
nadar authored Sep 22, 2021
2 parents bec27c5 + 84bcc7c commit f309bb3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/).

## 2.9.0 (22. September 2021)

+ [#35](https://github.com/luyadev/luya-headless/pull/35) Added strict caching property which is by default enabled. Strict cache will only cache data which are from status 200 response code.

## 2.8.1 (18. August 2021)

+ Replace static calls with `static` instead of `self` in order to override the default behavior in final classes.
Expand Down
6 changes: 6 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class Client
*/
public $cachePrefix;

/**
* @var boolean If enabled only requests with status code 200 are cached. Anything else won't be cached!
* @since 2.9.0
*/
public $strictCache = true;

/**
*
* @param string $accessToken
Expand Down
28 changes: 25 additions & 3 deletions src/base/AbstractRequestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ public function setIsCached()
*
* @param array|string $key The cache key. If an array is given it will be parsed and md5 encoded, therefore it won't be possible to encode.
* @param integer $ttl The number of seconds to store, its common that 0 is infinite.
* @param callable $fn The function which evaluates the content.
* @param callable $fn The function which evaluates the content. The callable must return an array with: http_status_code, request_headers, response_headers, response
* @return mixed
*/
public function getOrSetCache($key, $ttl, callable $fn)
protected function getOrSetCache($key, $ttl, callable $fn)
{
$cache = $this->client->getCache();

Expand All @@ -396,12 +396,34 @@ public function getOrSetCache($key, $ttl, callable $fn)
return $content;
}

/** @var array $content The content is always an array with: http_status_code, request_headers, response_headers, response */
$content = call_user_func($fn);

if (!$cache->set($key, $content, $ttl)) {
// if status code is not 200, the response will not be cached if enaled.
// otherwise it can happen that errors from the api will be cached for a long time.
if ($this->client->strictCache && $content['http_status_code'] !== 200) {
return $content;
}

if (!$cache->set($key, json_encode($content), $ttl)) {
throw new Exception("Unable to store the cache content for key '{$key}'.");
}

return $content;
}

/**
* Delete certain key fro mcache
*
* @param string $key
* @since 2.9.0
*/
public function deleteCache($key)
{
$cache = $this->client->getCache();

if ($cache) {
$cache->delete($key);
}
}
}
6 changes: 3 additions & 3 deletions src/collectors/CurlRequestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public function internalGet()
if ($this->isCachingEnabled()) {
$json = $this->getOrSetCache($this->getCacheKey(), $this->getCacheTtl(), function() {
$curl = $this->getCurl()->get($this->getRequestUrl());
return json_encode([
return [
'http_status_code' => $curl->http_status_code,
'request_headers' => $curl->request_headers,
'response_headers' => $curl->response_headers,
'response' => $curl->response
]);
];
});

$jsonArray = json_decode($json, true);
$jsonArray = is_array($json) ? $json : json_decode($json, true);

$this->curl = new Curl;
$this->curl->http_status_code = $jsonArray['http_status_code'];
Expand Down
52 changes: 52 additions & 0 deletions tests/base/AbstractActiveEndpointCachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,57 @@ public function getEndpointName()
$run2 = $class->get()->setCache(30)->response($client);

$this->assertTrue($run2->requestClient->getIsCached());

$run2->requestClient->deleteCache('foobar');
}

public function testCachingOf404Request()
{
$client = new Client(null, 'https://jsonplaceholder.typicode.com');
$client->strictCache = false;
$client->setCache(new DummySimpleCache());

$class = new class() extends Endpoint {
public function getEndpointName()
{
return 'thisdoesnotexists/1';
}
};

try {
$run1 = $class->get()->setCache(30)->response($client);
} catch (\Exception $e) {

}
$this->assertFalse($run1->requestClient->getIsCached());

$run2 = $class->get()->setCache(30)->response($client);

$this->assertTrue($run2->requestClient->getIsCached());
}

public function testCachingOf404RequestWithEnabledDoNotCacheBehavior()
{
$client = new Client(null, 'https://jsonplaceholder.typicode.com');
$client->strictCache = true;
$client->setCache(new DummySimpleCache());

$class = new class() extends Endpoint {
public function getEndpointName()
{
return 'thisdoesnotexistseither/1';
}
};

try {
$run1 = $class->get()->setCache(30)->response($client);
} catch (\Exception $e) {

}
$this->assertFalse($run1->requestClient->getIsCached());

$run2 = $class->get()->setCache(30)->response($client);

$this->assertFalse($run2->requestClient->getIsCached());
}
}

0 comments on commit f309bb3

Please sign in to comment.