CacheTrait.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
15 
16 /**
17  * CacheTrait adds support for cached responses.
18  *
19  * @author ingo herwig <ingo@wemove.com>
20  */
21 trait CacheTrait {
22 
23  /**
24  * Get the cache to use
25  * @return Cache
26  */
27  protected abstract function getCache();
28 
29  /**
30  * @see Format::isCached()
31  */
32  public function isCached(Response $response) {
33  $cacheId = $response->getCacheId();
34  return $this->getCache()->exists($this->getCacheSection($response), $cacheId);
35  }
36 
37  /**
38  * @see Format::getCacheDate()
39  */
40  public function getCacheDate(Response $response) {
41  $cacheId = $response->getCacheId();
42  return $this->getCache()->getDate($this->getCacheSection($response), $cacheId);
43  }
44 
45  /**
46  * Get the cache base section for the concreate format
47  * @note Sublcasses override this to define custom base cache sections
48  * @return String
49  */
50  protected function getBaseCacheSection() {
51  return 'cachedformat';
52  }
53 
54  /**
55  * Get the cache section for a response
56  * @param Response $response
57  * @return String
58  */
59  protected function getCacheSection(Response $response) {
60  $cacheId = $response->getCacheId();
61  return $this->getBaseCacheSection().'-'.$cacheId;
62  }
63 
64  /**
65  * Check if the response should be cached.
66  * @param $response
67  * @param Boolean
68  */
69  protected function isCaching(Response $response) {
70  return strlen($response->getCacheId()) > 0;
71  }
72 
73  /**
74  * Store the payload of the response in the cache.
75  * @param $response
76  * @param $payload String
77  */
78  protected function putInCache(Response $response, $payload) {
79  $this->getCache()->put($this->getCacheSection($response), $response->getCacheId(), $payload, $response->getCacheLifetime());
80  }
81 
82  /**
83  * Load the payload of the response from the cache.
84  * @param $response
85  * @return String
86  */
87  protected function getFromCache(Response $response) {
88  return $this->getCache()->get($this->getCacheSection($response), $response->getCacheId());
89  }
90 }
91 ?>
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
getCacheId()
Get the cache id.
putInCache(Response $response, $payload)
Store the payload of the response in the cache.
Definition: CacheTrait.php:78
trait CacheTrait
CacheTrait adds support for cached responses.
Definition: CacheTrait.php:21
isCached(Response $response)
Definition: CacheTrait.php:32
getCacheDate(Response $response)
Definition: CacheTrait.php:40
getCacheLifetime()
Get the lifetime of a cached response.
getBaseCacheSection()
Get the cache base section for the concreate format.
Definition: CacheTrait.php:50
getCacheSection(Response $response)
Get the cache section for a response.
Definition: CacheTrait.php:59
getFromCache(Response $response)
Load the payload of the response from the cache.
Definition: CacheTrait.php:87
isCaching(Response $response)
Check if the response should be cached.
Definition: CacheTrait.php:69
Cache defines the interface for cache implementations.
Definition: Cache.php:21