aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2018-05-29 11:10:07 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2018-06-04 08:48:54 +0200
commita34495933e83c4c850308e3448a16160162eb65a (patch)
treef0996c9fe47219b744a79890506e97238b2a36c1 /lib
parent36d74047f7ead966438ae7958ca7d7f816860515 (diff)
downloadnextcloud-server-a34495933e83c4c850308e3448a16160162eb65a.tar.gz
nextcloud-server-a34495933e83c4c850308e3448a16160162eb65a.zip
Move caching logic to response
This avoids having to do it at all the places we want cached responses. We can't inject the ITimeFactor without breaking public API. However we can perfectly overwrite the service (resulting in the same testable effect). Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r--lib/public/AppFramework/Http/Response.php16
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/public/AppFramework/Http/Response.php b/lib/public/AppFramework/Http/Response.php
index 512b312dae1..a6f5afd3c18 100644
--- a/lib/public/AppFramework/Http/Response.php
+++ b/lib/public/AppFramework/Http/Response.php
@@ -35,6 +35,7 @@
namespace OCP\AppFramework\Http;
use OCP\AppFramework\Http;
+use OCP\AppFramework\Utility\ITimeFactory;
/**
* Base class for responses. Also used to just send headers.
@@ -95,12 +96,23 @@ class Response {
* @return $this
* @since 6.0.0 - return value was added in 7.0.0
*/
- public function cacheFor($cacheSeconds) {
-
+ public function cacheFor(int $cacheSeconds) {
if($cacheSeconds > 0) {
$this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds . ', must-revalidate');
+
+ // Old scool prama caching
+ $this->addHeader('Pragma', 'public');
+
+ // Set expires header
+ $expires = new \DateTime();
+ /** @var ITimeFactory $time */
+ $time = \OC::$server->query(ITimeFactory::class);
+ $expires->setTimestamp($time->getTime());
+ $expires->add(new \DateInterval('PT'.$cacheSeconds.'S'));
+ $this->addHeader('Expires', $expires->format(\DateTime::RFC2822));
} else {
$this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
+ unset($this->headers['Expires'], $this->headers['Pragma']);
}
return $this;