Merged in feature_rss_cache (pull request #41)

Implementing simple Redis cache for RSS.
This commit is contained in:
Alexey Eschenko 2020-07-05 01:41:34 +00:00
commit a001d186ba
3 changed files with 19 additions and 2 deletions

3
.env
View file

@ -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 MAGNETICOD_DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@127.0.0.1:5436/test?application_name=magnetico_web
###< doctrine/doctrine-bundle ### ###< doctrine/doctrine-bundle ###
# Redis cache
REDIS_DSN=redis://127.0.0.1:6379/0
###> sentry/sentry-symfony ### ###> sentry/sentry-symfony ###
SENTRY_DSN= SENTRY_DSN=
###< sentry/sentry-symfony ### ###< sentry/sentry-symfony ###

View file

@ -28,3 +28,9 @@ framework:
# APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues) # APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues)
#app: cache.adapter.apcu #app: cache.adapter.apcu
prefix_seed: magnetico-web
pools:
magneticod.cache:
adapter: cache.adapter.redis
provider: '%env(REDIS_DSN)%'
default_lifetime: 600

View file

@ -5,16 +5,24 @@ namespace App\Api\V1\Controller;
use App\Feed\RssGenerator; use App\Feed\RssGenerator;
use Symfony\Component\HttpFoundation\{Request, Response}; use Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class RssController extends AbstractController class RssController extends AbstractController
{ {
private const CONTENT_TYPE = 'application/rss+xml'; 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'); $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]); return new Response($xml, 200, ['Content-Type' => self::CONTENT_TYPE]);
} }