-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SWP-2082] Caching with singleton like (#17)
Added a new property withAuthentication to make have access from parent class. withAuthentication makes sure that the access token is fetched and attached to request header. Renamed confusing httpClient for apiClient. Added laravel compatibility table to README.md Added phpcs Added a cache provider, to allow different cache drivers/adapters usable by this library. Added Laravel Cache Adapter supports from Laravel 5+. Added AbstractCacheableRequest to cache requests, any request can be cached by extending AbstractCacheableRequest. Added time conversion between seconds to minutes for access token expires_in response field.
- Loading branch information
1 parent
da257ac
commit f6c4aa4
Showing
42 changed files
with
1,050 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace PodPoint\Reviews\Cache; | ||
|
||
use PodPoint\Reviews\Request\AbstractCacheableRequest; | ||
|
||
/** | ||
* Class AbstractHasCacheTtlInResponse | ||
*/ | ||
abstract class AbstractHasCacheTtlInResponse extends AbstractCacheableRequest | ||
{ | ||
/** | ||
* The name of the field which has expires or ttl. | ||
* | ||
* @var string | ||
*/ | ||
protected $cacheTtlResponseField; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getCacheTtlResponseField(): string | ||
{ | ||
return $this->cacheTtlResponseField; | ||
} | ||
|
||
/** | ||
* @param string $cacheTtlResponseField | ||
*/ | ||
public function setCacheTtlResponseField(string $cacheTtlResponseField): void | ||
{ | ||
$this->cacheTtlResponseField = $cacheTtlResponseField; | ||
} | ||
|
||
/** | ||
* Returns the ttl from the body of the response. | ||
* | ||
* @param array $responseBody | ||
* @param int|null $default | ||
* | ||
* @return int | ||
*/ | ||
public function getCacheableTtlFromResponse(array $responseBody, int $default = null): int | ||
{ | ||
if ($this->cacheTtlResponseField && isset($responseBody[$this->cacheTtlResponseField])) { | ||
$ttl = (int) $responseBody[$this->cacheTtlResponseField]; | ||
|
||
return $this->convertFromSecondsToMinutes($ttl); | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
/** | ||
* Converts from seconds to minutes. | ||
* | ||
* @param $value | ||
* @return int | ||
*/ | ||
protected function convertFromSecondsToMinutes(int $value): int | ||
{ | ||
if ($value <= 0) { | ||
return 0; | ||
} | ||
|
||
return (int) $value / 60; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace PodPoint\Reviews\Cache; | ||
|
||
use PodPoint\Reviews\Exceptions\CacheAdapterException; | ||
|
||
/** | ||
* Class CacheProvider | ||
*/ | ||
class CacheProvider | ||
{ | ||
/** | ||
* Cache instance of driver/adapter. | ||
* | ||
* @var null | ||
*/ | ||
private static $instance = null; | ||
|
||
/** | ||
* CacheFactory constructor. | ||
*/ | ||
private function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Set instance. | ||
* | ||
* @param mixed $instance | ||
* | ||
* @return void | ||
*/ | ||
public static function setInstance($instance): void | ||
{ | ||
self::$instance = $instance; | ||
} | ||
|
||
/** | ||
* Get instance of Cache Driver/Adapter. | ||
* | ||
* @return null | ||
* @throws CacheAdapterException | ||
*/ | ||
public static function getInstance() | ||
{ | ||
if (self::$instance == null) { | ||
throw new CacheAdapterException(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace PodPoint\Reviews\Cache; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
|
||
/** | ||
* Class LaravelCache | ||
*/ | ||
class LaravelCacheAdapter | ||
{ | ||
/** | ||
* Returns cache by key. | ||
* | ||
* @param string $key | ||
* @param null $default | ||
* | ||
* @return mixed | ||
*/ | ||
public function get(string $key, $default = null) | ||
{ | ||
return Cache::get($key, $default); | ||
} | ||
|
||
/** | ||
* Check if cache exists. | ||
* | ||
* @param $key | ||
* | ||
* @return boolean | ||
*/ | ||
public function has($key): bool | ||
{ | ||
return Cache::has($key); | ||
} | ||
|
||
/** | ||
* Sets cache. | ||
* | ||
* @param string $key | ||
* @param $value | ||
* @param null $ttl | ||
* | ||
* @return boolean | ||
*/ | ||
public function set(string $key, $value, $ttl = null) | ||
{ | ||
return Cache::put($key, $value, $ttl); | ||
} | ||
|
||
/** | ||
* Deletes cache. | ||
* | ||
* @param string $key | ||
* | ||
* @return boolean | ||
*/ | ||
public function delete(string $key): bool | ||
{ | ||
return Cache::forget($key); | ||
} | ||
|
||
public function clear() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Returns multiple caches. | ||
* | ||
* @param array $keys | ||
* @param null $default | ||
* | ||
* @return array | ||
*/ | ||
public function getMultiple(array $keys, $default = null) | ||
{ | ||
$cache = []; | ||
|
||
foreach ($keys as $key) { | ||
$cache[$key] = Cache::get($key); | ||
} | ||
|
||
return $cache; | ||
} | ||
|
||
/** | ||
* Sets multiple cache values. | ||
* | ||
* @param array $values | ||
* @param integer|null $ttl | ||
*/ | ||
public function setMultiple(array $values, int $ttl = null) | ||
{ | ||
foreach ($values as $cacheKey => $value) { | ||
$this->set($cacheKey, $value, $ttl); | ||
} | ||
} | ||
|
||
/** | ||
* Deletes multiple cache keys. | ||
* | ||
* @param array $keys | ||
*/ | ||
public function deleteMultiple(array $keys) | ||
{ | ||
foreach ($keys as $key) { | ||
$this->delete($key); | ||
} | ||
} | ||
} |
Oops, something went wrong.