Browse Source

caching podcasts

root 1 year ago
parent
commit
1804879022
2 changed files with 24 additions and 8 deletions
  1. 3 3
      podcast_ror.routing.yml
  2. 21 5
      src/Controller/PodcastRorController.php

+ 3 - 3
podcast_ror.routing.yml

@@ -6,7 +6,7 @@ podcast_ror.all:
     requirements:
         _access: 'TRUE'
     options:
-        no_cache: 'TRUE'
+         no_cache: 'FALSE'
 podcast_ror.bytype:
     path: '/podcast/by-type/{contentType}/podcast.xml'
     defaults:
@@ -15,7 +15,7 @@ podcast_ror.bytype:
     requirements:
         _access: 'TRUE'
     options:
-        no_cache: 'TRUE'
+         no_cache: 'FALSE'
 podcast_ror.bytrxid:
     path: '/podcast/by-trx-id/{trxID}/podcast.xml'
     defaults:
@@ -24,4 +24,4 @@ podcast_ror.bytrxid:
     requirements:
         _access: 'TRUE'
     options:
-        no_cache: 'TRUE'
+         no_cache: 'FALSE'

+ 21 - 5
src/Controller/PodcastRorController.php

@@ -94,18 +94,34 @@ class PodcastRorController extends ControllerBase {
     public function podcast($query, $description) {
         // TODO: add $title argument to the function
         $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig');
-        $tmpl_data = [ 'ror' => array('podcast' => $this->query($query),
+        $query_results = $this->query($query);
+        $tmpl_data = [ 'ror' => array('podcast' => $query_results,
             'url' => \Drupal::service('path.current')->getPath(),
             'description' => $description,
             'title' => 'Radio Onda Rossa',
         )];
-        //$variables['ror']['url'] = 
-        $xml = $template->render($tmpl_data);
-        $resp = new Response($xml, 200, array(
+        $headers = array(
             'Content-Type' => 'application/rss+xml',
             'Access-Control-Allow-Origin' => '*',
             'Access-Control-Allow-Methods' => 'GET',
-        ));
+        );
+        $nodes = $tmpl_data['ror']['podcast']['nodes'];
+        $lastmodtime = 0;
+        if( !empty($nodes) )  {
+            foreach($nodes as $nid => $node) {
+                $modtime = intval($node['changed'][0]['value']);
+                if($modtime > $lastmodtime) {
+                    $lastmodtime = $modtime;
+                }
+            }
+        }
+        $xml = $template->render($tmpl_data);
+        $resp = new Response($xml, 200, $headers);
+        $resp->setPublic();
+        $resp->setTtl(300);
+        if($lastmodtime !== 0)  {
+            $resp->setLastModified(\DateTime::createFromFormat('U',$lastmodtime));
+        }
         return $resp;
     }
     public function podcastAll() {