Implementing simple Redis cache for RSS.
This commit is contained in:
parent
e6eee29316
commit
90090061f0
3
.env
3
.env
|
@ -17,6 +17,9 @@ APP_DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@127.0.0.1:5436/test?application_
|
|||
MAGNETICOD_DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@127.0.0.1:5436/test?application_name=magnetico_web
|
||||
###< doctrine/doctrine-bundle ###
|
||||
|
||||
# Redis cache
|
||||
REDIS_DSN=redis://127.0.0.1:6379/0
|
||||
|
||||
###> sentry/sentry-symfony ###
|
||||
SENTRY_DSN=
|
||||
###< sentry/sentry-symfony ###
|
||||
|
|
|
@ -28,3 +28,9 @@ framework:
|
|||
|
||||
# APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues)
|
||||
#app: cache.adapter.apcu
|
||||
prefix_seed: magnetico-web
|
||||
pools:
|
||||
magneticod.cache:
|
||||
adapter: cache.adapter.redis
|
||||
provider: '%env(REDIS_DSN)%'
|
||||
default_lifetime: 600
|
||||
|
|
|
@ -5,16 +5,24 @@ namespace App\Api\V1\Controller;
|
|||
use App\Feed\RssGenerator;
|
||||
use Symfony\Component\HttpFoundation\{Request, Response};
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class RssController extends AbstractController
|
||||
{
|
||||
private const CONTENT_TYPE = 'application/rss+xml';
|
||||
private const CACHE_KEY = 'rss_last';
|
||||
private const CACHE_LIFETIME = 600;
|
||||
|
||||
public function last(Request $request, RssGenerator $generator): Response
|
||||
public function last(Request $request, RssGenerator $generator, CacheInterface $magneticodCache): Response
|
||||
{
|
||||
$page = (int) $request->query->get('page', '1');
|
||||
|
||||
$xml = $generator->generateLast($page);
|
||||
$xml = $magneticodCache->get(self::CACHE_KEY, function (ItemInterface $item) use ($generator, $page) {
|
||||
$item->expiresAfter(self::CACHE_LIFETIME);
|
||||
|
||||
return $generator->generateLast($page);
|
||||
});
|
||||
|
||||
return new Response($xml, 200, ['Content-Type' => self::CONTENT_TYPE]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue