From a56f5a25c92a4dcad97f3f49c745eeffda3ecd7b Mon Sep 17 00:00:00 2001 From: Elizabeth Southwell Date: Sat, 16 Apr 2016 11:15:01 -0400 Subject: [PATCH 1/2] Added a cache provider for caching to RethinkDB --- src/xPDO/Cache/xPDORethinkDBCache.php | 110 ++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/xPDO/Cache/xPDORethinkDBCache.php diff --git a/src/xPDO/Cache/xPDORethinkDBCache.php b/src/xPDO/Cache/xPDORethinkDBCache.php new file mode 100644 index 00000000..7e3b32ac --- /dev/null +++ b/src/xPDO/Cache/xPDORethinkDBCache.php @@ -0,0 +1,110 @@ +getOption('rethinkdb_host', $options, '127.0.0.1'); + $port = $this->getOption('rethinkdb_port', $options, '28015'); + $database = $this->getOption('rethinkdb_database', $options, 'test'); + $table = $this->getOption('rethinkdb_table', $options, 'cache'); + + $this->conn = r\connect($host, $port, $database); + + $this->table = r\table($table); + + $this->initialized = true; + } + + public function add($key, $var, $expire= 0, $options= array()) { + try { + // TODO: Although I do store an expires date, I have not added the simple check below to actually ensure cache expiration. + $this->table->insert(array( + 'id' => $this->getCacheKey($key), + 'content' => json_encode($var), + 'expires' => time() + $expire + ), array('conflict' => "replace"))->run($this->conn); + return true; + } catch (Exception $e) { + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDORethinkDB[{$this->key}]: Error adding cache item with key {$key}. {$e->getMessage()}"); + } + return false; + } + + public function set($key, $var, $expire= 0, $options= array()) { + return $this->add($key, $var, $expire, $options); + } + + public function replace($key, $var, $expire= 0, $options= array()) { + return $this->add($key, $var, $expire, $options); + } + + public function delete($key, $options= array()) { + if (!isset($options['multiple_object_delete']) || empty($options['multiple_object_delete'])) { + try { + $this->table->get($this->getCacheKey($key))->delete()->run($this->conn); + return true; + } catch (Exception $e) { + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDORethinkDB[{$this->key}]: Error deleting cache item with key {$key}. {$e->getMessage()}"); + } + } else { + try { + // TODO: Clear the cache with a wildcard correctly, instead of just flushing the entire thing. + $this->flush($options); + return true; + } catch (Exception $e) { + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDORethinkDB[{$this->key}]: Error flushing cache due to delete request for key {$key}. {$e->getMessage()}"); + } + } + return false; + } + public function get($key, $options= array()) { + try { + return json_decode($this->table->get($this->getCacheKey($key))->run($this->conn)['content'], true); + } catch (Exception $e) { + $this->xpdo->log(xPDO::LOG_LEVEL_WARN, "xPDORethinkDB[{$this->key}]: Cache item with key {$key} does not exist. {$e->getMessage()}"); + } + return null; + } + public function flush($options= array()) { + try { + // TODO: Flush the specific partition instead of everything. + $this->table->delete()->run($this->conn); + return true; + } catch (Exception $e) { + $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDORethinkDB[{$this->key}]: Error flushing cache partition. {$e->getMessage()}"); + } + return false; + } +} \ No newline at end of file From bd308d8ad6584f3c4bdce0aa844248908df5247d Mon Sep 17 00:00:00 2001 From: Elizabeth Date: Fri, 20 May 2016 17:44:14 -0400 Subject: [PATCH 2/2] Modify the read_mode for the cache table --- src/xPDO/Cache/xPDORethinkDBCache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/xPDO/Cache/xPDORethinkDBCache.php b/src/xPDO/Cache/xPDORethinkDBCache.php index 7e3b32ac..002e98e9 100644 --- a/src/xPDO/Cache/xPDORethinkDBCache.php +++ b/src/xPDO/Cache/xPDORethinkDBCache.php @@ -42,7 +42,7 @@ public function __construct(& $xpdo, $options = array()) { $this->conn = r\connect($host, $port, $database); - $this->table = r\table($table); + $this->table = r\table($table, array('read_mode' => 'outdated')); $this->initialized = true; } @@ -107,4 +107,4 @@ public function flush($options= array()) { } return false; } -} \ No newline at end of file +}