<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Engineer by day. Automotive junkie by night.</description><title>Engineering Concepts</title><generator>Tumblr (3.0; @alekseykorzun)</generator><link>http://alekseykorzun.com/</link><item><title>How to gracefully handle cache expiration</title><description>&lt;p&gt;I received few inquires for a technical run down of how &lt;a href="http://github.com/AlekseyKorzun/memcached-wrapper-php"&gt;Memcached wrapper&lt;/a&gt; handles expiration of keys. &lt;/p&gt;

&lt;p&gt;When I first read about this concept, it was pretty hard to understand since most of the sources were way too technical (the fact that English is my second language &lt;em&gt;probably &lt;/em&gt;did not help either) for somebody who just entered word of &amp;#8216;holy crap! you can store stuff in memory&amp;#8217;.&lt;br/&gt;&lt;br/&gt;
I will try my best to explain the problem with &lt;em&gt;just&lt;/em&gt; caching and one of the ways you can avoid it without using too much of technical jargon.&lt;/p&gt;

&lt;h1&gt;Concept of Caching&lt;/h1&gt;

&lt;p&gt;Caching, to a less experienced developer is viewed as a tool that solves all of the performance problems. When Memcache was first introduced , developers would simply wrap a chunk of code in a block that does the following:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Look up a key from cache&lt;/li&gt;
	&lt;li&gt;If key does not exist in cache, call a random method to generate data that needs to be associated with that key &lt;/li&gt;
	&lt;li&gt;Store that data under the key in cache&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;And when same code block was executed again it would find the data associated with that key within the cache pool and give you the data&amp;#8230; &lt;em&gt;extremely&lt;/em&gt; fast. &lt;/p&gt;

&lt;p&gt;To a developer that&amp;#8217;s a wow factor by it self, I remember how impressed I was when I implemented something like this for the first time.&lt;br/&gt;&lt;br/&gt;
Such reaction is enough to simply put your tools down and call it a job well done. You can go home and celebrate your achievement and dream about handling thousands of users at any given time because this caching thing is awesome.&lt;/p&gt;

&lt;h1&gt;Rain on Your Parade&lt;/h1&gt;

