Permalink
Browse files

Merge pull request #9 from brandonsavage/caching_changes

Improving the caching mechanism, in preparation for providing a memcache...
  • Loading branch information...
2 parents 496349b + bc3a498 commit 7ba55fd73065ebfdafb926e2694c5e95f4fcf9e1 @brandonsavage brandonsavage committed Dec 11, 2012
View
@@ -37,6 +37,10 @@
$wgAutoloadClasses['BugzillaCacheI'] = $cwd . '/cache/BugzillaCacheI.class.php';
$wgAutoloadClasses['BugzillaCacheMysql'] = $cwd . '/cache/BugzillaCacheMysql.class.php';
$wgAutoloadClasses['BugzillaCacheDummy'] = $cwd . '/cache/BugzillaCacheDummy.class.php';
+$wgAutoloadClasses['BugzillaCacheApc'] = $cwd . '/cache/BugzillaCacheApc.class.php';
+$wgAutoloadClasses['BugzillaCacheMemcache'] = $cwd . '/cache/BugzillaCacheMemcache.class.php';
+
+
/**
View
@@ -147,7 +147,7 @@ public function setup_template_data() {
$key = md5($this->query->id . $this->_get_size() . get_class($this));
$cache = $this->_getCache();
if($result = $cache->get($key)) {
- $image = $result['data'];
+ $image = $result;
$this->response->image = $wgBugzillaChartUrl . '/' . $image;
} else {
$this->response->image = $wgBugzillaChartUrl . '/' . $this->generate_chart($key) . '.png';
View
@@ -32,7 +32,7 @@ public function __construct($type, $options, $title) {
$this->cache = FALSE;
$this->_set_options($options);
}
-
+
protected function _getCache()
{
global $wgCacheObject;
@@ -119,7 +119,7 @@ public function fetch() {
// If the cache entry is older than this we need to invalidate it
$expiry = strtotime("-$wgBugzillaCacheMins minutes");
-
+
if( !$row ) {
// No cache entry
@@ -132,10 +132,7 @@ public function fetch() {
return $this->data;
}else {
// Cache is good, use it
-
- $this->id = $row['id'];
- $this->fetched_at = wfTimestamp(TS_DB, $row['fetched_at']);
- $this->data = unserialize($row['data']);
+ $this->data = unserialize($row);
$this->cached = TRUE;
}
}
@@ -323,4 +320,4 @@ public function _fetch_by_options() {
$this->data['message'];
}
}
-}
+}
@@ -0,0 +1,18 @@
+<?php
+
+class BugzillaCacheApc implements BugzillaCacheI
+{
+
+ public function set($key, $value, $ttl = 300) {
+ return apc_store($key, $value, $ttl);
+ }
+
+ public function get($key) {
+ return apc_fetch($key);
+ }
+
+ public function expire($key) {
+ return apc_delete($key);
+ }
+
+}
@@ -0,0 +1,35 @@
+<?php
+
+class BugzillaCacheMemcache implements BugzillaCacheI
+{
+
+ protected $_memcache;
+
+ public function __construct() {
+ // As much as I detest using a global here, it is necessary to avoid
+ // needing to inject the $wgMemc object, thus breaking the usefulness
+ // of the interface. Using the $wgMemc object is important for the
+ // consistency of the code.
+ global $wgMemc;
+ $this->_memcache = $wgMemc;
+ }
+
+ public function set($key, $value, $ttl = 300) {
+ // Get the wikimedia key style expected
+ $key = wfMemcKey($key);
+ return $this->_memcache->set($key, $value, $ttl);
+ }
+
+ public function get($key) {
+ // Get the wikimedia key style expected
+ $key = wfMemcKey($key);
+ return $this->_memcache->get($key);
+ }
+
+ public function expire($key) {
+ // Get the wikimedia key style expected
+ $key = wfMemcKey($key);
+ return $this->_memcache->delete($key);
+ }
+
+}
@@ -2,43 +2,51 @@
class BugzillaCacheMysql implements BugzillaCacheI
{
- protected $_slave;
- protected $_master;
-
- public function __construct()
- {
- // TO-DO: This methodology creates some difficulties to unit testing.
- $this->_slave = wfGetDB( DB_SLAVE );
- $this->_master = wfGetDB( DB_MASTER );
- }
-
+
+
public function set($key, $value, $ttl = 300)
{
- //TO-DO: It's probably a bad thing to write straight SQL against an
- // abstraction layer. The abstraction layer doens't offer full
- // functionality, though, and this reduces the number of queries
- // for something as simple as caching. Also, the doQuery() method
- // is marked "private" but that is commented out; that may change
- // in a future release.
- $key_c = $this->_master->strencode($key);
- $value_c = $this->_master->strencode($value);
- $date = wfTimestamp(TS_DB);
- $now = time(); // Using time() because it's a PHP built-in.
- $expires = $now+$ttl;
-
- $sql = 'REPLACE INTO bugzilla_cache
- (`key`, `fetched_at`, `data`, `expires`)
- VALUES
- ("%s", "%s", "%s", %d)';
-
- $sql = sprintf($sql, $key_c, $date, $value_c, $expires);
- $res = $this->_master->doQuery($sql);
+ $master = $this->_getDatabase();
+ $key_c = $key;
+ $value_c = $value;
+
+ $date = wfTimestamp(TS_DB);
+ $now = time(); // Using time() because it's a PHP built-in.
+ $expires = $now + $ttl;
+ if (null === $this->get($key)) {
+ $res = $master->insert(
+ 'bugzilla_cache',
+ array(
+ '`key`' => $key_c,
+ 'fetched_at' => $date,
+ 'data' => $value_c,
+ 'expires' => $expires
+ ),
+ __METHOD__
+ );
+ } else {
+ $res = $this->update(
+ 'bugzilla_cache',
+ array(
+ 'fetched_at' => $date
+ ),
+ '`key` = "' . $key_c . '"',
+
+ __METHOD__
+ );
+ }
+
return $res;
}
+ protected function _getDatabase($type = DB_MASTER) {
+ return wfGetDB($type);
+ }
+
public function get($key)
{
- $res = $this->_slave->select(
+ $slave = $this->_getDatabase(DB_SLAVE);
+ $res = $slave->select(
'bugzilla_cache',
array('id', 'fetched_at', 'data', 'expires'),
'`key` = "' . $key . '"',
@@ -54,12 +62,13 @@ public function get($key)
return;
}
- return $row;
+ return $row['data'];
}
public function expire($key)
{
- return $this->_master->delete(
+ $master = $this->_getDatabase();
+ return $master->delete(
'bugzilla_cache',
array('`key`="' . $key .'"')
);

0 comments on commit 7ba55fd

Please sign in to comment.