Ramblings on technology with a dash of social commentary
RSS icon Email icon Home icon
  • Redis 101 at Desert Code Camp 2011

    Posted on October 27th, 2011 phpguru No comments

    I’m proud to announce that I’ll be teaching Redis 101 at Desert Code Camp on November 5, 2011.

    Redis is a powerful memory-resident data store.

    In order to give you a good background on what Redis is, let’s take a trip down memory lane, back to our first lesson on how computers work. Computers have a hard disk drive, which enables data and files to be stored permanently (or, at least, for very long periods of time). They also have memory, or RAM, which is volatile, but very, very fast, comparatively.

    Why do we need both hard-drives and memory? Economics, mainly. Cheap, huge hard drives can store millions of files permanently, even if the power goes out. The tradeoff to all that massive amount of permanent storage is that hard drives are slow. RAM, on the other hand, is volatile storage, meaning that whatever is in RAM is lost when you power down your computer. Data in RAM is blazing fast, though. The trade-offs for all that blazing speed are high cost and impermanence.

    The latest Solid State Disks (SSD’s) are breaking some of these rules by being decent-sized, permanent storage that’s faster and quieter than a regular hard drive, but not as expensive as RAM. SSDs are more like hard drives or memory sticks with no moving parts. What’s Cool About SSDs would make a good blog post in the near future.

    Back to caching. In general terms, a cache is simply a faster place to retrieve data from.

    Your computer can access data stored in RAM much, much faster than it can access files on disk. When you launch a program, your computer is reading the application data from disk (slow) and loading it into memory (fast) so you can work on it.

    Several forms of caching are used to speed up surfing the web.

    You’re probably most familiar with client-side caching—your browser cache, or Temporary Internet Files for you Windows users. Web browsers use a local cache (just a special hidden folder) on your hard drive to store the stuff you’ve downloaded before. The theory here is, if you’ve already downloaded the home page of a site, chances are most of the files, scripts and stylesheets are reused across other pages on the stite, so by keeping local copy in the cache, your browser doesn’t have to re-fetch assets from the server again.

    Server-side caching, on the other hand, is a technique implemented by web application architects, to help speed up web applications. Some of the things that can be cached on the server-side include frequently-used files, query results, or processing PHP templates into their rendered HTML. With server-side caching, the idea is to alleviate some or most of the work your web server has to do when processing a particular request.

    Going back to our lesson on how computers work, we know hard drives are slow and memory is very fast. So when talking about maximum performance for web servers, we had better be looking at storing data in memory.

    Two of the most popular memory-caching platforms for PHP are Memcache and APC, the Alternative PHP Cache. These PHP extensions have been around a very long time, and are able to utilize RAM, instead of files on disk, to make data available to your PHP scripts almost instantly.

    Memcache and APC are known as key-value stores. Memcache is only a key-value store, meaning that’s basically the only feature it offers. APC is a key-value store, and also an opcode cache. An opcode cache actually compiles PHP scripts into machine-executable code, very much like compiling C# into a .DLL or Java code into a .war file. Compiled code runs faster than code that has to be parsed first.

    With any key-value store, you hand it a key and some data, and then later, you can lookup the data again almost instantly by using the original key.

    So why Redis? Like Memcache, Redis is also a key-value store, but it also offers several unique, blazing-fast data structures, including hashes, sets, lists and more. In addition, Redis includes a terrific write-to-disk feature as well as master-slave replication, giving you an extremely flexible and powerful set of tools, that fits in perfectly with any database-driven web application. And all for the amazing low-low price of only $0.00! By combining APC’s opcode cache capabilities, with the flexibility of Redis, you have everything you need to make your web applications really scream.

    Whether you’ve never used a caching platform before or you’re already fluent in Memcache or APC for high-performance website scalability, come and learn why Redis is quite possibly the best thing since sliced bread.

  • Using XDebug with Command-line PHP and NetBeans

    Posted on October 25th, 2011 phpguru No comments

    Magic sauce, so easy to misplace:

    export $XDEBUG_CONFIG="idekey=netbeans-xdebug"
    echo $XDEBUG_CONFIG
    idekey=netbeans-xdebug

    The string, `netbeans-xdebug` is configurable inside NetBeans preferences.

  • Enabling Command-line PHP Curl on Mac OS X Lion

    Posted on October 25th, 2011 phpguru No comments

    If you’re doing web development on the Mac with the Apache 2 and PHP 5.3.6 that ships from Apple, you might one day run into a use for some command-line scripts. I happen to be doing just that at the moment. Well it seems that Apple ships PHP 5 with everything you need to use the curl libraries, except the extension.

    I was surprised with an error warning like this:

    PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20090626/php_curl.dll' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20090626/php_curl.dll, 9): image not found in Unknown on line 0
    Huh? I need a Windows DLL on Lion? I guess so…

    I’ve been using CURL scripts from PHP running under Apache since day one, but what I’m realizing is that the command line version of PHP, aka, the php-cli, is installed a little differently.

    First, you may want to check your php.ini that the extension is uncommented (remove the preceding semicolon):
    extension=php_curl.dll This is a pretty obvious step to try, even though Lion is clearly not Windows, and the section in php.ini clearly says it’s for Windows Extensions.

    So you might try installing the latest version of cURL.
    cd ~
    mkdir src
    cd src
    curl -V
    curl 7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5

    At this point, you start wondering what’s going on, because cURL is installed at the Factory by Apple. Strange…

    So you can
    sudo find / -name php_curl -print just to see if the DLL is in the wrong place or something, maybe a quick symlink would fix it.
    Nope.

    But this is interesting… PHP 5.3.6 from Apple ships with the curl extension:

    cd /Users/%me%/Documents/src/php-5.3.6/ext/curl
    phpize
    ./configure
    make
    sudo make install

    Now edit your php.ini and add
    extension=curl.so
    and
    sudo apachectl restart
    Well that didn’t do much. Now I get:
    PHP Warning: Module 'curl' already loaded in Unknown on line 0

    Hmmm, stranger still.

    Here’s the punchline: All you need to do is comment out (add the semicolon at the front)
    ;extension=php_curl.dll
    and restart Apache.

    The warning was exactly correct – it couldn’t find a Windows DLL. I wonder why that’s uncommented.

    I don’t remember uncommenting it, but maybe I did.