Posts in category "PHP"

Easy Caching with StashPHP

So short post this time, I swear I’m not dead, just busy!

Frequently with PHP you are going to need to cache things, mostly expensive SQL queries, but also data you aren’t going to want to be inserting into the database on every page hit, for instance website statistics.

With PHP we have a few options to achieve this;

  1. Caching with the file system.
    •  Upsides:
      • Works well with the Opcode cache
      • Usually the fastest method of caching for small or medium websites
    • Downsides:
      • Clearing the cache can be a lot slower as you will have to recursively search through path’s and delete.
  2. Caching with SqlLite
    1. Upsides:
      • Can be substantially faster than a full-blown RDBMS
      • All data is stored in a normal file in the host’s file system.
    2. Downsides:
      • Can only support one writer at a time, which can cause high file system latency, which is inconvenient if there are many clients trying to access it simultaneously.
  3. Caching with APC
    1. Upsides:
      • Makes PHP faster for you through the so called opcode caching.
      • No special configuration required.
    2. Downsides:
      • Practically none
  4. Caching with Memcached
    1. Upsides:
      • Allows machines to pool their memory together as one large memory cache, perfect for large websites.
      • Cross platform and cross RDBMS
    2. Downsides:
      • Stores data in the RAM, not ideal for small systems
      • Is considered to be a volatile in-memory key/value store
  5. Caching with Redis
    1. Upsides:
      1. Can act like memcached as a key/value store however it’s really a data structure server.
      2. Persistence to disk, by default.
      3. Values up to 512MB in size
      4. Built in clustering
      5. Extremely fast at everything
    2. Downsides:
      1. The more objects you put in it, the more memory its going to use.

So as you can see there are a bunch of different systems that handle caching in arguably better or worse ways depending on how big your website is. Putting a small website on Redis is probably overkill,  you might already have set up a RDBMS solution and now not want to change to a key value store etc.

This is where StashPHP comes in, you basically use the StashPHP library to cache things like so:

First you setup the driver to use, lets just use File System for the moment:

<?php // Create Driver with default options $driver = new Stash\Driver\FileSystem(); $driver->setOptions(array());

// Inject the driver into a new Pool object.
$pool = new Stash\Pool($driver);

Now you can setup your by wrapping the following code around your code:

<?php // Get a cache item. $item = $pool->getItem('path/to/item');

// Attempt to get the data
$data = $item->get();

// Check to see if the data was a miss.
if($item->isMiss())
{
    // Let other processes know that this one is rebuilding the data.
    $item->lock();

    // Run intensive code
    $data = codeThatTakesALongTime();

    // Store the expensive to generate data.
    $item->set($data);
}

// Continue as normal.
useDataForStuff($data);

Now rather than later on when you decide to add another cache, rather than needing to go rewrite all your caching calls etc. you can just change the setup of the drivers like so:

<?php $subDrivers = array(); $subDrivers[] = new Stash\Driver\Apc(); $subDrivers[] = new Stash\Driver\FileSystem(); $subDrivers[] = new Stash\Driver\Memcached(); $options = array('drivers' => $subDrivers);
$driver = new Stash\Driver\Composite($options);

$pool = new Stash\Pool($driver);

Presto, we’ve just avoided a nightmare!


Posted on September 07, 2015

WordPress – Shortcode in PHP files

So in WordPress with plugins etc. you end up using shortcode in posts to keep things simplier, an example would be if you want to display a gallery you would just use:
[galleries id="1"]
However trying to this when your outside of the “WordPress Loop” [in your theme files usually] you will hit the problem that WordPress won’t parse the shortcode. So whats the soloution? Super simple just append the following to your shortcode:
echo apply_filters('the_content', '[galleries id="1"]');
Presto, it works!

To read up more on shortcodes in WordPress click here.


Posted on September 11, 2011

PyroCMS – CMS on the CodeIgnitor Framework

So lately I’ve been playing around with PyroCMS, for any of you who don’t know about Pyro, its basically an open source content  management solution thats based on the CodeIgnitor framework. What does that mean you ask?

  • MVC design pattern. (Model->View->Controller)
  • Security,  Cross-Site Request Forgery protection, XSS filtering and very secure password encryption.
  • Caching, in order to achieve maximum performance.
  • Translated into 16 languages!
  • Extremely lightweight!
  • Support for modules, themes, plugins and widgets!
  • Multi – Site Manager (In Pro Version £45 )

Really it takes the pain out of  making your standard websites, you’ll have an area where the client can update their website in a simple environment  and thanks to ‘permissions’ you can be sure they won’t mess anything up!

Of course there is always going to be a stage when your better off without a framework, but for projects that it will work for, PyroCMS will save you a BUNCH of time!


Posted on August 14, 2011

Create a URL Shortener With Your Domain

YOURLS
If you use URL shortening services like TinyURL, it may be worth your while to setup a domain shortening service on your own domain. This is quiet easily done with a WordPress plugin now.
YOURLS is a free PHP script created by Lester Chan and Ozh Richard that allow you to create something similar to TinyURL.

  • It is easy to install.
  • It has a statistic side that allows you to keep track of all the URLs you are currently using.
  • It also easily allows you to choose between making your service open for all or just for you.
  • It lets you have customizable links instead of random characters.
  • Lastly it has a bookmarklet that allows you to make TinyURLs on the fly!

Download it and try it out! 😉


Posted on September 24, 2009