<?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>Web Cash &#187; cache</title>
	<atom:link href="http://www.earn-web-cash.com/tag/cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to Enable Client-Side Caching of Resized Images</title>
		<link>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/</link>
		<comments>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 00:37:44 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/</guid>
		<description><![CDATA[About a month ago, I wrote a tutorial on dynamically resizing images in PHP. Despite my best intentions, I never found the time to go back and rewrite the script to include server-side caching of the images created. However, one reader asked if it was possible to enable a client-side cache of a dynamically resized [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago, I wrote a tutorial on <a href="http://www.earn-web-cash.com/2008/01/30/resize-images-php/">dynamically resizing images in PHP</a>.  Despite my best intentions, I never found the time to go back and rewrite the script to include server-side caching of the images created.</p>
<p>However, one reader asked if it was possible to enable a client-side cache of a dynamically resized image.  It surely is, and it&#8217;s very simple.<br />
<span id="more-166"></span></p>
<h4>Dynamic Image Resizing: Revisited</h4>
<p>If you&#8217;ve got no clue how to resize an image in PHP, then you ought to go back and re-read the <a href="http://www.earn-web-cash.com/2008/01/30/resize-images-php/">original article</a>.  For the rest of you, here&#8217;s a quick refresher on what happens.</p>
<p>Our script takes a filename as a $_GET parameter.  If the file exists, the script loads it in as an image resource.  Using the GD library, we create a new image resource that is the proper size.</p>
<p>Finally, we use a content-type header to tell the browser to interpret the data as an image and we output the image data.  Like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Enabling a Client Side Cache</h4>
<p>In order to enable a client-side cache, all we need to do is add an extra header to the mix.</p>
<p>There is a set of headers in HTTP to handle this sort of need &#8211; Cache-Control.  You can use this to enable caching, disable caching, and set some restrictions on what can be cached for how long.</p>
<p>The option we&#8217;re most interested in is max-age=[seconds].  This response header is sent along with the data to tell the browser that it can re-use a cached copy as long as it is no older than the specified age.</p>
<p>To enable this with PHP, add another header() call before the imagepng() call, and set a Cache-Control header with an appropriate max-age.  Like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: max-age=3600&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Keep Request Queries Consistent</h4>
<p>With this system, you could set a pretty long max-age for the cache.  Assuming that the original image never changes, the user could cache a resized copy for months and your script would be happy.</p>
<p>However, you need to keep your query strings consistent for this to work.  Since your request includes a query string, that full string is included in the location that is cached.</p>
<p>According to your browser, it&#8217;s not the image that is cached, it&#8217;s the location&#8230;</p>
<pre>http://www.earn-web-cash.com/scripts/resize-image.php?image=http://www.earn-web-cash.com/wp-content/uploads/2008/01/dzone-screenshot.png</pre>
<p>If you were to change the query string in any way &#8211; leaving out the www, for example &#8211; the browser will think it is fetching a new resource and it won&#8217;t consult the cache.</p>
<p>This is especially important if you include size parameters in the query string to dynamically choose the new size.  You would need to be careful to keep the parameters in the same order every time for the cache to work properly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Cache Cleanup Script &#8211; Delete Old Files</title>
		<link>http://www.earn-web-cash.com/scripts-plugins-and-modules/cache-cleanup-script-php/</link>
		<comments>http://www.earn-web-cash.com/scripts-plugins-and-modules/cache-cleanup-script-php/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 00:41:00 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/scripts-plugins-and-modules/cache-cleanup-script-php/</guid>
		<description><![CDATA[The Cache Cleanup Script is designed to do just what it says &#8211; clean up temporary files created in some sort of cache directory. This script is meant to accompany some other kind of script that temporarily stores information in a file. How Cache Cleanup Works Here&#8217;s the source code in text format. At the [...]]]></description>
			<content:encoded><![CDATA[<p>The Cache Cleanup Script is designed to do just what it says &#8211; clean up temporary files created in some sort of cache directory.  This script is meant to accompany some other kind of script that temporarily stores information in a file.</p>
<h5>How Cache Cleanup Works</h5>
<p>Here&#8217;s the <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/cleanup.txt' title='Cache Cleanup Script'>source code</a> in text format.</p>
<p>At the top of the script, you need to set two values &#8211; $dirName and $timeLimit.</p>
<p>$dirName should be a local path to the directory you want to scan for old files.  $timeLimit should be the maximum allowable inactive time, in seconds.</p>
<p>The logic of the script is very straightforward.  First, we <code>scandir()</code> to get an array of filenames in our cache directory.</p>
<p>Then, we loop through each fileName.  If the file was last accessed before our timeLimit, we delete the file.  If not, we leave the file alone.</p>
<p>If the script attempts to delete a file, it will output a success or failure message.  Otherwise, there is no output.</p>
<h5>How to Implement Cache Cleanup</h5>
<p>First, set the configuration variables to your desired settings.  I intentionally refrained from using any kind of input into the script for security reasons.</p>
<p>Next, decide how you want to run the script.</p>
<p>If your host allows you to run cron jobs, the logical choice would be to make this a cron job run every so often.  Depending on how long you want the temporary files to remain on your server, I&#8217;d run the cron job once a day or so.</p>
<p>If your host doesn&#8217;t allow cron jobs, you could manually access the script each time you want to check for idle files and delete them.  This isn&#8217;t a bad choice if you want to check once a week or so, but it is kind of annoying to do it every day.</p>
<p>You could also incorporate this into your caching script.  Every time the script creates or accesses a cached file, it could call this script (as a function) and delete unused files.  This may not be a desirable option for performance reasons if you have a lot of cached files, but it wouldn&#8217;t be bad for smaller cache directories.</p>
<p>Anyhow, I just wrote <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/cleanup.txt' title='Cache Cleanup Script'>the script</a>.  You figure out how you want to implement it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dirName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cache'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// Path to the cache directory</span>
<span style="color: #000088;">$timeLimit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// Max time since a file has been accessed</span>
			<span style="color: #666666; font-style: italic;">//   before it should be deleted</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'.'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">fileatime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$timeLimit</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$file</span> deleted successfully.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$file</span> not deleted.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/scripts-plugins-and-modules/cache-cleanup-script-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

