diff --git a/lib/cache.php b/lib/cache.php index 5fba5cf8..a146618b 100644 --- a/lib/cache.php +++ b/lib/cache.php @@ -19,15 +19,27 @@ public static function generateId ($module, $id){ * get a cached string * @param string $module * @param int $id + * @param int $lifetime in secs * @return string $str */ - public static function get ($module, $id) { + public static function get ($module, $id, $max_life_time = null) { $id = self::generateId($module, $id); - $db = new db(); - $search = array ('id' => $id); - $row = $db->selectOne('system_cache', null, $search); - if ($row){ + QBuilder::setSelect('system_cache'); + QBuilder::filter('id =', $id); + $row = QBuilder::fetchSingle(); + + if (!$row) { + return null; + } + if ($max_life_time){ + $expire = $row['unix_ts'] + $max_life_time; + if ($expire < time()){ + return null; + } else { + return unserialize($row['data']); + } + } else { return unserialize($row['data']); } return null; @@ -41,9 +53,12 @@ public static function get ($module, $id) { * @return strin $str */ public static function set ($module, $id, $data) { + self::delete($module, $id); + $id = self::generateId($module, $id); $db = new db(); - $values = array ('id' => $id); + + $values = array ('id' => $id, 'unix_ts' => time()); $values['data'] = serialize($data); return $db->insert('system_cache', $values); } diff --git a/lib/db.php b/lib/db.php index 48738215..cf8ba3d2 100644 --- a/lib/db.php +++ b/lib/db.php @@ -587,10 +587,17 @@ public static function fetch (){ self::$debug[] = self::$query; self::unsetVars(); self::$query = null; - return $rows; } + public static function fetchSingle (){ + $rows = self::fetch(); + if (isset($rows[0])){ + return $rows[0]; + } + return null; + } + /** * method for unsetting static vars when an operation is compleate. */