<?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>The Why and The How &#187; REST</title> <atom:link href="http://www.thewhyandthehow.com/category/rest/feed/" rel="self" type="application/rss+xml" /><link>http://www.thewhyandthehow.com</link> <description>Just another WordPress weblog</description> <lastBuildDate>Fri, 09 Jul 2010 02:32:51 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.8.4</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Memcache, memcache, memcache!</title><link>http://www.thewhyandthehow.com/memcache-memcache-memcache/</link> <comments>http://www.thewhyandthehow.com/memcache-memcache-memcache/#comments</comments> <pubDate>Wed, 20 May 2009 23:31:29 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[REST]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[memcache]]></category> <category><![CDATA[programming]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=706</guid> <description><![CDATA[Possibly the single-most important piece of advice I give to every software developer right now is to use memcache (or memcached to be specific). I&#8217;ve become an evangelist of the software since working for Squidoo.
Memcache is just exactly what it says it is &#8212; a memory cache daemon (service). It&#8217;s lightweight and very fast and [...]]]></description> <content:encoded><![CDATA[<p>Possibly the single-most important piece of advice I give to every software developer right now is to <strong>use <a
href="http://www.danga.com/memcached/">memcache</a></strong> (or memcached to be specific). I&#8217;ve become an evangelist of the software since working for <a
href="http://www.squidoo.com/building-squidoo">Squidoo</a>.</p><p>Memcache is just exactly what it says it is &#8212; a memory cache daemon (service). It&#8217;s lightweight and very fast and can be used for all kinds of caching: page, database, remote data, etc., etc.</p><p>I use it on this blog in conjunction with fetching the Twitter comments that appear in the right-hand sidebar on the home page, for example. Here&#8217;s the general pseudo code for how memcache is used:</p><pre>
  key = generate_cache_key()
  if (value = is_key_in_cache(key))
    return value
  get_uncached_value() // DB, REST, complex code, etc.
  save_to_cache(key, value)
