Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Improving the caching mechanism, in preparation for providing a memcache... #9

Merged
merged 5 commits into from

3 participants

Brandon Savage Romain d'Alverny Adrian Gaudebert
Brandon Savage

... option for IT to utilize in production.

Brandon Savage

Is anybody ever going to review this pull request?

Romain d'Alverny

Can't test it right now, but it makes sense.

I will need to resubmit my own postgresql/sqlite branch after this one makes it through.

Adrian Gaudebert

Both memcache and apc cache work, we now need the MySQL cache to work and this is going in.

For information, there is an unserialize error with the MySQL cache, Brandon is working on it.

Brandon Savage

Adrian, I believe the MySQL cache errors are fixed. Can you retest?

cache/BugzillaCacheMysql.class.php
((45 lines not shown))
+ '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 . '"',
Adrian Gaudebert Owner

Don't you need to add some quotes here as well?

Adrian Gaudebert Owner

Around the keyword key I mean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brandon Savage brandonsavage merged commit 7ba55fd into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 31, 2012
  1. Brandon Savage

    Improving the caching mechanism, in preparation for providing a memca…

    brandonsavage authored
    …che option for IT to utilize in production.
Commits on Jun 4, 2012
  1. Brandon Savage
Commits on Aug 14, 2012
  1. Brandon Savage
Commits on Dec 11, 2012
  1. Brandon Savage
  2. Brandon Savage

    Fixing conflict.

    brandonsavage authored
This page is out of date. Refresh to see the latest.
4 Bugzilla.php
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';
+
+
/**
2  BugzillaOutput.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';
9 BugzillaQuery.class.php
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;
}
}
18 cache/BugzillaCacheApc.class.php
View
@@ -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);
+ }
+
+}
35 cache/BugzillaCacheMemcache.class.php
View
@@ -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);
+ }
+
+}
73 cache/BugzillaCacheMysql.class.php
View
@@ -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 .'"')
);
Something went wrong with that request. Please try again.