1. Francesco Laurita
  2. CodeIgniter Reactor

Commits

Francesco Laurita  committed 1959ee4

Added TTL functionality for the DB_cache class.
Now cache file can auto expire, based on defined parameter.
Es:
$this->db->cache_on(20);//The next queries will auto expire after 20 seconds
$query = $this->db->query("SELECT * FROM content");

  • Participants
  • Parent commits 1eb69a2
  • Branches default

Comments (0)

Files changed (2)

File system/database/DB_cache.php

View file
 		{
 			return FALSE;
 		}
-
-		return unserialize($cachedata);
+		
+		//Getting the expired time back
+		if ( ! preg_match("/(\d+TS--->)/", $cachedata, $match))
+		{
+			return FALSE;
+		}
+		
+		$saved_time = trim(str_replace('TS--->', '', $match[1]));
+		$data		= trim(str_replace($match[0], '', $cachedata));
+		//TTL disabled
+		if ($saved_time == 0)
+		{
+			log_message('debug', __CLASS__ . " TTL for this query is disabled, return it forever");
+			return unserialize($data);
+		}
+		
+		// Has the file expired? If so we'll delete it.
+		if (time() >= $saved_time)
+		{
+				@unlink($filepath);
+				log_message('debug', __CLASS__ . " Cache file for this query has expired. File deleted");
+				return FALSE;
+		}
+		
+		log_message('debug', __CLASS__ . " Return this query from cache");
+		return unserialize($data);
 	}
 
 	// --------------------------------------------------------------------
 
 			@chmod($dir_path, DIR_WRITE_MODE);
 		}
-
-		if (write_file($dir_path.$filename, serialize($object)) === FALSE)
+		
+		$expire = 0;
+		if ($this->db->cache_ttl > 0)
+		{
+			$expire = time() + $this->db->cache_ttl;
+		}
+		
+		if (write_file($dir_path.$filename,$expire . 'TS--->'. serialize($object)) === FALSE)
 		{
 			return FALSE;
 		}

File system/database/DB_driver.php

View file
 	var $cache_on		= FALSE;
 	var $cachedir		= '';
 	var $cache_autodel	= FALSE;
+	var $cache_ttl		= 0;//TTL in seconds
 	var $CACHE; // The cache class object
 
 	// Private variables
 	var $stmt_id;
 	var $curs_id;
 	var $limit_used;
-
+	
 
 
 	/**
 	 * Enable Query Caching
 	 *
 	 * @access	public
+	 * @param integer TTL in secods
 	 * @return	void
 	 */
-	function cache_on()
+	function cache_on($ttl = 0)
 	{
 		$this->cache_on = TRUE;
+		if (is_numeric($ttl))
+		{
+			$this->cache_ttl = $ttl;
+		}
 		return TRUE;
 	}
 
 	 */
 	function cache_off()
 	{
+		$this->cache_ttl = 0;
 		$this->cache_on = FALSE;
 		return FALSE;
 	}