Skip to content

Commit

Permalink
Switch to Memcached
Browse files Browse the repository at this point in the history
In preparation for adding some cache warming background tasks.
  • Loading branch information
conormcd committed Dec 24, 2014
1 parent 832245a commit 7a80e90
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
1 change: 1 addition & 0 deletions .travis.php.ini
@@ -0,0 +1 @@
extension=memcached.so
16 changes: 11 additions & 5 deletions .travis.yml
@@ -1,15 +1,21 @@
language: php

php:
- 5.6
- 5.5
- 5.4
- hhvm

services:
- memcached

before_script:
- "make vendor"
- "export PHP_INI=~/.phpenv/versions/$(phpenv version-name)/etc/php.ini"
- "if [ \"$TRAVIS_PHP_VERSION\" == \"5.4\" ]; then echo \"extension=apc.so\" >> ${PHP_INI}; fi"
- "if [ \"$TRAVIS_PHP_VERSION\" == \"5.5\" ]; then printf \"\\n\" | pecl install apcu-beta; fi"
- "if [ \"$TRAVIS_PHP_VERSION\" == \"5.6\" ]; then printf \"\\n\" | pecl install apcu-beta; fi"
- "if [ \"$TRAVIS_PHP_VERSION\" != \"hhvm\" ]; then echo \"apc.enable_cli = 1\" >> ${PHP_INI}; fi"
- "if [ \"$TRAVIS_PHP_VERSION\" != \"hhvm\" ]; then phpenv config-add .travis.php.ini; fi"
- "make travis-env"

script: make

cache:
directories:
- vendor
47 changes: 37 additions & 10 deletions lib/Cache.php
@@ -1,11 +1,14 @@
<?php

/**
* A facade over APC.
* A facade over Memcached.
*
* @author Conor McDermottroe <conor@mcdermottroe.com>
*/
class Cache {
/** The connection to Memcached. */
private static $_memcached = null;

/**
* Retrieve an entry from the cache.
*
Expand All @@ -15,15 +18,17 @@ class Cache {
* not found.
*/
public static function get($key) {
$result = null;
$success = false;
if (Environment::get('CACHE_ENABLE') && function_exists('apc_fetch')) {
$result = apc_fetch($key, $success);
if (!$success) {
self::connect();

$value = null;
if (Environment::get('CACHE_ENABLE')) {
$value = self::$_memcached->get($key);
if (self::$_memcached->getResultCode() !== Memcached::RES_SUCCESS) {
Logger::debug("Cache miss for $key");
$value = null;
}
}
return $success ? $result : null;
return $value;
}

/**
Expand All @@ -36,9 +41,19 @@ public static function get($key) {
* @return void
*/
public static function set($key, $value, $ttl) {
if (Environment::get('CACHE_ENABLE') && function_exists('apc_store')) {
apc_store($key, $value, $ttl);
Logger::debug("Storing value at $key for $ttl seconds");
if (Environment::get('CACHE_ENABLE')) {
// The Memcached extension considers anything over 60*60*24*30 to
// be a UNIX timestamp, so we need to adjust for that here.
if ($ttl >= (60*60*24*30)) {
$ttl = $ttl + time();
}

self::$_memcached->set($key, $value, $ttl);
if (self::$_memcached->getResultCode() === Memcached::RES_SUCCESS) {
Logger::debug("Storing value at $key for $ttl seconds");
} else {
Logger::error("Failed to store $key in Memcached");
}
}
}

Expand Down Expand Up @@ -78,6 +93,18 @@ public static function run($key, $ttl, $callable, $args = array()) {
}
return $result;
}

/**
* Connect to Memcached if we aren't already connected.
*
* @return void
*/
private static function connect() {
if (self::$_memcached === null) {
self::$_memcached = new Memcached('www.mcdermottroe.com');
self::$_memcached->addServer('127.0.0.1', 11211);
}
}
}

?>

0 comments on commit 7a80e90

Please sign in to comment.