Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 5cd01a5

Browse files
committed
Introduced "Cacheable Response" feature
1 parent c9215ae commit 5cd01a5

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ Or in your template:
6262
{% endcache %}
6363
</div>
6464
```
65+
#### :bulb: Introducing Cacheable Responses (V3 only)
66+
As of the V3 there's a new, easier and cleaner way to setup HTTP cache to decrease your server bandwidth along your CPU load: Cacheable Responses.
67+
And it's pretty easy to implement:
68+
```php
69+
/**
70+
* @Route("/cached", name="cached")
71+
*/
72+
public function cachedAction(Phpfastcache $phpfastcache, Request $request): Response
73+
{
74+
return (new CacheableResponse($phpfastcache->get('filecache'), $request))->getResponse('cache_key', 3600, function () {
75+
return new Response('Random bytes: ' . \random_bytes(255));
76+
});
77+
}
78+
```
79+
`CacheableResponse` is provided by `\Phpfastcache\Bundle\Response\CacheableResponse`.
80+
This class will handle responses headers (cache-control, etag, etc...) and http status (304 Not modified).
81+
6582
#### :boom: phpFastCache Bundle support
6683
Found an issue or had an idea ? Come here [here](https://github.com/PHPSocialNetwork/phpfastcache-bundle/issues) and let us know !
6784

src/Response/CacheableResponse.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Geolim4
5+
* Date: 12/05/2018
6+
* Time: 02:18
7+
*/
8+
9+
namespace Phpfastcache\Bundle\Response;
10+
11+
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
12+
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
class CacheableResponse
17+
{
18+
const RESPONSE_PREFIX = '__CACH_RESP__';
19+
20+
/**
21+
* @var ExtendedCacheItemPoolInterface
22+
*/
23+
protected $request;
24+
25+
/**
26+
* @var ExtendedCacheItemPoolInterface
27+
*/
28+
protected $cacheInstance;
29+
30+
/**
31+
* CachePromise constructor.
32+
* @param ExtendedCacheItemPoolInterface $cacheInstance
33+
*/
34+
public function __construct(ExtendedCacheItemPoolInterface $cacheInstance, Request $request)
35+
{
36+
$this->cacheInstance = $cacheInstance;
37+
$this->request = $request;
38+
}
39+
40+
/**
41+
* @param string $cacheKey
42+
* @param int|\DateInterval $expiresAfter
43+
* @param callable $callback
44+
* @return mixed
45+
* @throws PhpfastcacheLogicException
46+
*/
47+
public function getResponse(string $cacheKey, $expiresAfter = null, callable $callback): Response
48+
{
49+
$cacheKey = self::RESPONSE_PREFIX . $cacheKey;
50+
$cacheItem = $this->cacheInstance->getItem($cacheKey);
51+
$cacheResponse = $cacheItem->get();
52+
53+
/**
54+
* No isHit() test here as we directly
55+
* test if the cached response object
56+
* is effectively a "Response" object
57+
*/
58+
if (!($cacheResponse instanceof Response)) {
59+
$response = $callback();
60+
if($response instanceof Response){
61+
$cacheItem->expiresAfter($expiresAfter);
62+
63+
$response->setExpires($cacheItem->getExpirationDate());
64+
$response->setSharedMaxAge($cacheItem->getTtl());
65+
$response->headers->addCacheControlDirective('must-revalidate', true);
66+
$response->setEtag(md5($response->getContent()));
67+
$response->setPublic();
68+
69+
$cacheItem->set($response);
70+
$this->cacheInstance->save($cacheItem);
71+
$cacheResponse = $response;
72+
}else{
73+
throw new PhpfastcacheLogicException('Your callback response MUST return a valid Symfony HTTP Foundation Response object');
74+
}
75+
}else{
76+
$cacheResponse->isNotModified($this->request);
77+
}
78+
79+
return $cacheResponse;
80+
}
81+
}

0 commit comments

Comments
 (0)