{"id":5743,"date":"2017-11-25T04:17:56","date_gmt":"2017-11-25T04:17:56","guid":{"rendered":"http:\/\/ben.akrin.com\/?p=5743"},"modified":"2017-11-25T04:17:56","modified_gmt":"2017-11-25T04:17:56","slug":"a-universal-caching-algorithm-for-php-using-memcached","status":"publish","type":"post","link":"https:\/\/ben.akrin.com\/?p=5743","title":{"rendered":"A Universal Caching Algorithm for PHP using Memcached"},"content":{"rendered":"<p>Here is an elegant way to use the same caching logic for all function calls which should have a cache. With the proliferation of 3rd party APIs I was quite happy to find a way to address them all with a single mechanism.<\/p>\n<p>[php]function expensive_third_party_call( $param1, $param2 ) {<br \/>\n    \/\/ universal caching algorithm header<br \/>\n    $result = memcached_retrieve( __FUNCTION__ . serialize(func_get_args()) ) ;<br \/>\n    if( $result!==null ) {<br \/>\n        return $result ;<br \/>\n    }<\/p>\n<p>    \/\/ this is where the third party call actually happens, if we are hit the cache missed<br \/>\n    $to_return = \/* some super complex and time consuming logic, throw in a couple of web calls*\/ ;<\/p>\n<p>    \/\/ universal caching algorithm footer<br \/>\n    memcached_store( __FUNCTION__ . serialize(func_get_args()), $to_return, CACHE_TIMEOUT ) ;<br \/>\n    return $to_return ;<br \/>\n}<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/ helper functions bellow \/\/\/\/\/\/\/\/\/\/<br \/>\n$m = false ;<\/p>\n<p>function memcached_retrieve( $key ) {<br \/>\n    global $m ;<\/p>\n<p>    $new_key = md5( $key ) ;<br \/>\n    if( $m===false ) {<br \/>\n        $m = new Memcached() ;<br \/>\n        $m-&gt;addServer( &#8216;localhost&#8217;, 11211 ) ;<br \/>\n    }<br \/>\n    $temp = $m-&gt;get( $new_key ) ;<br \/>\n    $result_code = $m-&gt;getResultCode() ;<br \/>\n    if( $result_code==Memcached::RES_SUCCESS ) {<br \/>\n        return $temp ;<br \/>\n    } else if( $result_code==Memcached::RES_NOTFOUND ) {<br \/>\n        return null ;<br \/>\n    } else {<br \/>\n        echo &quot;error: can&#8217;t retrieve memcached key {$key} with result_code {$result_code}&quot; ;<br \/>\n    }<\/p>\n<p>    return null ;<br \/>\n}<\/p>\n<p>function memcached_store( $key, $data, $timeout ) {<br \/>\n    global $m ;<\/p>\n<p>    $new_key = md5( $key ) ;<\/p>\n<p>    if( $m===false ) {<br \/>\n        $m = new Memcached() ;<br \/>\n        $m-&gt;addServer( &#8216;localhost&#8217;, 11211 ) ;<br \/>\n    }<\/p>\n<p>    \/\/ a little heavy handed but we use null to represent that nothing was found in the cache so we can&#8217;t have this be the data<br \/>\n    if( $data===null ) {<br \/>\n        $data = false ;<br \/>\n    }<\/p>\n<p>    $m-&gt;set( $new_key, $data, $timeout ) ;<br \/>\n    $result_code = $m-&gt;getResultCode() ;<br \/>\n    if( $result_code!==Memcached::RES_SUCCESS ) {<br \/>\n        echo &quot;error: can&#8217;t store memcached key {$key} with result_code {$result_code}&quot; ;<br \/>\n        return false ;<br \/>\n    } else {<br \/>\n        return true ;<br \/>\n    }<\/p>\n<p>    return false ;<br \/>\n}<br \/>\n[\/php]<\/p>\n<h1>Requirements<\/h1>\n<ul>\n<li>apt-get install memcached php-memcached<\/li>\n<li>make sure to define CACHE_TIMEOUT<\/li>\n<\/ul>\n<h1>Functioning principle<\/h1>\n<p>Using PHP&#8217;s awareness of the current function it is in along with the parameters which are passed to it, we derive a unique key which is used so store and retrieve from the cache.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an elegant way to use the same caching logic for all function calls which should have a cache. With the proliferation of 3rd party APIs I was quite happy to find a way to address them all with a single mechanism. [php]function expensive_third_party_call( $param1, $param2 ) { \/\/ universal caching algorithm header $result &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ben.akrin.com\/?p=5743\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Universal Caching Algorithm for PHP using Memcached&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21],"tags":[70,191,290],"class_list":["post-5743","post","type-post","status-publish","format-standard","hentry","category-i-t","category-web-development","tag-cache","tag-php","tag-universal"],"_links":{"self":[{"href":"https:\/\/ben.akrin.com\/index.php?rest_route=\/wp\/v2\/posts\/5743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ben.akrin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ben.akrin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ben.akrin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ben.akrin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5743"}],"version-history":[{"count":0,"href":"https:\/\/ben.akrin.com\/index.php?rest_route=\/wp\/v2\/posts\/5743\/revisions"}],"wp:attachment":[{"href":"https:\/\/ben.akrin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ben.akrin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ben.akrin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}