<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>AjaxMasters Blog &#187; cache</title> <atom:link href="http://blog.ajaxmasters.com/tag/cache/feed/" rel="self" type="application/rss+xml" /><link>http://blog.ajaxmasters.com</link> <description>Just another WordPress weblog</description> <lastBuildDate>Thu, 24 Jun 2010 11:55:25 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Use ajax to create a cacheing system</title><link>http://blog.ajaxmasters.com/use-ajax-to-create-a-cacheing-system/</link> <comments>http://blog.ajaxmasters.com/use-ajax-to-create-a-cacheing-system/#comments</comments> <pubDate>Tue, 29 Sep 2009 10:49:52 +0000</pubDate> <dc:creator>Chocksy</dc:creator> <category><![CDATA[Tutorial]]></category> <category><![CDATA[cache]]></category> <category><![CDATA[html]]></category> <category><![CDATA[imageroll]]></category> <category><![CDATA[images]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[php]]></category> <category><![CDATA[plugin]]></category> <category><![CDATA[system]]></category> <category><![CDATA[vi.sualize.us]]></category> <category><![CDATA[wordpress]]></category><guid
isPermaLink="false">http://blog.ajaxmasters.com/?p=163</guid> <description><![CDATA[<a
href="http://blog.ajaxmasters.com/use-ajax-to-create-a-cacheing-system/"><img
align="left" hspace="5" width="150" height="150" src="http://blog.ajaxmasters.com/wp-content/uploads/2009/09/cache-170x170.png" class="alignleft wp-post-image tfe" alt="Cache Pic" title="cache" /></a><p>As I said in my last post, I wanted from the first time to make a cacheing system for my imageroll page. Because i take the images from vi.sualize.us servers and we don't want to make to much trafic and make stuff worse for them.</p><p>So what i did is simple, i made a class that extracts the rss data from theirs servers and get the images to display them. I do this every time someone visits the page.</p>]]></description> <content:encoded><![CDATA[<p><div
id="attachment_164" class="wp-caption alignleft" style="width: 287px"><a
href="http://blog.ajaxmasters.com/wp-content/uploads/2009/09/cache.png"><img
src="http://blog.ajaxmasters.com/wp-content/uploads/2009/09/cache.png" alt="Cache Pic" title="cache" width="277" height="277" class="size-full wp-image-164" /></a><p
class="wp-caption-text">Cache Pic</p></div><p>As I said in my last post, I wanted from the first time to make a cacheing system for my imageroll page. Because i take the images from vi.sualize.us servers and we don&#8217;t want to make to much trafic and make stuff worse for them.</p><p>So what i did is simple, i made a class that extracts the rss data from theirs servers and get the images to display them. I do this every time someone visits the page. If any image is new for the first time we display the image from vi.sualize.us and run a functions in javascript to check for new images. If any new images exist then we cache them in a folder on our server and the next time a user visits the page that image will be shown from our server. Pretty simple.</p><p>For now i&#8217;ll just show you the stuff that makes this thing run and if you want i can share the whole script so that you can make one for your blog or website.</p><p>I&#8217;ll also try to make a WordPress plugin so you can integrate it easily in your blog.</p><p>So here we go. We start with the function that checks for new images:</p><pre class="brush: php;">
functio checkCache($image_url){
            $path=pathinfo($image_url);
            $img_cached = 'cache/'.$path['filename'].'.'.$path['extension'];
            if(!file_exists($img_cached)) {
                createCacheImg($image_url);
            }
}
</pre><p>This function just checks if the image exists in the &#8220;<b>cache</b>&#8221; folder. If it&#8217;s not there then we run the &#8220;<b>createCacheImg</b>&#8221; function.</p><p>We now need a function to get the image so we check if the image is in the cache folder or not so we know what to display.</p><pre class="brush: php;">
    function getImage($or_img) {
        $path=pathinfo($or_img);
        $filename=$path['filename'];
        $img_cached = 'cache/'.$filename.'.'.$path['extension'];
        if(file_exists($img_cached)) {
            $url=$img_cached;
        }else {
            $url=$or_img;
        }

        return $url;
    }
</pre><p>Very simple function that just returns the right url of the image.</p><p>Now we cache the image.</p><pre class="brush: php;">
function createCacheImg($image){
        $srcsize = getimagesize($image);
        $w = $srcsize[0];
        $h = $srcsize[1];

        $path=pathinfo($image);
        $dest='cache/'.$path['filename'].'.'.$path['extension'];
        if($path['extension']=='jpg' || $path['extension']=='jpeg' || $path['extension']=='JPG') {
            $src_img = imagecreatefromjpeg($image);
        }else if($path['extension']=='gif') {
                $src_img = imagecreatefromgif($image);
            }else if($path['extension']=='png') {
                    $src_img = imagecreatefrompng($image);
                }
        $img_cpy=imagecreatetruecolor($w,$h);
        imagecopy($img_cpy, $src_img, 0, 0, 0, 0, $w, $h);
        if($path['extension']=='jpg' || $path['extension']=='jpeg' || $path['extension']=='JPG') {
            imagejpeg($img_cpy,$dest);
        }else if($path['extension']=='gif') {
                imagegif($img_cpy,$dest);
            }else if($path['extension']=='png') {
                    imagepng($img_cpy,$dest);
                }
        imagedestroy($src_img);
}
</pre><p>This function will save the image into the cache folder. Now we need a new file &#8220;<b>checkcache.php</b>&#8221; where we put this code:</p><pre class="brush: php;">
include('functions.php');
$image=$_GET['image'];
checkCache($image);
</pre><p> Right now we should have this kind of structure in our imageroll folder: A folder &#8220;<b>cache</b>&#8221; a file &#8220;<b>functions.php</b>&#8221; where we place all these functions we just did and a file &#8220;<b>index.php</b>&#8221; where we run the javascript function and display the images.</p><p>The &#8220;<b>index.php</b>&#8221; file requires this code:</p><pre class="brush: xml;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
    &lt;head&gt;

        &lt;title&gt;
            Image ROLL
        &lt;/title&gt;

        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
        &lt;script src=&quot;http://www.google.com/jsapi&quot;&gt;&lt;/script&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;  google.load(&quot;jquery&quot;, &quot;1.3.2&quot;); &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
&lt;input type=&quot;hidden&quot; id=&quot;image&quot; value=&quot;http://test.com/image.jpg&quot;&gt;
&lt;img src=&quot;&lt;?=getImage($image)?&gt;&quot; alt=&quot;test image&quot;/&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>We have here a simple index.php file that imports the google api and jquery so we can run a simple ajax function. This is the javascript code that you should insert in this file or a separate one if you want:</p><pre class="brush: jscript;">

            $(document).ready(function() {
                checkCache();
            });

            function checkCache(){
                var page=$('#image');
                var url='checkcache.php';
                var pars='page='+page.val();
                $.get(url,pars);
            }
</pre><p>This will run this function after the page loads and it will check for new images. So this is just a simple script that will make you pages load faster expecialy if you load the images from outside of your server.</p><p>If you liked this i&#8217;ll go and make a more complex one and even share the imageroll script i did. That until i find the time and make a Wordpress Plugin.</p> ]]></content:encoded> <wfw:commentRss>http://blog.ajaxmasters.com/use-ajax-to-create-a-cacheing-system/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 8/27 queries in 0.018 seconds using disk

Served from: blog.ajaxmasters.com @ 2010-07-31 13:30:57 -->