</pre><p>My actual implementation for the Twitter backtweets looks something like this:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p7063');">[<span
id="p7063_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p706code3'); return false;">View Code</a> PHP</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p7063"><td
class="code" id="p706code3"><pre class="php"><span style="color: #000033;">$uri</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://backtweets.com/search.json?q=www.thewhyandthehow.com&amp;key=API-KEY'</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$cache_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'rest-service-'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><span style="color: #000033;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> Helpers<span style="color: #339933;">::</span><span style="color: #004000;">cacheGet</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$cache_key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #b1b100;">return</span> <span style="color: #000033;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> Helpers<span style="color: #339933;">::</span><span style="color: #004000;">remoteLoad</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// check valid $result here</span>
&nbsp;
Helpers<span style="color: #339933;">::</span><span style="color: #004000;">cacheSet</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$cache_key</span><span style="color: #339933;">,</span> <span style="color: #000033;">$result</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000033;">$result</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>The helper functions look like this (class declaration not shown for brevity):</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p7064');">[<span
id="p7064_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p706code4'); return false;">View Code</a> PHP</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p7064"><td
class="code" id="p706code4"><pre class="php">    <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> getCache<span style="color: #009900;">&#40;</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>self<span style="color: #339933;">::</span><span style="color: #000033;">$cache</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000033;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Memcache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000033;">$temp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span>MEMCACHE_HOST<span style="color: #339933;">,</span> MEMCACHE_PORT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         self<span style="color: #339933;">::</span><span style="color: #000033;">$cache</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$temp</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">return</span> self<span style="color: #339933;">::</span><span style="color: #000033;">$cache</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/**
     * set a value to the memcache
     *
     * @param string key name
     * @param mixed value
     * @param integer timeout (default 1 day)
     * @param boolean compression flag
     * @return boolean success
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> cacheSet<span style="color: #009900;">&#40;</span><span style="color: #000033;">$key</span><span style="color: #339933;">,</span> <span style="color: #000033;">$value</span><span style="color: #339933;">,</span> <span style="color: #000033;">$timeout</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">86400</span><span style="color: #339933;">,</span> <span style="color: #000033;">$compress</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000033;">$cache</span> <span style="color: #339933;">=</span> self<span style="color: #339933;">::</span><span style="color: #004000;">getCache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$timeout</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$timeout</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/min"><span style="color: #990000;">min</span></a><span style="color: #009900;">&#40;</span><span style="color: #000033;">$timeout</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2592000</span><span style="color: #009900;">&#41;</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: #000033;">$compress</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$key</span><span style="color: #339933;">,</span> <span style="color: #000033;">$value</span><span style="color: #339933;">,</span> MEMCACHE_COMPRESSED<span style="color: #339933;">,</span> <span style="color: #000033;">$timeout</span><span style="color: #009900;">&#41;</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: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$key</span><span style="color: #339933;">,</span> <span style="color: #000033;">$value</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000033;">$timeout</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/**
     * get an item from the cache
     *
     * @param string key
     * @return mixed value or false
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> cacheGet<span style="color: #009900;">&#40;</span><span style="color: #000033;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000033;">$cache</span> <span style="color: #339933;">=</span> self<span style="color: #339933;">::</span><span style="color: #004000;">getCache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000033;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>As you can see the code is very simple. The <strong>cacheGet</strong> function is trivial, so no discussion is needed. The <strong>cacheSet</strong> function is a little more complex, but basically that is to support a very simple calling convention and to ensure that the parameters are sane. One obviously missing check is to ensure that the Memcache variable returned by <strong>getCache</strong> is valid &#8212; I guess I have some work to do.</p><p>The advantage for using caching in this manner should be obvious. Instead of requiring an actual HTTP request per pageview, this method keeps a copy around for 20 minutes before initiating another request to the <a
href="http://backtweets.com/">backtweets.com</a> servers. This is friendly to the remote servers while also increasing the responsiveness of my application.</p><p>This logic applies to any big, slow or heavy processes that might occur on a web server, not just remote HTTP requests. If your application generates lots of mostly-static HTML markup that is re-used or requested frequently, it could be cached in memcache. If you have any big or frequently-requested database queries, throw the results in memcache and take the load off of your DB server.</p><p>AND&#8230;</p><p>Since memcache is based on TCP/IP, it doesn&#8217;t have to run locally relative to your application server. Have one big memcache box, or two, or three, and make requests from all of your application servers.</p><p>If your application is falling over because of load, you might want to take a look at the problem spots. You&#8217;ll probably find that using memcache will reduce overall load on your database and application servers and increase the responsiveness of your site.</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/memcache-memcache-memcache/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Amazon web services change for associates</title><link>http://www.thewhyandthehow.com/amazon-web-services-change-for-associates/</link> <comments>http://www.thewhyandthehow.com/amazon-web-services-change-for-associates/#comments</comments> <pubDate>Sat, 09 May 2009 00:36:00 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[REST]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[aws]]></category> <category><![CDATA[tech]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=677</guid> <description><![CDATA[Although I haven&#8217;t written about it yet, for me the most important set of web services and APIs for my work are provided by Amazon. Without a doubt I use these services more than any others. Today I received this message:Effective immediately, we are renaming the Amazon Associates Web Service as the “Product Advertising API.” [...]]]></description> <content:encoded><![CDATA[<p>Although I haven&#8217;t written about it yet, for me the most important set of web services and APIs for my work are provided by Amazon. Without a doubt I use these services more than any others. Today I received this message:</p><div
class="quote"><p>Effective immediately, we are renaming the Amazon Associates Web Service as the “Product Advertising API.” This new name more accurately reflects the purpose of the API, which is to enable developers to advertise products offered on the Amazon sites and thereby receive advertising fees from us.</p><p
class="para-break">In addition to the new name, signatures will be necessary to authenticate each call to the Product Advertising API. This requirement will be phased in starting May 11, 2009, and by August 15, 2009, all calls to the Product Advertising API must be authenticated or they will not be processed.</p></div><p>Because this will have a big impact on any developer, I wanted to share a couple of links I found today. First is the documentation page: <a
href="http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/rest-signature.html">Signing AWS Commerce API requests</a>. The second is a recommendation from <a
href="http://twitter.com/giltotherescue">Gil</a> (the genius that keeps <a
href="http://www.squidoo.com/">Squidoo</a> running): <a
href="http://tarzan-aws.com/">Tarzan: A fast, powerful PHP toolkit for building web applications with Amazon Web Services</a>. This library looks like a great resource for developing against many of the AWS services. I&#8217;ll be doing some investigation and will follow up when I can talk more intelligently about it.</p><p>For now, good luck with your current AWS projects!</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/amazon-web-services-change-for-associates/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss><!--
This site's performance optimized by W3 Total Cache:

W3 Total Cache improves the user experience of your blog by caching
frequent operations, reducing the weight of various files and providing
transparent content delivery network integration.

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using memcached
Page Caching using memcached
Database Caching 8/15 queries in 0.007 seconds using memcached

Served from: a2.c0.354a.static.theplanet.com @ 2010-09-10 04:06:32 -->