3 namespace seekat\API\Call
;
5 use http\Client\Request
;
6 use http\Client\Response
;
7 use seekat\API\Call\Cache\Control
;
8 use seekat\API\Call\Cache\Service
;
9 use seekat\API\Call\Cache\Service\Hollow
;
20 * @param Service $cache
22 public function __construct(Service
$cache = null) {
23 $this->cache
= $cache ??
new Hollow
;
28 * @param Request $request
29 * @param Response $response
32 public function save(Request
$request, Response
$response) : bool {
33 $ctl = new Control($request);
34 if (!$ctl->isValid()) {
39 if ($time - 1 <= $response->getHeader("X-Cache-Time")) {
42 $response->setHeader("X-Cache-Time", $time);
44 return $this->cache
->store($ctl->getKey(), $response);
48 * Attempt to load call data
49 * @param Request $request
50 * @param Response $response out param
53 public function load(Request
$request, Response
&$response = null) : bool {
54 $ctl = new Control($request);
55 if (!$ctl->isValid()) {
59 if (!$this->cache
->fetch($ctl->getKey(), $response)) {
62 if ($ctl->isStale($response)) {
63 if (($lmod = $response->getHeader("Last-Modified"))) {
64 $request->setOptions(["lastmodified" => strtotime($lmod)]);
66 if (($etag = $response->getHeader("ETag"))) {
67 $request->setOptions(["etag" => $etag]);