<?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>High Fibre Programming &#187; touch</title>
	<atom:link href="http://www.4pmp.com/tag/touch/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.4pmp.com</link>
	<description>PHP, MySQL, C, Java, Linux and other great after dinner speech topics</description>
	<lastBuildDate>Tue, 17 Jan 2012 09:10:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Java: touch &#8211; set file last modified time</title>
		<link>http://www.4pmp.com/2009/12/java-touch-set-file-last-modified-time/</link>
		<comments>http://www.4pmp.com/2009/12/java-touch-set-file-last-modified-time/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 10:30:58 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[modified]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[touch]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=217</guid>
		<description><![CDATA[I just discovered that I need to touch() a file in Java. It appears there isn&#8217;t a way to do this using the standard Java library, so I rolled my own: import java.io.*; import java.util.Date; class Touch { /** * Touches a given file * * @author Nick Giles &#60;http://www/4pmp.com/&#62; */ public static void main(String [...]]]></description>
			<content:encoded><![CDATA[<p>I just discovered that I need to <a href="http://www.computerhope.com/unix/utouch.htm">touch()</a> a file in Java.   It appears there isn&#8217;t a way to do this using the standard Java library, so I rolled my own:</p>
<pre class="brush: java;">
import java.io.*;
import java.util.Date;

class Touch
{
    /**
     * Touches a given file
     *
     * @author Nick Giles &lt;http://www/4pmp.com/&gt;
     */
    public static void main(String args[])
    {
        try
        {
            // Create a new file object for the file we want to touch
            File f = new File(&quot;touch.txt&quot;);

            // See if the file already exists
            if (f.exists())
            {
                // The file already exists, so just update its last modified time
                if (!f.setLastModified(System.currentTimeMillis()))
                {
                    throw new IOException(&quot;Could not touch file&quot;);
                }
            }
            else
            {
                // The file doesn't exist, so create it
                f.createNewFile();
            }
        }
        catch (SecurityException e)
        {
            System.err.println(&quot;Security Error: &quot; + e.getMessage());
        }
        catch (IOException e)
        {
            System.err.println(&quot;IO Error: &quot; + e.getMessage());
        }
    }
}
</pre>
<p>Just like the Linux command, if the input is a directory then it will update the last modified time for the directory but not recurse into it.   If you need to recurse then the above can easily be extended to recurse itself using f.isDirectory() and f.listFiles().   If anyone needs this then just let me know and I&#8217;ll rustle it up, but for now I&#8217;ll leave that as en exercise for the reader <img src='http://www.4pmp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2009/12/java-touch-set-file-last-modified-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PEAR Cache_Lite &#8211; preventing stampeding</title>
		<link>http://www.4pmp.com/2009/09/pear-cache_lite-preventing-stampeding/</link>
		<comments>http://www.4pmp.com/2009/09/pear-cache_lite-preventing-stampeding/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 20:18:13 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[cache_lite]]></category>
		<category><![CDATA[improving]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[stampeding]]></category>
		<category><![CDATA[timeout]]></category>
		<category><![CDATA[touch]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=35</guid>
		<description><![CDATA[The PEAR Cache_Lite package is an excellent caching system; lightweight and fast, however when put into use on a high-traffic website a few issues came to light. The first problem we hit was stampeding. What&#8217;s stampeding? Stampeding is the situation when a request, let&#8217;s say from User1, arrives for a cached item that has expired. [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://pear.php.net/package/Cache_Lite/">PEAR Cache_Lite</a> package is an excellent caching system; lightweight and fast, however when put into use on a high-traffic website a few issues came to light.   The first problem we hit was stampeding.   </p>
<h4>What&#8217;s stampeding?</h4>
<p>Stampeding is the situation when a request, let&#8217;s say from <em>User1</em>, arrives for a cached item that has expired.   The cache system returns boolean false and the process of rebuilding that cached data begins, calling the database, formatting the data and so on.   </p>
<p>If, during this process of rebuilding the cached data, another request arrives for the same cached item, let&#8217;s say from <em>User2</em>, another process of rebuilding the cached data begins.   This is because the process started by <em>User1</em> has not yet finished and so the cache system still returns boolean false when requested for the cached item.</p>
<p>So now we have two processes running, regenerating the same cache item.   The situation can get out of hand if more and more requests for the same cache item arrive &#8211; causing the load on the web server or database to increase, and everything to potentially grind to a halt.</p>
<h4>The solution</h4>
<p>What&#8217;s required is for the cache system to know that a particular cache is being regenerated and therefore return the old cache until the new data has been regenerated.   Thankfully this can be achieved very simply with the addition of just one extra line of code into the Cache_Lite class, Lite.php.</p>
<p>The trick is to <a href="http://www.php.net/touch">touch()</a> the cache file immediately after realising it has expired in the <em>Cache_Lite::get()</em> function.   After touching the file, the get function will return false and the calling code will regenerate the cache data.   </p>
<p><code>@touch($this->_file);</code></p>
<p>By touching the file, the modification time of the cache file is set to the current time and therefore all subsequent requests will think the cache is still valid and return the old data.   Once the first process has regenerated the data, it saves it and the cache file once again contains up-to-date data.</p>
<h4>Some finishing touches</h4>
<p>By touching the cache, processes immediately following the one which is regenerating the fresh data will return out-of-date data, albeit by a matter of seconds &#8211; which in most cases really won&#8217;t matter nor be noticed.</p>
<p>If, however, something were to happen to the process regenerating the data, such as an uncaught exception, database timeout, etc that it would fail and not save the cache, then the old cache will be valid until it expires again &#8211; so it will have effectively been valid for twice its intended lifetime.   </p>
<p>We can limit this by setting the modification time in the touch command to be the current time, minus the cache lifetime, plus 60s &#8211; which would mean that if the regenerating process were to fail, the cache would only be valid for another 60s.</p>
<p><code>@touch($this->_file, time() - abs($this->_lifeTime) + 60);</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2009/09/pear-cache_lite-preventing-stampeding/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

