Permalink
Browse files

* Fix cache invalidation?!

  • Loading branch information...
1 parent bb9559f commit 73e4267234877f85c1add9714131d3edc3ac2322 Danny Allen committed Jul 12, 2011
@@ -17,23 +17,30 @@
class Cache {
public static function exists($key) {
- return apc_exists(APP_ID . '_' . $key);
+ self::getKey($key);
+
+ return apc_exists($key['full']);
}
public static function load($key, $unserialize = false) {
+ self::getKey($key);
+
if ($unserialize) {
- return Db::unserialize(apc_fetch(APP_ID . '_' . $key));
+ return Db::unserialize(apc_fetch($key['full']));
} else {
- return apc_fetch(APP_ID . '_' . $key);
+ return apc_fetch($key['full']);
}
}
public static function loadSave($key, $function, $args = array(), $newData = null,
$serialize = false, $ttl = 0) {
+ self::getKey($key);
+
+ // attempt to load data
$data = self::load($key, $serialize);
if (!empty($data)) {
@@ -54,16 +61,18 @@ public static function loadSave($key, $function, $args = array(), $newData = nul
}
- public static function delete($key, $baseKey = APP_ID) {
- if (is_array($key)) {
+ public static function delete($key) {
+ self::getKey($key);
+
+ if (is_array($key['id'])) {
$success = true;
- foreach ($key as $theKey) {
- if ($baseKey === false) {
+ foreach ($key['id'] as $theKey) {
+ if ($key['base'] === false) {
// sometimes, we can only pass the full key name
$tmpSuccess = apc_delete($theKey);
} else {
- $tmpSuccess = apc_delete($baseKey . '_' . $theKey);
+ $tmpSuccess = apc_delete($key['base'] . '_' . $theKey);
}
// report any failures
@@ -75,23 +84,26 @@ public static function delete($key, $baseKey = APP_ID) {
return $success;
} else {
- if ($baseKey === false) {
+ if ($key['base'] === false) {
// sometimes, we can only pass the full key name
- return apc_delete($key);
+ return apc_delete($key['id']);
+
} else {
- return apc_delete($baseKey . '_' . $key);
+ return apc_delete($key['full']);
}
}
}
- public static function deletePartial($key, $baseKey = APP_ID) {
+ public static function deletePartial($key) {
+ self::getKey($key);
+
$deleted = 0;
$cache = apc_cache_info('user');
foreach ($cache['cache_list'] as $item) {
- if ((strpos($item['info'], $baseKey . '_') !== false) &&
- (strpos($item['info'], $key) !== false)) {
+ if ((strpos($item['info'], $key['base'] . '_') !== false) &&
+ (strpos($item['info'], $key['id']) !== false)) {
// partial key found (in app namespace!), delete
self::delete($item['info'], false);
@@ -104,12 +116,14 @@ public static function deletePartial($key, $baseKey = APP_ID) {
public static function store($key, $data, $serialize = false, $ttl = 0) {
+ self::getKey($key);
+
if (function_exists('apc_add')) {
if ($serialize) {
$data = Db::serialize($data);
}
- return apc_add(APP_ID . '_' . $key, $data, $ttl);
+ return apc_add($key['full'], $data, $ttl);
} else {
return self::save($key, $data, $serialize, $ttl);
@@ -118,17 +132,21 @@ public static function store($key, $data, $serialize = false, $ttl = 0) {
public static function save($key, $data, $serialize = false, $ttl = 0) {
+ self::getKey($key);
+
if ($serialize) {
$data = Db::serialize($data);
}
- return apc_store(APP_ID . '_' . $key, $data, $ttl);
+ return apc_store($key['full'], $data, $ttl);
}
public static function getMinJs($key, $script, $minScript = null) {
+ self::getKey($key);
+
// output filename
- $filename = '/js/min/' . $key . '.js';
+ $filename = '/js/min/' . $key['id'] . '.js';
if (!is_file(BASE_DIR . $filename)) {
// minify script
@@ -194,6 +212,28 @@ public static function getMinInlineJs($script, $cacheKey = null) {
return $min;
}
+
+
+ private static function getKey(&$key) {
+ if (is_array($key)) {
+ if (isset($key['base']) || isset($key['id'])) {
+ // already set, return
+ return true;
+ }
+
+ $tmp['base'] = $key[0];
+ $tmp['id'] = $key[1];
+
+ } else {
+ $tmp['base'] = APP_ID;
+ $tmp['id'] = $key;
+ }
+
+ $tmp['full'] = $tmp['base'] . '_' . $tmp['id'];
+
+ // set
+ $key = $tmp;
+ }
}
?>
@@ -55,10 +55,20 @@ public static function getLastIssueDate($timewarp = null, $getValid = true, $onl
if ($getValid) {
// load list of issues
if (!$onlyPublished) {
- $issues = Cache::loadSave('issue_latest_unpublished', 'Digest::loadDigests', array('issue', 'latest', false));
+ $issues = Cache::loadSave(array('base' => DIGEST_APP_ID,
+ 'id' => 'issue_latest_unpublished'),
+ 'Digest::loadDigests',
+ array('issue',
+ 'latest',
+ false));
} else {
// only get published
- $issues = Cache::loadSave('issue_latest', 'Digest::loadDigests', array('issue', 'latest', true));
+ $issues = Cache::loadSave(array('base' => DIGEST_APP_ID,
+ 'id' => 'issue_latest'),
+ 'Digest::loadDigests',
+ array('issue',
+ 'latest',
+ true));
}
$key = self::findIssueDate($date, $issues, $accurate);
@@ -208,6 +208,12 @@ public static function getAvailableSettings() {
'1' => _('Yes')),
'default' => '1',
'example' => null);
+ $tmp['AUTO_DISCARD_COMMITS'] = array('title' => _('Auto Discard Commits'),
+ 'valid' => array('0' => _('No'),
+ '1' => _('Yes')),
+ 'default' => '1',
+ 'example' => null);
+
$tmp['DATA_TERMS_VERSION'] = array('title' => _('Data Terms Version'),
'valid' => null,
'default' => 0.1,
@@ -292,6 +298,7 @@ public static function getGroupedSettings() {
'HELP_CONTAINER' => $tmp['HELP_CONTAINER'],
'SMTP' => $tmp['SMTP'],
'SHOW_INSERT' => $tmp['SHOW_INSERT'],
+ 'AUTO_DISCARD_COMMITS' => $tmp['AUTO_DISCARD_COMMITS'],
'DATA_TERMS_VERSION' => $tmp['DATA_TERMS_VERSION'],
'SURVEY_ACTIVE' => $tmp['SURVEY_ACTIVE']));
@@ -31,7 +31,12 @@ public function __construct() {
$this->title = _('Home');
// load data
- $this->issues = Db::reindex(Cache::loadSave('issue_latest', 'Digest::loadDigests', array('issue', 'latest')), 'date');
+ $this->issues = Db::reindex(Cache::loadSave(array('base' => DIGEST_APP_ID,
+ 'id' => 'issue_latest'),
+ 'Digest::loadDigests',
+ array('issue',
+ 'latest')),
+ 'date');
// find 6 months ago, 1 year ago, random digests
if ($this->issues) {
View
@@ -50,17 +50,39 @@ public function __construct($id, $issue) {
if ($this->review) {
// if review, look at all issues
if ($this->id == 'issues') {
- $issues = Cache::loadSave('issue_latest_unpublished', 'Digest::loadDigests', array('issue', 'latest', false));
+ $issues = Cache::loadSave(array('base' => DIGEST_APP_ID,
+ 'id' => 'issue_latest_unpublished'),
+ 'Digest::loadDigests',
+ array('issue',
+ 'latest',
+ false));
+
} else if ($this->id == 'archive') {
- $issues = Cache::loadSave('archive_latest_unpublished', 'Digest::loadDigests', array('archive', 'latest', false));
+ $issues = Cache::loadSave(array('base' => DIGEST_APP_ID,
+ 'id' => 'archive_latest_unpublished'),
+ 'Digest::loadDigests',
+ array('archive',
+ 'latest',
+ false));
}
} else {
// if not review, only look at published issues
if ($this->id == 'issues') {
- $issues = Cache::loadSave('issue_latest', 'Digest::loadDigests', array('issue', 'latest', true));
+ $issues = Cache::loadSave(array('base' => DIGEST_APP_ID,
+ 'id' => 'issue_latest'),
+ 'Digest::loadDigests',
+ array('issue',
+ 'latest',
+ true));
+
} else if ($this->id == 'archive') {
- $issues = Cache::loadSave('archive_latest', 'Digest::loadDigests', array('archive', 'latest', true));
+ $issues = Cache::loadSave(array('base' => DIGEST_APP_ID,
+ 'id' => 'archive_latest'),
+ 'Digest::loadDigests',
+ array('archive',
+ 'latest',
+ true));
}
}
@@ -57,7 +57,11 @@ public function __construct($id) {
$type = 'issue';
}
- $this->data = Cache::loadSave($type . '_' . $this->sortType, 'Digest::loadDigests', array($type, $this->sortType));
+ $this->data = Cache::loadSave(array('base' => DIGEST_APP_ID,
+ 'id' => $type . '_' . $this->sortType),
+ 'Digest::loadDigests',
+ array($type,
+ $this->sortType));
}

0 comments on commit 73e4267

Please sign in to comment.