Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

Commit

Permalink
use unix_ts. Added fetchOne to Qbuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
diversen committed Apr 8, 2011
1 parent e326c80 commit a38e061
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
27 changes: 21 additions & 6 deletions lib/cache.php
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion lib/db.php
Expand Up @@ -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.
*/
Expand Down

0 comments on commit a38e061

Please sign in to comment.