svn: /web/presentations/trunk/slides/intro/ rss_flickr.xml
| From: | Rasmus Lerdorf | Date: | Sat, 24 Sep 2011 06:19:58 +0000 |
| Subject: | svn: /web/presentations/trunk/slides/intro/ rss_flickr.xml | ||
| Groups: | php.pres | ||
| Request: | Send a blank email to pres+get-1749@lists.php.net to get a copy of this message | ||
rasmus Sat, 24 Sep 2011 06:19:58 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=317236
Log:
Add a cache
Changed paths:
U web/presentations/trunk/slides/intro/rss_flickr.xml
Modified: web/presentations/trunk/slides/intro/rss_flickr.xml
===================================================================
--- web/presentations/trunk/slides/intro/rss_flickr.xml 2011-09-24 05:51:38 UTC (rev 317235)
+++ web/presentations/trunk/slides/intro/rss_flickr.xml 2011-09-24 06:19:58 UTC (rev 317236)
@@ -1,8 +1,32 @@
<slide title="5-second Atom parser">
<break lines="1" />
-<example title="Recent Flickr Photos" fontsize="1.5em"
rfontsize="1em" result="1"><![CDATA[<?php
+<example title="Recent Flickr Photos" fontsize="1.5em"
rfontsize="1em" result="0"><![CDATA[<?php
$url = 'http://www.flickr.com/services/feeds/photos_public.gne';
foreach(simplexml_load_file($url)->entry as $it) echo $it->content;
?>]]></example>
+<example hide="1" result="1"><![CDATA[<?php
+define('MINSZ',10);
+function cache($url) {
+ if(isset($_GET['cache'])) $cache = (int)$_GET['cache'];
+ else $cache = 1;
+ $tmp = '/var/tmp/'.md5($url);
+ if(file_exists($tmp)) $st = stat($tmp);
+ if(!$cache || !$st || $st && ($st['size']<MINSZ ||
$st['mtime']<(time()-14400))) {
+ if($st && $st['size']>=MINSZ) touch($tmp); // Keep re-using entry
+ $stream = fopen($url,'r'); // until new is ready
+ $tmpf = tempnam('/tmp','YWS');
+ file_put_contents($tmpf, $stream);
+ fclose($stream);
+ rename($tmpf, $tmp);
+ }
+ return $tmp;
+}
+
+$url = 'http://www.flickr.com/services/feeds/photos_public.gne';
+$data = cache($url);
+foreach(simplexml_load_file($data)->entry as $it) echo $it->content;
+
+?>]]></example>
+
</slide>