Permalink
Browse files

Object cache, archive support

  • Loading branch information...
1 parent 56b6538 commit fe018c763cd8eaf65ddc9789e41e008ed0190c84 @brendonh committed Nov 10, 2011
@@ -0,0 +1,38 @@
+<?php
+
+class Archiver extends CApplicationComponent {
+
+ public $params;
+ public $conn;
+
+ public function put($key, $value) {
+ if ($this->conn) {
+ return dba_replace($key, $value, $this->conn);
+ }
+ $conn = dba_popen($this->params['filename'], "c", $this->params['handler']);
+ dba_replace($key, $value, $conn);
+ dba_close($conn);
+ }
+
+ public function get($key) {
+ Yii::log("Cache miss", "info");
+ if ($this->conn) {
+ return dba_fetch($key, $this->conn);
+ }
+ $conn = dba_open($this->params['filename'], "r", $this->params['handler']);
+ $value = dba_fetch($key, $conn);
+ dba_close($conn);
+ return $value;
+ }
+
+ public function openPersistent() {
+ $this->conn = dba_open($this->params['filename'], 'c', $this->params['handler']);
+ }
+
+ public function closePersistent() {
+ dba_close($this->conn);
+ unset($this->conn);
+ }
+
+}
+
@@ -26,19 +26,38 @@ class RedisConnection extends CApplicationComponent {
public $conn;
+ public $useCache = false;
+ public $cache = array();
+
public function init() {
$profile = new Predis\Profiles\ServerVersion24();
$this->conn = new YiidisClient(
$this->params,
array('profile'=>$profile, 'prefix'=>$this->params['prefix']));
if (isset($this->params['trace']) && $this->params['trace']) $this->conn->trace = true;
+ if (isset($this->params['cache']) && $this->params['cache']) $this->useCache = true;
parent::init();
}
public function get($key) {
- return $this->conn->get($key);
+ if ($this->useCache && isset($this->cache[$key])) {
+ return $this->cache[$key];
+ }
+ $val = $this->conn->get($key);
+ if ($this->useCache) $this->cache[$key] = $val;
+ return $val;
+ }
+
+ public function hget($key, $hashKey) {
+ $comboKey = "hash:{$key}--{$hashKey}";
+ if ($this->useCache && isset($this->cache[$comboKey])) {
+ return $this->cache[$comboKey];
+ }
+ $val = $this->conn->hget($key, $hashKey);
+ if ($this->useCache) $this->cache[$comboKey] = $val;
+ return $val;
}
public function set($key, $val, $scenario='update') {
@@ -50,6 +69,8 @@ public function set($key, $val, $scenario='update') {
// Wrong. What should this throw?
throw new ObjectExistsException($key);
}
+
+ if ($this->useCache) $this->cache[$key] = $val;
return true;
}
@@ -2,6 +2,7 @@
class RedisNoObject extends Exception {}
class RedisNotJSON extends Exception {}
+class RedisIllegalMultiGet extends Exception {}
class RedisModel extends CFormModel {
@@ -10,6 +11,7 @@ class RedisModel extends CFormModel {
public static $_keyPrefix = "misc";
public static $_expire;
public static $_compress = false;
+ public static $_archive = false;
public $_key;
public $_scenario;
@@ -82,6 +84,10 @@ public static function fromJSONArrays($keys, $blobs, $skipPrefix=false) {
public static function fromKeyArray($keys, $skipPrefix=false) {
$class = get_called_class();
+ if ($class::$_archive) {
+ throw new RedisIllegalMultiGet("Class is archived: $class");
+ }
+
if ($skipPrefix) {
$getKeys = $keys;
} else {
@@ -100,10 +106,22 @@ public static function getJSON($key, $skipPrefix=false) {
$fullKey = $class::$_keyPrefix . ':' . $key;
}
$json = Yii::app()->redis->get($fullKey);
- if (!$json) {
- throw new RedisNoObject($key);
+
+ if ($json) return $json;
+
+ if ($class::$_archive) {
+ $json = Yii::app()->archiver->get($fullKey);
+ if ($json) {
+ Yii::log("Caching $fullKey");
+ Yii::app()->redis->conn->set($fullKey, $json);
+ if ($class::$_expire) {
+ Yii::app()->redis->conn->expire($fullKey, $class::$_expire);
+ }
+ return $json;
+ }
}
- return $json;
+
+ throw new RedisNoObject($key);
}
public static function get($key, $skipPrefix=false) {
@@ -167,13 +185,18 @@ public function put() {
$data = $json;
}
- Yii::app()->redis->set($this->redisKey(),
- $data,
- $this->_scenario);
+ $key = $this->redisKey();
+
+ Yii::app()->redis->set($key, $data, $this->_scenario);
if ($class::$_expire) {
- Yii::app()->redis->conn->expire($this->redisKey(), $class::$_expire);
+ Yii::app()->redis->conn->expire($key, $class::$_expire);
}
+
+ if ($class::$_archive) {
+ Yii::app()->archiver->put($key, $data);
+ }
+
}
public function touch() {
@@ -15,7 +15,7 @@ public function updateFriendCache() {
}
$cacheKey = 'friends:' . $this->_key;
Yii::app()->redis->conn->del($cacheKey);
- Yii::app()->redis->conn->lpush($cacheKey, $friendIDs);
+ Yii::app()->redis->conn->sadd($cacheKey, $friendIDs);
}
}

0 comments on commit fe018c7

Please sign in to comment.