From 3c46aa53353ab90b39fb43f6117541af53851dce Mon Sep 17 00:00:00 2001 From: shish Date: Wed, 30 Apr 2008 11:26:12 +0000 Subject: [PATCH] memcache api git-svn-id: file:///home/shish/svn/shimmie2/trunk@833 7f39781d-f577-437e-ae19-be835c7a54ca --- core/database.class.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/core/database.class.php b/core/database.class.php index c3aa4957..912b976d 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -37,6 +37,7 @@ class Database { var $db; var $extensions; var $get_images = "SELECT images.*,UNIX_TIMESTAMP(posted) AS posted_timestamp FROM images "; + var $cache_hits = 0, $cache_misses = 0; /* * Create a new database object using connection info @@ -46,6 +47,7 @@ class Database { if(is_readable("config.php")) { require_once "config.php"; $this->db = @NewADOConnection($database_dsn); + $this->use_memcache = isset($memcache); if($this->db) { $this->db->SetFetchMode(ADODB_FETCH_ASSOC); $this->db->Execute("SET NAMES utf8"); // FIXME: mysql specific :| @@ -64,6 +66,10 @@ class Database { "; exit; } + if($this->use_memcache) { + $this->memcache = new Memcache; + $this->memcache->pconnect('localhost', 11211) or ($this->use_memcache = false); + } } else { header("Location: install.php"); @@ -71,6 +77,33 @@ class Database { } } +// memcache {{{ + public function cache_get($key) { + if($this->use_memcache) { + $val = $this->memcache->get($key); + if($val) { + $this->cache_hits++; + return $val; + } + else { + $this->cache_misses++; + } + } + return false; + } + + public function cache_set($key, $val, $time=0) { + if($this->use_memcache) { + $this->memcache->set($key, $val, false, $time); + } + } + + public function cache_delete($key) { + if($this->use_memcache) { + $this->memcache->delete($key); + } + } +// }}} // misc {{{ public function count_pages($tags=array()) { global $config;