&lt;p&gt;While you are still thinking that you solved all of the scalability problems you will ever have, your website get&amp;#8217;s a tsunami of new visitors from a TV PR campaign your marketing team launched. &lt;br/&gt;&lt;br/&gt;
Everything looks great, you are smirking at how well the cache is working.&lt;br/&gt;&lt;br/&gt;&lt;em&gt;Then the site goes down.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You scrambles to find a solution, &lt;em&gt;it can&amp;#8217;t be&lt;/em&gt; the caching you just implemented. It&amp;#8217;s just too fast to fail.&lt;br/&gt;&lt;br/&gt;
Usually (from what I seen) people will point fingers at the database or whatever complex/slow pieces of code you were hiding behind a cache. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;(And while the database might be the slowest part of your application, that&amp;#8217;s not the reason why your web site went down.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Since cache is &lt;em&gt;sooo fast&lt;/em&gt;, and your logs are telling you that your database simply died as soon as it started to see a little bit of requests you naturally assume that&amp;#8217;s the main issue and it should be addressed.&lt;br/&gt;&lt;br/&gt;
So in a moment of panic you scream for more database slaves, a better tune of your cluster and perhaps a crazy last second sharding implementation.&lt;br/&gt;&lt;br/&gt;
That works out great, you are back in business and things are looking up.&lt;/p&gt;

&lt;h1&gt;Paradise in a Desert&lt;/h1&gt;

&lt;p&gt;Just like a paradise you might discover after walking under a burning sun in a desert, the solution you put in place to prevent another downtime due to a massive amount of traffic was simply an illusion.&lt;/p&gt;

&lt;p&gt;The web site will still go down under the same conditions (unless of-course you invested in a small data center that hosts dozens of database clusters, then it&amp;#8217;s debatable). &lt;/p&gt;

&lt;p&gt;The reason for such mistakes is overlooking the fact that once you cache something that takes more than x seconds to execute, it will eventually expire from cache.&lt;br/&gt;&lt;br/&gt;
Either from having a short time to live (TTL) or by being pushed out the caching pool to make space for fresher data.&lt;/p&gt;

&lt;h1&gt;Breaking it Down&lt;/h1&gt;

&lt;p&gt;Let&amp;#8217;s say you have 35 queries on your web site that you put behind a caching layer. You request the page and it flies. Absolutely no issues. &lt;/p&gt;

&lt;p&gt;Even when cache expires and you grab the page it loads pretty damn fast, you can add 10 more concurrent visitors and there will be no problems. The database picks it all up easily and puts in back in the cache.&lt;/p&gt;

&lt;p&gt;Now, if you multiply those 10 concurrent requests by let&amp;#8217;s say 20. If all of the queries are cached, cache pool can process 200 requests without any issues.&lt;br/&gt;&lt;br/&gt;
But once the data in the cache expires all of a sudden all 200 requests to cache pool return &amp;#8216;resource not found&amp;#8217; and sends all of those requests directly to the database at once.&lt;/p&gt;

&lt;p&gt;And then the alarms go off.&lt;br/&gt;&lt;br/&gt;
The chain reaction is usually something along these lines:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Database becomes overloaded and clients stall waiting for data to be returned back from the database.&lt;/li&gt;
	&lt;li&gt;As the clients wait for a possibly dead database, http server keeps those clients in it&amp;#8217;s pool while attempting to serve new clients using resources it has left.&lt;/li&gt;
	&lt;li&gt;The http server has ran out of resources since it can&amp;#8217;t process clients as fast as it usually does because everything is being used up by requests that are waiting for database.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;That sucks, right? If only there was a way to prevent this from happening.&lt;/p&gt;

&lt;h1&gt;Caching Just Got Smarter&lt;/h1&gt;

&lt;p&gt;One of the approaches to this problem is fairy simple. The concept is to wrap the original resource you are caching in an array that contains a time stamp that is set to a time that is just a few minutes short of the time when the item is actually set to expire.&lt;br/&gt;&lt;br/&gt;
And when your application unpacks cached resource it will check that time stamp and if current time greater it means that item it just retrieved is going to expire relatively soon. &lt;br/&gt;&lt;br/&gt;
Once it knows that item is going to expire, it will update the cached record with a new one that contains the exacly the same data it just recieved but with a longer expiration time.&lt;br/&gt;&lt;br/&gt;
Essentially telling anybody else who is pulling the data that the item is &lt;strong&gt;&lt;em&gt;not &lt;/em&gt;&lt;/strong&gt;going to expire any time soon.&lt;/p&gt;

&lt;p&gt;Then you simply lie to your application and tell them that this request did not get anything back from cache. It will now execute a database query you were caching and save it back into the database with a new expiration date.&lt;br/&gt;&lt;br/&gt;
All of the new requests will now have an updated version of cached data.&lt;/p&gt;

&lt;p&gt;While it&amp;#8217;s possible that more than one request will slip through the flood gates, this is usually really rare. When I tested with 200 concurrent connections the key was updated by a single request. &lt;br/&gt;&lt;br/&gt;
Since you are caching everything, your database and apache should be pumping out requests fairly fast so you can probably afford more than a single person slipping through this barrier one in a while without creating an unrecoverable request queue of death.&lt;/p&gt;

&lt;h1&gt;Walk the Talk&lt;/h1&gt;

&lt;p&gt;Let&amp;#8217;s get a little bit more technical and try to implement this solution in our code. First we need to come up with a time interval that we will subtract from the original expiration date. &lt;/p&gt;

&lt;p&gt;This time interval depends on your caching strategy, basic rule of thumb take a median expiration intervals for the most important keys in cache that represent data from really slow database queries.&lt;br/&gt;&lt;br/&gt;
If that number is let&amp;#8217;s say an hour and you can guarantee that on average, during a relatively busy day you get more than one request every 10 minutes, it&amp;#8217;s safe to set time interval to 10 minutes.&lt;br/&gt;&lt;br/&gt;
If you have cached data that needs to be updated at a faster rate, always make sure that you are guaranteed that there will be a single request between original expiration time and the fake expiration time (ie: your safety net).&lt;/p&gt;

&lt;p&gt;So since we now have a number in mind, let&amp;#8217;s write a simple wrapper for Memcached extension so we can override &lt;strong&gt;&lt;em&gt;set()&lt;/em&gt;&lt;/strong&gt; method and wrap a resource we are caching in an array containing our &amp;#8216;fake&amp;#8217; expiration date:&lt;/p&gt;

&lt;pre class="prettyprint lang-php"&gt;
&lt;code class="language-php"&gt;
use \Memcached;

class Wrapper
{
    /**
     * Time to substract from original expiration date
     *
     * @var int
     */

    const DELAY = 600;

    /**
     * Instance of Memcached
     *
     * @var Memcached
     */
    protected $memcached;

    /**
     * Indicates that current look-up will expire shortly (dog-pile)
     *
     * @var bool
     */
    protected $isResourceExpired = false;

    /**
     * Class constructor
     */
    public function __construct()
    {
        $this-&amp;gt;memcached = new Memcached();
    }

    /**
     * Add a new cached record using passed resource and key association
     *
     * @param string $key key to store passed resource under
     * @param mixed $resource resource you want to cache
     * @param int $ttl when should this key expire in seconds
     * @return bool
     */
    public function set($key, $resource, $ttl)
    {
        return (bool)$this-&amp;gt;memcached-&amp;gt;set($key, $this-&amp;gt;wrap($resource, $ttl), $ttl);
    }

    /**
     * Wrap new cached resource into an array containing TTL stamp
     *
     * @param mixed $resource resource that is getting cached
     * @param int $ttl internal extended expiration
     * @return mixed[] returns packed resource with TTL stamp to store in cache
     */
    protected function wrap($resource, $ttl)
    {
        // Set meta expiration date 10 minutes before the actual date
        $ttl -= self::DELAY;

        return array(
            'ttl' =&amp;gt; $ttl,
            'resource' =&amp;gt; $resource
        );
    }
}
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;As you can see, we simply intercept &lt;strong&gt;&lt;em&gt;set()&lt;/em&gt;&lt;/strong&gt; method on original extension so we can call wrap() method on a resource we are caching. In return that method will take original expiration time we are attempting to set and subtract 10 minutes from it prior to adding it to our array.&lt;br/&gt;&lt;br/&gt;
Now, we need to intercept a &lt;strong&gt;get()&lt;/strong&gt; method so we can unwrap the previously wrapped data and check the fake expiration date we set in order to determine if we should pretend the result is no longer cached.&lt;/p&gt;

&lt;p&gt;To do so let&amp;#8217;s add following methods to our wrapper:&lt;/p&gt;

&lt;pre class="prettyprint lang-php"&gt;
&lt;code class="language-php"&gt;
/**
 * Override get method so we can wrap resource that is being cached
 * in an array containing additional metadata
 *
 * @param $string $key
 * @param mixed $resource where to store retrieved resource
 * @return bool
 */
protected function get($key, &amp;amp;$resource)
{
    // Attempt to retrieve record within cache pool
    $response = $this-&amp;gt;memcached-&amp;gt;get($key);
    if ($this-&amp;gt;memcached-&amp;gt;getResultCode() == Memcached::RES_SUCCESS) {
        // Pass record to unwrap method
        $resource = $this-&amp;gt;unwrap($key, $response);

        // If key is marked as expired (needs to be updated within this request)
        // we will not return true, but instead fake a failure
        if (!$this-&amp;gt;isResourceExpired) {
            return true;
        }
    }

    return false;
}

/**
 * Get requested data back into memory while setting a delayed cache entry
 * if data is expiring soon
 *
 * @param string $key key that you are retrieving
 * @param mixed[] packed data that we got back from cache pool
 * @return mixed|bool returns cached resource or false if invalid data was
 * passed for unwrapping
 */
protected function unwrap($key, array $data)
{
    // If expiration date is not set to never
    if ($data['ttl'] &amp;gt; 0) {
        // If current time is equal or greater than a fake expiration time
        if (time() &amp;gt;= $data['ttl']) {
            // Set the stale value back into cache for a short 'delay' of 10 minutes
            // so no one else tries to write the same data.
            //
            // Note how we are calling our set method that utilizes wrap()
            if ($this-&amp;gt;set($key, $this-&amp;gt;wrap($data['resource'], self::DELAY), self::DELAY)) {
                // Set flag that tells
                $this-&amp;gt;isResourceExpired = true;
            }
        }
    }

    return $this-&amp;gt;store($key, $data['resource']);
}
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;You now have a pretty solid protection that stops random flood of requests that bypass your caching layer at the same time.&lt;/p&gt;

&lt;p&gt;As part of my Memcached wrapper, I included a &lt;a href="https://github.com/AlekseyKorzun/memcached-wrapper-php/blob/master/examples/performance.php"&gt;simple proof of concept script&lt;/a&gt; that you can use to test this scenario your self.&lt;/p&gt;

&lt;p&gt;When I bench marked the script in question with 200 concurrent requests the results did all the talking:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Using technique we implemented:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="prettyprint lang-sh"&gt;
&lt;code class="language-sh"&gt;
[30-Apr-2013 01:02:52 UTC] Wrapper database hit: 2013-04-30 03:02:52
[30-Apr-2013 01:07:52 UTC] Wrapper database hit: 2013-04-30 03:07:52
[30-Apr-2013 01:12:51 UTC] Wrapper database hit: 2013-04-30 03:12:51
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
As you can see only a single request out of 200 got through to query the database and update cache.
&lt;/p&gt;

&lt;p&gt;
&lt;strong&gt;&lt;em&gt;Using raw get/set methods:&lt;/em&gt;&lt;/strong&gt;
&lt;/p&gt;

&lt;pre class="prettyprint lang-sh"&gt;
&lt;code class="language-sh"&gt;
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:09 UTC] Memcached database hit: 2013-04-30 03:18:09
[30-Apr-2013 01:18:10 UTC] Memcached database hit: 2013-04-30 03:18:10
[30-Apr-2013 01:18:10 UTC] Memcached database hit: 2013-04-30 03:18:10
[30-Apr-2013 01:18:10 UTC] Memcached database hit: 2013-04-30 03:18:10
[30-Apr-2013 01:18:10 UTC] Memcached database hit: 2013-04-30 03:18:10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
&lt;em&gt;&lt;strong&gt;&amp;#8230;. the Apache could no longer keep up with requests.&lt;/strong&gt;&lt;/em&gt;
&lt;/p&gt;

&lt;h1&gt;Fin.&lt;/h1&gt;</description><link>http://alekseykorzun.com/post/49520668105</link><guid>http://alekseykorzun.com/post/49520668105</guid><pubDate>Fri, 03 May 2013 12:55:46 -0400</pubDate><category>php</category><category>tutorial</category><category>memcache</category><category>memcached</category><category>optimization</category><category>scalibility</category><category>mysql</category><category>database</category></item><item><title>Store your application files in Memcached pool with MemFS</title><description>&lt;p&gt;
This package provides a way to store your application files in Memcached pool with automatic
parsing using eval() upon retrieval.
&lt;/p&gt;

&lt;p&gt;
I wrote this after trolling a friend of mine how storing files in Memcached is faster than including
them from disk, so while there are definite uses for this under right circumstances make sure you understand
what you are doing.
&lt;/p&gt;

&lt;h2&gt;Performance&lt;/h2&gt;

&lt;p&gt;
First let&amp;#8217;s look at benchmarks running in VirtualBox on MacBook Pro:
&lt;/p&gt;

&lt;pre class="prettyprint lang-bash"&gt;
Loaded 500 files using regular include() in 0.020308017730713
Loaded 500 files using MemFS in 0.15591597557068
Loaded 500 files using MemFS (10 files at the time) 0.018874883651733
Loaded 500 files using MemFS (250 files at the time) 0.015585899353027
Loaded 500 files using MemFS (500 files at the time) 0.016402959823608
&lt;/pre&gt;

&lt;p&gt;
Keep in mind that the laptop in question has SSD drive. 
&lt;/p&gt;

&lt;p&gt;
As you can see, when compared directly one for one you might get performance degration but as you 
scale up and request 10+ files you will see a slight speed increase of about 0.00143313407 seconds.
&lt;/p&gt;

&lt;p&gt;
Not much but this benchmark is not scientific and was performed on a empty machine with SSD drive with no I/O 
load. Your setup might be different and if you take 0.0014 * requests per seconds your application is processing you
might see a benefit of saving 14 seconds per 10000 requests.
&lt;/p&gt;

&lt;h2&gt;Use cases&lt;/h2&gt;

&lt;p&gt;
- Servers that host PHP based applications in a saturated or slow I/O environment.
- Distributed applications that constantly update their logic, can be updated by updating 
memory pool with new code that they will include and evaluate.
- Applications that need to load and parse more than 1 file in a consecutive order.
&lt;/p&gt;

&lt;h2&gt;Memcached&lt;/h2&gt;

&lt;p&gt;
For optimal results, make sure server hosting cache pool is hosted on a separate server but shares the LAN 
with your web servers. 
&lt;/p&gt;

&lt;p&gt;
Depending on your load the link between the servers must be greater than 100Mbps.
&lt;/p&gt;

&lt;p&gt;     
Make sure to use igBinary with latest version of daemon.
&lt;/p&gt;

&lt;h2&gt;Installation&lt;/h2&gt;

&lt;p&gt;
If you have your own autoloader, simply update namespaces and drop the files
into your frameworks library.
&lt;/p&gt;

&lt;p&gt;
For people that do not have that setup, you can visit &lt;a href="http://getcomposer.org"&gt;http://getcomposer.org&lt;/a&gt; to install
composer on your system. After installation simply run `composer install` in parent
directory of this distribution to generate vendor/ directory with a cross system autoloader.
&lt;/p&gt;

&lt;h2&gt;Benchmark&lt;/h2&gt;

&lt;p&gt;
	You can benchmark and gauge how much benefit this optimization might bring you by running:
&lt;/p&gt;

&lt;pre class="prettyprint lang-php"&gt;
php benchmark/run.php
&lt;/pre&gt;

&lt;p&gt;
make sure to run it twice before reading the results so the application can cache files
&lt;/p&gt;

&lt;h2&gt;Grab It&lt;/h2&gt;

&lt;p&gt;
You can grab the source code from my GitHub repository:
&lt;/p&gt;

&lt;a href="https://github.com/AlekseyKorzun/memfs-php"&gt;&lt;a href="https://github.com/AlekseyKorzun/memfs-php"&gt;https://github.com/AlekseyKorzun/memfs-php&lt;/a&gt;&lt;/a&gt;</description><link>http://alekseykorzun.com/post/47581405544</link><guid>http://alekseykorzun.com/post/47581405544</guid><pubDate>Tue, 09 Apr 2013 20:49:35 -0400</pubDate><category>php</category><category>php5</category><category>memcache</category><category>memcached</category><category>cache</category><category>storage</category><category>performance</category><category>scalibility</category><category>caching</category><category>optimization</category><category>profile</category><category>filesystem</category><category>memfs</category><category>loading</category><category>include</category><category>fast</category><category>programming</category><category>c10k</category><category>distributed</category><category>io</category><category>ssd</category></item><item><title>New version of Memcached Wrapper for PHP 5</title><description>&lt;p&gt;I updated my Memcached wrapper and moved away from a static approach to a more dynamic approach that supports multiple pools (as some of you requested).&lt;/p&gt;

&lt;p&gt;Also the wrapper will now pass any direct methods to the extension, so you get exactly the same functionality as you do using Memcached  with a benefit of a smart caching layer.&lt;/p&gt;

&lt;p&gt;If you still need a static version of the wrapper you can find it under 0.1 branch. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Change log:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;* Abandoned static approach for a cleaner implementation&lt;br/&gt;
* Added proper support for multiple pools&lt;br/&gt;
* Wrapper for easy interaction with Memcached instance&lt;br/&gt;
* Minor documentation and code fixes&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Grab your copy here:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://alekseykorzun.github.io/memcached-wrapper-php"&gt;&lt;a href="http://alekseykorzun.github.io/memcached-wrapper-php"&gt;http://alekseykorzun.github.io/memcached-wrapper-php&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://alekseykorzun.com/post/47239668872</link><guid>http://alekseykorzun.com/post/47239668872</guid><pubDate>Fri, 05 Apr 2013 21:55:00 -0400</pubDate></item><item><title>Retrieving default billing and shipping information in Magento</title><description>&lt;p&gt;
I recently got to play around with Magento (eCommerce platform based on Zend Framework) and had to figure out 
default billing and shipping information for all of our current clients.
&lt;/p&gt;
&lt;p&gt;
Apparently developer community build around Magento is pretty amateurish judging by technical discussions
that are found on their forums. 
&lt;/p&gt;
&lt;p&gt;
When I looked up how to retrieve default address information, the answers would all be in a pure PHP code
that utilized Magento models to retrieve data.
&lt;/p&gt;
&lt;p&gt;
Thats bloated and frankly plain dumb. Not to mention that some of us do not use PHP for system specific tasks. 
&lt;/p&gt;
&lt;p&gt;
In my case I&amp;#8217;m using Python so I needed a SQL query that I can run against our Magento database to retrieve 
required data.
&lt;/p&gt;
&lt;p&gt;
Magento stores data with dozens of relations (very confusing if you are just starting out) and some of relations do not make
much sense. They split out entity properties by type (datetime, int, text, etc) and associate each type separately to main 
entity via id. 
&lt;/p&gt;
&lt;p&gt;
Not to mention that address data is considered to be it&amp;#8217;s own entity and has it&amp;#8217;s indexing outside customers space.
&lt;/p&gt;
&lt;p&gt;
First we have to grab default billing and shipping ids associated to our user accounts:
&lt;/p&gt;&lt;pre class="prettyprint lang-sql"&gt;
SELECT 
    `ce`.`email`,
    `default_billing_id`.`value` AS `default_billing`,
    `default_shipping_id`.`value` AS `default_shipping`
FROM
    `customer_entity` AS `ce`
	LEFT JOIN `customer_entity_int` AS `default_billing_id` ON
		(`default_billing_id`.`entity_id` = `ce`.`entity_id`) AND
		(`default_billing_id`.`attribute_id` = '14')
	LEFT JOIN `customer_entity_int` AS `default_shipping_id` ON 
		(`default_shipping_id`.`entity_id` = `ce`.`entity_id`) AND
		(`default_shipping_id`.`attribute_id` = '13')
WHERE
    (`ce`.`entity_type_id` = 1) AND
    (`ce`.`is_active` = 1) 
&lt;/pre&gt;

&lt;p&gt;
As you can see attribute id of &amp;#8216;13&amp;#8217; represents default shipping identifier and attribute &amp;#8216;14&amp;#8217; represents default billing identifier.
&lt;/p&gt;
&lt;p&gt;
Now we need to join across address entity to retrieve actual address information linked to default addresses the user has, in this
example I will retrieve default zip code for both billing and shipping address:

&lt;/p&gt;&lt;pre class="prettyprint lang-sql"&gt;
SELECT 
    `ce`.`email`,
    `default_billing_id`.`value` AS `default_billing`,
    `default_shipping_id`.`value` AS `default_shipping`,
    `default_billing_zipcode`.`value` AS `default_billing_zipcode`,
    `default_shipping_zipcode`.`value` AS `default_shipping_zipcode`
FROM
    `customer_entity` AS `ce`
	LEFT JOIN `customer_entity_int` AS `default_billing_id` ON
		(`default_billing_id`.`entity_id` = `ce`.`entity_id`) AND
		(`default_billing_id`.`attribute_id` = '14')
	LEFT JOIN `customer_entity_int` AS `default_shipping_id` ON 
		(`default_shipping_id`.`entity_id` = `ce`.`entity_id`) AND
		(`default_shipping_id`.`attribute_id` = '13')
	LEFT JOIN `customer_address_entity_varchar` AS `default_shipping_zipcode` ON 
		(`default_shipping_id`.`value` = `default_shipping_zipcode`.`entity_id`) AND
		(`default_shipping_zipcode`.`attribute_id` = '29')
	LEFT JOIN `customer_address_entity_varchar` AS `default_billing_zipcode` ON 
		(`default_billing_id`.`value` = `default_billing_zipcode`.`entity_id`) AND
		(`default_billing_zipcode`.`attribute_id` = '29')
WHERE
    (`ce`.`entity_type_id` = 1) AND
    (`ce`.`is_active` = 1)
&lt;/pre&gt;
&lt;p&gt;
Note that attribute_id&amp;#8217;s might vary in your installation. You might consider modifying this query to use attribute labels for a more
portable approach.
&lt;/p&gt;

&lt;p&gt;
Feel free to reach out to me if you have any questions. 
&lt;/p&gt;</description><link>http://alekseykorzun.com/post/44225959120</link><guid>http://alekseykorzun.com/post/44225959120</guid><pubDate>Thu, 28 Feb 2013 12:31:00 -0500</pubDate><category>php</category><category>php5</category><category>magento</category><category>ecommerce</category><category>mysql</category><category>sql</category></item><item><title>Simple framework wrapper for Amazon's AWS SDK for PHP 5</title><description>&lt;p&gt;If you interact with any of Amazon&amp;#8217;s Web Services using PHP you should be using their &lt;a href="http://aws.amazon.com/sdkforphp/"&gt;SDK&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;It&amp;#8217;s very powerful and pretty simple to get started with.&lt;/p&gt;

&lt;p&gt;In my case I did not want to checkout their whole repository into my frameworks namespace and decided to use .phar file they provided as&lt;br/&gt;
an alternative. &lt;/p&gt;

&lt;p&gt;To me, using a .phar file for third party libraries kind of makes sense if I&amp;#8217;m not planning to extend or overwrite any of the available functionality.&lt;/p&gt;

&lt;p&gt;Plus if you have your own directory conventions and don&amp;#8217;t want to bother re-factoring Amazon&amp;#8217;s structure, .phar might come in handy.&lt;/p&gt;

&lt;p&gt;The issue I ran into (not really an issue but inconsistency always bothers me as an engineer) is that if you are using namespaces and your own autoloader, requiring .phar file anywhere you need to access AWS is pretty ghetto.&lt;/p&gt;

&lt;p&gt;The way I solved it is by using a very simple wrapper that either loads configuration (using \library\configuration as an example) containing your key/secret or takes key/secret as a constructor parameter and simply stores instance of AWS internally while routing all the calls directly to it.&lt;/p&gt;

&lt;p&gt;You can check out this approach on my GitHub page located here: &lt;a href="https://github.com/AlekseyKorzun/aws-sdk-wrapper"&gt;&lt;a href="https://github.com/AlekseyKorzun/aws-sdk-wrapper"&gt;https://github.com/AlekseyKorzun/aws-sdk-wrapper&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://alekseykorzun.com/post/44203912088</link><guid>http://alekseykorzun.com/post/44203912088</guid><pubDate>Thu, 28 Feb 2013 01:21:00 -0500</pubDate><category>php</category><category>php5</category><category>amazon</category><category>aws</category><category>sdk</category><category>wrapper</category><category>frameworks</category></item><item><title>Is this real life?</title><description>&lt;img src="http://25.media.tumblr.com/c69efd1663be990aa2f44b804ba12911/tumblr_mi44yvqnbu1qjg7ico1_500.jpg"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;Is this real life?&lt;/p&gt;</description><link>http://alekseykorzun.com/post/42926471071</link><guid>http://alekseykorzun.com/post/42926471071</guid><pubDate>Tue, 12 Feb 2013 10:13:43 -0500</pubDate></item><item><title>Found birthday card I received from my friends at @lot18. Haha...</title><description>&lt;img src="http://25.media.tumblr.com/956e3dd941f259211702e82ab42cec02/tumblr_mi32lixhSR1qjg7ico1_500.jpg"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;Found birthday card I received from my friends at @lot18. Haha (at Lot18 HQ)&lt;/p&gt;</description><link>http://alekseykorzun.com/post/42886295812</link><guid>http://alekseykorzun.com/post/42886295812</guid><pubDate>Mon, 11 Feb 2013 20:24:54 -0500</pubDate></item><item><title>Thai bag work (by Aleksey Korzun)</title><description>&lt;iframe width="400" height="225" src="http://www.youtube.com/embed/ORvwrDAes-k?wmode=transparent&amp;autohide=1&amp;egm=0&amp;hd=1&amp;iv_load_policy=3&amp;modestbranding=1&amp;rel=0&amp;showinfo=0&amp;showsearch=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;Thai bag work (by &lt;a href="http://www.youtube.com/watch?v=ORvwrDAes-k&amp;feature=share"&gt;Aleksey Korzun&lt;/a&gt;)&lt;/p&gt;</description><link>http://alekseykorzun.com/post/39966981497</link><guid>http://alekseykorzun.com/post/39966981497</guid><pubDate>Mon, 07 Jan 2013 19:09:04 -0500</pubDate></item><item><title>Updated PHP 5 client for Cloud Servers(tm) by Rackspace.</title><description>&lt;p&gt;Just a quick announcement that I updated my PHP 5 client for Cloud Servers(tm) API.&lt;/p&gt;
&lt;p&gt;Some of the major fixes include retry limiting for authentication requests, support for updated response codes (especially for server/image requests) and better examples.&lt;/p&gt;
&lt;p&gt;Code was also updated to conform to PSR-2 and phpDocumentator 2.&lt;/p&gt;
&lt;p&gt;Grab the latest version here: &lt;a href="http://alekseykorzun.github.io/rackspace-open-cloud-php/"&gt;&lt;a href="http://alekseykorzun.github.io/rackspace-open-cloud-php/"&gt;http://alekseykorzun.github.io/rackspace-open-cloud-php/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://alekseykorzun.com/post/29711632224</link><guid>http://alekseykorzun.com/post/29711632224</guid><pubDate>Sat, 18 Aug 2012 17:10:00 -0400</pubDate><category>php</category><category>php5</category><category>api</category><category>rest</category><category>cloud servers</category><category>rackspace</category><category>phpdocumentator</category><category>cloud</category><category>github</category></item><item><title>A re-factored plug-n-play PHP 5 client for Twilio's REST API</title><description>&lt;p&gt;I have pretty high standards when it comes to code and felt like the current PHP 5 library that Twilio offered did not really fit well with modern frameworks.&lt;/p&gt;
&lt;p&gt;So I decided to rewrite it, if you have your own framework and looking for a cleaner Twilio client make sure give this a try.&lt;/p&gt;
&lt;p&gt;Features&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Direct plug-in-play integration with PHP 5 based frameworks&lt;/li&gt;
&lt;li&gt;Namespace utilization, ability to piggy back off your own autoloader&lt;/li&gt;
&lt;li&gt;Completely refactored to remove clutter and omit ridiculous constructor inheritance the original package was build with&lt;/li&gt;
&lt;li&gt;Various optimizations, no longer pre-loads all of the available actions when an idle instance is created&lt;/li&gt;
&lt;li&gt;Cleaner file structure&lt;/li&gt;
&lt;li&gt;Single coding standard across all of the components&lt;/li&gt;
&lt;li&gt;100% phpDocumentator 2 code coverage&lt;/li&gt;
&lt;li&gt;100% PSR2 standard coverage&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Find out more at: &lt;a href="http://alekseykorzun.github.com/Twilio-PHP-5/" title="http://alekseykorzun.github.com/Twilio-PHP-5/" target="_blank"&gt;&lt;a href="http://alekseykorzun.github.com/Twilio-PHP-5/"&gt;http://alekseykorzun.github.com/Twilio-PHP-5/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://alekseykorzun.com/post/29654337257</link><guid>http://alekseykorzun.com/post/29654337257</guid><pubDate>Fri, 17 Aug 2012 20:14:23 -0400</pubDate><category>twilio</category><category>php</category><category>php5</category><category>library</category><category>client</category><category>rest</category><category>api</category><category>psr2</category><category>phpdocumentator</category><category>frameworks</category><category>progress</category></item><item><title>Sorry for not updating my blog, I have been busy doing grown up...</title><description>&lt;img src="http://25.media.tumblr.com/tumblr_lwxbbfbTPs1qjg7ico1_500.jpg"/&gt;&lt;br/&gt; &lt;br/&gt;&lt;img src="http://25.media.tumblr.com/tumblr_lwxbbfbTPs1qjg7ico2_500.jpg"/&gt;&lt;br/&gt; &lt;br/&gt;&lt;img src="http://25.media.tumblr.com/tumblr_lwxbbfbTPs1qjg7ico3_500.jpg"/&gt;&lt;br/&gt; &lt;br/&gt;&lt;p&gt;Sorry for not updating my blog, I have been busy doing grown up stuff. Wanted to share some of pictures I recently took of my car with you guys.&lt;/p&gt;
&lt;p&gt;Friend and I swapped in 3.15 LSD last month (love it) and minus a leaking power steering line that I will be fixing in a bit the car is flawless mechanically.&lt;/p&gt;
&lt;p&gt;The final piece to the puzzle? ESS S/C and a wider stance. Happy holidays!&lt;/p&gt;</description><link>http://alekseykorzun.com/post/14922802156</link><guid>http://alekseykorzun.com/post/14922802156</guid><pubDate>Wed, 28 Dec 2011 12:02:02 -0500</pubDate></item><item><title>Q&amp;A: Can you demonstrate how to perform binary search with PHP?</title><description>&lt;p&gt;I decided it&amp;#8217;s time to start posting (interesting) questions I receive from time to time; today&amp;#8217;s question will be about binary search in PHP.&lt;/p&gt;
&lt;p&gt;Since it seems that there are multiple of discussions on this subject within Java/C++ community mostly leaving PHP programmers in the dark.&lt;/p&gt;
&lt;p&gt;In layman&amp;#8217;s terms (using numbers as example) binary search takes an array of sorted numbers, finds the middle number and compares it to the number you want to find.&lt;/p&gt;
&lt;p&gt;If middle number is greater then number you passed, it will split array in two take all the numbers on the right of the middle number and use those numbers as a new starting point for splitting until match is found.&lt;/p&gt;
&lt;p&gt;Opposite is true if your number is smaller then the middle number within sorted array.&lt;/p&gt;
&lt;p&gt;This is pretty fast when dealing with larger data sets and very easy to implement in PHP:&lt;/p&gt;
&lt;p&gt;The above is a very basic method for binary searching, you can use it as your own starting point.&lt;/p&gt;
&lt;p&gt;By now, you might be thinking: Why should I use this .vs using array_search()?&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s a very valid concern, the problem with array_search is that it uses a basic &lt;a href="http://en.wikipedia.org/wiki/Big_O_notation" target="_blank"&gt;O(n)&lt;/a&gt; to find matches (&lt;a href="http://en.wikipedia.org/wiki/Linear_time#Linear_time" target="_self"&gt;&lt;a href="http://en.wikipedia.org/wiki/Linear_time#Linear_time"&gt;http://en.wikipedia.org/wiki/Linear_time#Linear_time&lt;/a&gt;&lt;/a&gt;). &lt;/p&gt;
&lt;p&gt;Which means:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;It will perform faster when searching very small data sets.&lt;/li&gt;
&lt;li&gt;It &lt;strong&gt;&lt;em&gt;might &lt;/em&gt;&lt;/strong&gt;perform faster if you are searching for a value in a begging of your data set.&lt;/li&gt;
&lt;li&gt;The larger your data set grows the slower it will be compared to &lt;a href="http://en.wikipedia.org/wiki/Binary_search_algorithm" target="_blank"&gt;binary search&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;As always, benchmarking is the key to figuring out which approach would fit you best when sorting data.&lt;/p&gt;</description><link>http://alekseykorzun.com/post/8236583113</link><guid>http://alekseykorzun.com/post/8236583113</guid><pubDate>Fri, 29 Jul 2011 20:41:00 -0400</pubDate><category>binary search</category><category>algorithm</category><category>Big O notation</category><category>PHP</category><category>performance</category><category>benchmarking</category><category>optimization</category><category>c10k</category><category>scalibility</category><category>data</category></item><item><title>Facebook: Jerry Cain Updated JavaScript SDK and OAuth 2.0 Roadmap</title><description>&lt;p&gt;Facebook released updated Roadmap SDK/OAuth 2.0, you can read more about it here: &lt;a href="http://developers.facebook.com/blog/post/525/"&gt;http://developers.facebook.com/blog/post/525/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I will keep a close eye on it, since the change-set will affect my php-sdk patch:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://alekseykorzun.com/post/6925234287/integrating-facebooks-social-plug-ins-with-facebook-php"&gt;http://alekseykorzun.com/post/6925234287/integrating-facebooks-social-plug-ins-with-facebook-php&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Highlights:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;July 22:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Updated JavaScript SDK available&lt;/li&gt;
&lt;li&gt;Migration setting changes&lt;/li&gt;
&lt;li&gt;Renamed &amp;#8220;OAuth 2.0 for Canvas&amp;#8221; migration to &amp;#8220;signed_request for Canvas&amp;#8221;&lt;/li&gt;
&lt;li&gt;Added new &amp;#8220;OAuth Migration&amp;#8221; setting&lt;/li&gt;
&lt;li&gt;Defaulted all new apps to have &amp;#8220;OAuth Migration&amp;#8221; setting enabled&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;strong&gt;July 29:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Updates to PHP SDK to be new-cookie-format aware.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;strong&gt;October 1:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;All apps to have &amp;#8220;OAuth Migration&amp;#8221; setting enabled (Revised from Sept. 1)&lt;/li&gt;
&lt;li&gt;All apps to have &amp;#8220;signed_request for Canvas&amp;#8221; setting enabled&lt;/li&gt;
&lt;li&gt;SSL Cert required for apps on Facebook (Canvas and Page Tab apps)&lt;/li&gt;
&lt;/ul&gt;</description><link>http://alekseykorzun.com/post/8008230457</link><guid>http://alekseykorzun.com/post/8008230457</guid><pubDate>Sun, 24 Jul 2011 14:20:50 -0400</pubDate><category>facebook</category><category>sdk</category><category>roadmap</category><category>php-sdk</category><category>oauth</category><category>update</category></item><item><title>We are pleased to announce a set of much anticipated...</title><description>&lt;img src="http://25.media.tumblr.com/tumblr_lor454KyUr1r0rxzao1_500.gif"/&gt;&lt;br/&gt; New simplified upload form&lt;br/&gt;&lt;br/&gt; &lt;img src="http://25.media.tumblr.com/tumblr_lor454KyUr1r0rxzao2_500.gif"/&gt;&lt;br/&gt; Ability to combine images into an album&lt;br/&gt;&lt;br/&gt; &lt;img src="http://24.media.tumblr.com/tumblr_lor454KyUr1r0rxzao3_500.gif"/&gt;&lt;br/&gt; Improved member section&lt;br/&gt;&lt;br/&gt; &lt;img src="http://25.media.tumblr.com/tumblr_lor454KyUr1r0rxzao4_500.gif"/&gt;&lt;br/&gt; Ability to modify images and albums&lt;br/&gt;&lt;br/&gt; &lt;img src="http://24.media.tumblr.com/tumblr_lor454KyUr1r0rxzao5_500.gif"/&gt;&lt;br/&gt; Introduction of albums&lt;br/&gt;&lt;br/&gt; &lt;p&gt;We are pleased to announce a set of much anticipated improvements.&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Upload form UX has been dramatically improved. Upon selection of image a new field will appear for the next selection. By default description/private options are hidden unless decide to upload something. Also when attempting to upload multiple images, the option to combine them into their own album will appear.&lt;/li&gt;
&lt;li&gt;A completely new member section, allows image management (edit/deletion), album management (create/edit/delete) and a better interface for your favorites.&lt;/li&gt;
&lt;li&gt;URL structure was modified to provide a better linking exprience.&lt;/li&gt;
&lt;li&gt;Numerous performance issues and bugs were also addressed.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;We welcome your feedback, please send us any bugs or new features you would us to implement.&lt;/p&gt;
&lt;p&gt;Like us on Facebook: &lt;a href="http://www.facebook.com/imagesocket" title="http://www.facebook.com/imagesocket"&gt;&lt;a href="http://www.facebook.com/imagesocket"&gt;http://www.facebook.com/imagesocket&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;</description><link>http://alekseykorzun.com/post/7944102263</link><guid>http://alekseykorzun.com/post/7944102263</guid><pubDate>Fri, 22 Jul 2011 18:21:00 -0400</pubDate></item><item><title>High scalability and MySQL's NOW() don't mix</title><description>&lt;p&gt;When developers optimize their applications, the usage of NOW() and other dynamic MySQL methods is often overlooked. &lt;/p&gt;
&lt;p&gt;The problem with using NOW() (and other identical methods) that the query will always be dynamic, which means that there will be no cache record within the database.&lt;/p&gt;
&lt;p&gt;So every single request that invokes dynamic time operations/etc queries will be treated as a completely fresh query.&lt;/p&gt;
&lt;p&gt;For example, let&amp;#8217;s compare two queries:&lt;/p&gt;
&lt;pre class="prettyprint lang-sh"&gt;SELECT count(1)
  FROM `schedule`
 WHERE `date` &amp;lt;= NOW()
&lt;/pre&gt;
&lt;p&gt;.vs&lt;/p&gt;
&lt;pre class="prettyprint lang-sql"&gt;SELECT count(1)
  FROM `schedule`
 WHERE `date` &amp;lt;= "2011-07-20 16:17:00"
&lt;/pre&gt;
&lt;p&gt;The first query will always be dynamic. So if the first request roughly took 0.03s, the following requests will take exactly the same time.&lt;/p&gt;
&lt;p&gt;This is not true for the latter; since the time stamp is static, only initial request will see 0.03s time and you will get a copy of cached result back for every other request.&lt;/p&gt;
&lt;p&gt;So that&amp;#8217;s great. What is the solution to this? The solution is to have static time stamps that update within specific interval.&lt;/p&gt;
&lt;p&gt;For example, if you just replace all NOW()&amp;#8217;s with a time stamp that has a hard coded &amp;#8216;00&amp;#8217; for seconds, you will have a faster web site for any requests (- initial) it receives within a minute.&lt;/p&gt;
&lt;pre class="prettyprint lang-sql"&gt;if (strpos($query, 'NOW()') !== false) {
    $query = str_replace('NOW()', '"'. date('Y-m-d H:i:00') .'"', $query);
}
&lt;/pre&gt;
&lt;p&gt; &lt;!-- more --&gt;&lt;/p&gt;
&lt;p&gt;That works great as a catch all, obviously some queries can (and should) have a longer stamp. For this you can re-use my Interval class:&lt;/p&gt;
&lt;pre class="prettyprint lang-php"&gt;&amp;lt;?php
namespace Time;

use \DateTime;
use \DateInterval;

/**
 * Provides ability to interval a fluid time stamp.
 *
 * @package Time
 * @license MIT
 * @author Aleksey Korzun &amp;lt;al.ko@webfoundation.net&amp;gt;
 * @link &lt;a href="http://alekseykorzun.com/post/7856565665/high-scalability-and-mysqls-now-dont-mix"&gt;http://alekseykorzun.com/post/7856565665/high-scalability-and-mysqls-now-dont-mix&lt;/a&gt;
 * @link &lt;a href="http://www.alekseykorzun.com"&gt;http://www.alekseykorzun.com&lt;/a&gt;
 */
class Interval
{
	/**
	 * Instance of DateTime
	 *
	 * @var DateTime
	 */
	protected static $date;

	/**
	 * Format used for output, defaults to Y-m-d H:i:s via initialize
	 *
	 * @var string
	 */
	protected static $format;

	/**
	 * Initialize class if it was not previously initialized
	 *
	 * @return void
	 */
	protected static function initialize()
	{
		// Generally we want to base our intervals on a single time stamp
		// per execution. If you have a script that requires a new time stamp
		// for intervals simply remove if wrapper around this code
		if (!self::$date) {
			self::$date = new DateTime();
		}

		if (!self::$format) {
			self::$format = 'Y-m-d H:i:s';
		}
	}

	/**
	 * Switch current time stamp to intervalled version
	 *
	 * @param DateInterval $interval intervals (in minutes) that you wish to break the date out to
	 * @return string processed time stamp
	 */
	public static function minutes(DateInterval $interval)
	{
		self::initialize();

		// Make sure current format supports minutes and interval is valid
		if (!self::hasMinutes() &amp;amp;&amp;amp; $interval-&amp;gt;i) {
			return false;
		}

		$minutes = (int) (self::$date-&amp;gt;format('i') - (self::$date-&amp;gt;format('i') % $interval-&amp;gt;i));
		if (strlen($minutes) == 1) {
			$minutes = 0 . $minutes;
		}

		// Notice that 's' is converted to 00 within getFormat() method
		return self::$date-&amp;gt;format(str_replace('i', $minutes, self::getFormat('s')));
	}

	/**
	 * Return processed version of current time stamp
	 *
	 * @param DateInterval $interval intervals (in seconds) that you wish to break the date out to
	 * @return string processed time stamp
	 */
	public static function seconds(DateInterval $interval)
	{
		self::initialize();

		// Make sure current format supports minutes and interval is valid
		if (!self::hasSeconds() &amp;amp;&amp;amp; $interval-&amp;gt;s) {
			return false;
		}

		$seconds = (int) (self::$date-&amp;gt;format('s') - (self::$date-&amp;gt;format('s') % (int) $interval-&amp;gt;s));
		if (strlen($seconds) == 1) {
			$seconds = 0 . $seconds;
		}

		return self::$date-&amp;gt;format(str_replace('s', $seconds, self::getFormat()));
	}

	/**
	 * Sets new date format
	 *
	 * @see &lt;a href="http://www.php.net/manual/en/function.date.php"&gt;http://www.php.net/manual/en/function.date.php&lt;/a&gt;
	 * @param string $format new date format to use
	 * @return void
	 */
	public static function setFormat($format)
	{
		self::$format = (string) $format;
	}

	/**
	 * Retrieves currently set date format with ability to overwrite certain
	 * parts of the date with a static number.
	 *
	 * @param string $swap convert specific format characters to a static '00'
	 * @return string format
	 */
	protected static function getFormat($swap = null)
	{
		if (!is_null($swap)) {
			return str_replace((array) $swap, '00', self::$format);
		}

		return self::$format;
	}

	/**
	 * Checks if current format supports minutes
	 *
	 * @return bool returns true if support is present
	 */
	protected static function hasMinutes()
	{
		return (strpos(self::$format, 'i') !== false);
	}

	/**
	 * Checks if current format supports seconds
	 *
	 * @return bool returns true if seconds are supported
	 */
	protected static function hasSeconds()
	{
		return (bool) (strpos(self::$format, 's') !== false);
	}
}
&lt;/pre&gt;
&lt;p&gt;For example, if you need to update your time stamp every 15 minutes you can do:&lt;/p&gt;
&lt;pre class="prettyprint lang-php"&gt;$sql = "SELECT count(1)
              FROM `schedule`
           WHERE `date` &amp;lt;= '" . Interval::minutes(new DateInterval('PT15M'))  . "'";
&lt;/pre&gt;
&lt;p&gt;Happy optimizing!&lt;/p&gt;</description><link>http://alekseykorzun.com/post/7856565665</link><guid>http://alekseykorzun.com/post/7856565665</guid><pubDate>Wed, 20 Jul 2011 16:56:00 -0400</pubDate><category>php</category><category>mysql</category><category>now()</category><category>c10k</category><category>scalability</category><category>performance</category><category>tips</category><category>benchmarks</category><category>timestamp</category></item><item><title>Final revision of Google.com pushed (12:00AM EST)</title><description>&lt;p&gt;&lt;a target="_blank" href="http://premium.imagesocket.com/images/2011/06/30/newgoogle1gif3579.GIF"&gt;&lt;img src="http://premium.imagesocket.com/images/2011/06/30/newgoogle1gif3579.GIF" align="middle"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://premium.imagesocket.com/images/2011/06/30/newgoogle2gifg1zy.GIF"&gt;&lt;img src="http://premium.imagesocket.com/images/2011/06/30/newgoogle2gifg1zy.GIF" align="middle"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Google just pushed final revision to their home page and search results. (12:00AM EST)&lt;/p&gt;</description><link>http://alekseykorzun.com/post/7071017910</link><guid>http://alekseykorzun.com/post/7071017910</guid><pubDate>Thu, 30 Jun 2011 00:19:13 -0400</pubDate><category>+1</category><category>google</category><category>homepage</category><category>search</category><category>revision</category><category>new</category></item><item><title>Integrating Facebook's Social Plug-ins with Facebook's PHP SDK (v.3.0)</title><description>&lt;p&gt;I made a quick modification to &lt;a target="_blank" href="https://github.com/facebook/php-sdk"&gt;Facebook&amp;#8217;s PHP SDK (v.3.0.0)&lt;/a&gt; that seamlessly integrates new token format that Facebook&amp;#8217;s &lt;a target="_blank" href="http://developers.facebook.com/docs/plugins/"&gt;social plug-ins&lt;/a&gt; utilize.&lt;/p&gt;
&lt;p&gt;No need to settle for older (&lt;span&gt;v2.2.x) version of SDK or create dirty work &lt;/span&gt;&lt;span&gt;arounds &lt;/span&gt;&lt;span&gt;in order to retrieve and pass token data to PHP.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Grab it from: &lt;a href="https://github.com/AlekseyKorzun/php-sdk"&gt;&lt;a href="https://github.com/AlekseyKorzun/php-sdk"&gt;https://github.com/AlekseyKorzun/php-sdk&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here is a diff if you just want to patch v.3.x SDK on your own: &lt;a href="https://github.com/AlekseyKorzun/php-sdk/commit/dbb230a8acb9ccfa0b6fa90fcd3caecd3b677dac#L0L315"&gt;&lt;a href="https://github.com/AlekseyKorzun/php-sdk/commit/dbb230a8acb9ccfa0b6fa90fcd3caecd3b677dac#L0L315"&gt;https://github.com/AlekseyKorzun/php-sdk/commit/dbb230a8acb9ccfa0b6fa90fcd3caecd3b677dac#L0L315&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;P.S Facebook will be making changes to authentication system for both JS and PHP SDK&amp;#8217;s so keep an eye on their &lt;a href="https://github.com/facebook/php-sdk"&gt;GitHub &lt;/a&gt;for further updates.&lt;/p&gt;</description><link>http://alekseykorzun.com/post/6925234287</link><guid>http://alekseykorzun.com/post/6925234287</guid><pubDate>Sat, 25 Jun 2011 23:24:47 -0400</pubDate><category>php</category><category>github</category><category>facebook</category><category>connect</category><category>social</category><category>plug-ins</category><category>sdk</category><category>tokens</category><category>authenication</category></item><item><title>Why are you still using Facebook's legacy JavaScript SDK?</title><description>&lt;p&gt;Usually when developers focus on page load times, things like third party API&amp;#8217;s are often overlooked or simply ignored (&lt;em&gt;it works, why should I re-implement this?!&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;Today I noticed that one of the web sites I visit still used old &lt;a title="Javascript SDK" target="_blank" href="http://developers.facebook.com/docs/reference/oldjavascript/"&gt;Javascript SDK&lt;/a&gt; offered by Facebook. To my surprise that seemed to be a very common trend: &lt;a title="http://www.google.com/&amp;amp;q=%22Cross-domain+Receiver+Page%22" target="_blank" href="http://www.google.com/&amp;amp;q=%22Cross-domain+Receiver+Page%22"&gt;&lt;a href="http://www.google.com/&amp;amp;q=%22Cross-domain+Receiver+Page%22"&gt;http://www.google.com/&amp;amp;q=%22Cross-domain+Receiver+Page%22&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In some cases, the subjects already use the new &lt;a title="SDK" target="_blank" href="http://developers.facebook.com/docs/reference/javascript/"&gt;SDK&lt;/a&gt; (due to integration of social plugins) on top of the legacy SDK.&lt;/p&gt;
&lt;p&gt;This is a case of double dipping that will cost you traffic &lt;em&gt;&lt;strong&gt;and &lt;/strong&gt;&lt;/em&gt;revenue.&lt;/p&gt;
&lt;h3&gt;Footprint:&lt;/h3&gt;
&lt;p&gt;Old SDK loads a total of 5 files in a package that&amp;#8217;s roughly &lt;strong&gt;241.93KB&lt;/strong&gt; in size.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lndegv80y81qis7rm.png"/&gt;&lt;/p&gt;
&lt;p&gt;On the other hand the new SDK loads a total of 2 files in a package that&amp;#8217;s roughly &lt;strong&gt;125.07KB &lt;/strong&gt;in size.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lndek7umYv1qis7rm.png"/&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s almost 50% less data for end user to transfer and 50%+ less requests for their browser to make.&lt;/p&gt;
&lt;h3&gt;Delivery:&lt;/h3&gt;
&lt;p&gt;Both versions are hosted on Akamai but there is a difference between the two approaches.&lt;/p&gt;
&lt;p&gt;Old:&lt;/p&gt;
&lt;pre class="prettyprint lang-sh"&gt;a1875.g.akamai.net has address 204.0.5.56
a1875.g.akamai.net has address 204.0.5.59&lt;/pre&gt;
&lt;p&gt;New:&lt;/p&gt;
&lt;pre class="prettyprint lang-sh"&gt;e3821.c.akamaiedge.net has address 184.86.159.139&lt;/pre&gt;

&lt;p&gt;As you can see the new SDK is hosted on Akamai Edge Platform which is basically a Akamai CDN on steroids.&lt;/p&gt;
&lt;blockquote&gt;&lt;span&gt;Akamai&amp;#8217;s EdgePlatform is one of the world&amp;#8217;s largest distributed computing platforms. It is a network of more than 84,000 secure servers equipped with proprietary software and deployed in 72 countries that relies on applied mathematics and algorithms to help solve congestion and vulnerability problems on the Internet. These servers reside within approximately 1,000 of the world&amp;#8217;s networks monitoring the Internet in real time - gathering information about traffic, congestion, and trouble spots. Akamai uses this intelligence to optimize routes and replicate data dynamically to deliver content and applications more quickly, reliably, and securely.&lt;/span&gt;&lt;/blockquote&gt;
&lt;p&gt;You can read more about it here: &lt;a href="http://www.akamai.com/html/technology/edgeplatform.html"&gt;&lt;a href="http://www.akamai.com/html/technology/edgeplatform.html"&gt;http://www.akamai.com/html/technology/edgeplatform.html&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;ul&gt;&lt;li&gt;Much smaller foot print&lt;/li&gt;
&lt;li&gt;Twice as less requests for additional data&lt;/li&gt;
&lt;li&gt;Better content delivery platform&lt;/li&gt;
&lt;li&gt;Most of the new social plug-ins already use it&lt;/li&gt;
&lt;li&gt;The only SDK Facebook actively supports&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So go make a switch now, your users will thank you.&lt;/p&gt;</description><link>http://alekseykorzun.com/post/6919673137</link><guid>http://alekseykorzun.com/post/6919673137</guid><pubDate>Sat, 25 Jun 2011 20:23:49 -0400</pubDate><category>facebook</category><category>connect</category><category>javascript</category><category>sdk</category><category>performance</category><category>ux</category><category>content</category><category>delivery</category><category>akamai</category><category>api</category><category>platform</category></item><item><title>phpAudit v1.0 </title><description>&lt;p&gt;&lt;b&gt;phpAudit&lt;/b&gt; is a simple shell script that scans PHP files for possible security risks.&lt;/p&gt;
&lt;p&gt;
I made this script to speed up verification of third party PHP libraries/code (so a random fork of XYZ API library does not send your credentials to a third-party server via a sneaky method).
&lt;/p&gt;
&lt;h3&gt;Usage:&lt;/h3&gt;
&lt;p&gt;
Make sure script has execution permissions (chmod a+x phpAudit) and simply run:
&lt;b&gt;./phpAudit /path/to/directory/with/php/files/&lt;/b&gt; to begin scanning.
&lt;/p&gt;
&lt;h3&gt;Demo:&lt;/h3&gt;

&lt;pre class="prettyprint lang-sh"&gt;
./phpAudit library/Facebook/
[:)] Scanning 'library/Facebook/'...
[+] Checking for PHP configuration modifications
[+] Checking for network operations
  [!] Warning: library/Facebook/base_facebook.php
$result = curl_exec($ch); if (curl_errno($ch) == 60) { // CURLE_SSL_CACERT curl_setopt($ch, CURLOPT_CAINFO, $result = curl_exec($ch); 'error_code' =&amp;gt; curl_errno($ch), 'message' =&amp;gt; curl_error($ch), 'type' =&amp;gt; 'CurlException', curl_close($ch); curl_close($ch);
[+] Checking for system operations
  [!]Warning: library/Facebook/base_facebook.php
$result = curl_exec($ch); $result = curl_exec($ch);
&lt;/pre&gt;

&lt;p&gt;
Grab the script from here: &lt;a href="https://github.com/AlekseyKorzun/phpAudit"&gt;&lt;a href="https://github.com/AlekseyKorzun/phpAudit"&gt;https://github.com/AlekseyKorzun/phpAudit&lt;/a&gt;&lt;/a&gt;, might be a little buggy since it was put together at 2 AM :-)&lt;/p&gt;</description><link>http://alekseykorzun.com/post/6895798411</link><guid>http://alekseykorzun.com/post/6895798411</guid><pubDate>Sat, 25 Jun 2011 02:53:00 -0400</pubDate><category>php</category><category>audit</category><category>security</category><category>scanner</category><category>scans</category><category>risks</category></item><item><title>
Finally did my first in and out car wash after swapping back my...</title><description>&lt;img src="http://24.media.tumblr.com/tumblr_lmrkkoxZWT1qjg7ico1_500.jpg"/&gt;&lt;br/&gt; &lt;br/&gt;&lt;img src="http://24.media.tumblr.com/tumblr_lmrkkoxZWT1qjg7ico2_500.jpg"/&gt;&lt;br/&gt; &lt;br/&gt;&lt;p&gt;
Finally did my first in and out car wash after swapping back my Hella tail lights (was missing protective gaskets so I had to settle for my LED’s).
&lt;/p&gt;
&lt;p&gt;
Also I decided to try Armor All wheel protectant (seems to be getting good reviews). A bit pricey but if I don’t have to spend an hour every other weekend to clean my rims it will be a welcome addition to my auto care toolkit.
&lt;/p&gt;</description><link>http://alekseykorzun.com/post/6513052407</link><guid>http://alekseykorzun.com/post/6513052407</guid><pubDate>Tue, 14 Jun 2011 01:00:25 -0400</pubDate><category>e34</category><category>540</category><category>concepts</category><category>matte</category><category>flat</category><category>custom</category><category>new york</category><category>love</category></item></channel></rss>
