Bristol SU Support Package
CachedClientDecorator.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
12 class CachedClientDecorator implements Client
13 {
14 
19  private $client;
20 
26  private $cache;
27 
33  {
34  $this->client = $client;
35  $this->cache = $cache;
36  }
37 
47  public function request($method, $uri, array $options = [])
48  {
49  if ($this->isCacheable($method)) {
50  return $this->cache->remember($this->getKey($method, $uri, $options), 7200, function() use ($method, $uri, $options){
51  return $this->forwardCall($method, $uri, $options);
52  });
53  }
54  return $this->forwardCall($method, $uri, $options);
55  }
56 
66  private function getKey($method, $uri, $options)
67  {
68  return self::class.$method.$uri.json_encode($options);
69  }
70 
77  private function isCacheable($method)
78  {
79  return in_array(strtoupper($method), ['GET', 'HEAD', 'OPTIONS', 'TRACE']);
80  }
81 
82 
92  private function forwardCall($method, $uri, $options)
93  {
94  return $this->client->request($method, $uri, $options);
95  }
96 }