Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /****
- inc_db.php
- ****/
- $server = "localhost";
- $login = "root";
- $password = "";
- $database = "tests_cache";
- $conn = mysql_connect($server, $login, $password);
- mysql_select_db($database,$conn);
- /**
- * Make a SQL Simple Query (no caching)
- * @author eka808
- * @param $sql : SQL Statement
- */
- function SQLQrySimple($sql)
- {
- //Simple statement, no cachin
- $req = mysql_query($sql) or die(mysql_error());
- while ($datarow = mysql_fetch_assoc($req))
- $data[] = $datarow;
- return $data;
- }
- /**
- * Generate a cache file corresponding to a cached data
- * @param unknown_type $cachefile : the cache file URI
- * @param unknown_type $sql : the SQL statement of the data cached
- */
- function GenerateCacheFile($cachefile, $sql)
- {
- $cache_timelimit = 5;
- $cacheobject = new CachedLine();
- $cacheobject->generation_time = time();
- $cacheobject->data = SQLQrySimple($sql);
- $cacheobject->time_limit = $cache_timelimit;
- $fh = fopen($cachefile, 'w');
- file_put_contents($cachefile, serialize($cacheobject));
- return $cacheobject;
- }
- /**
- * Make a sql query with caching capabilities
- * @param $sql : the sql statement of the data
- * @param $query_id_forcaching : a UNIQUE id of a statement for caching
- */
- function SQLQry($sql, $query_id_forcaching=null)
- {
- //Folder when put the cache
- $cache_folder = $_SERVER['DOCUMENT_ROOT'] . '/ILikeCache/cachetmp/';
- if ($query_id_forcaching == null)
- {
- //
- //No caching query
- //
- print_rr("NO CACHING");
- return SQLQrySimple($sql);
- }
- else
- {
- //
- //Cached query
- //
- //Cache file generated (one file per query)
- $cachefile = $cache_folder.$query_id_forcaching.".cache";
- if (file_exists($cachefile))
- {
- //The file exists, there is a cache file for this query
- print_rr("CACHED");
- $cached = file_get_contents($cachefile);
- $cached = unserialize($cached);
- $current_timestamp = time();
- if (($current_timestamp - $cached->generation_time) > $cached->time_limit)
- {
- //If the current timestamp minus generation time timestamp > time limit,
- //it's that the cache is expired, so regenerate one
- print_rr("EXPIRED");
- unlink($cachefile);
- $cacheobject = GenerateCacheFile($cachefile, $sql);
- return $cacheobject;
- }
- return $cached->data;
- }
- else
- {
- //
- //Not cache for the query and it should ! (first generation of the cache ?)
- //Generate the line
- //
- print_rr("NOT CACHED");
- $cacheobject = GenerateCacheFile($cachefile, $sql);
- return $cacheobject->data;
- }
- }
- }
- /**
- * Tool class used by generation : represent a line
- * @author eka808
- *
- */
- class CachedLine
- {
- public $data;
- public $generation_time;
- public $time_limit;
- }
- ?>
- <?php
- /****
- inc_fonc.php
- ****/
- function print_rr($statement)
- {
- echo "<pre>";
- print_r($statement);
- echo "</pre>";
- }
- ?>
- <?php
- /****
- indexphp
- ****/
- include_once ("inc/inc_db.php");
- include_once ("inc/inc_fonc.php");
- $sql = "select * from News";
- $data = SQLQry($sql, 1);
- print_rr($data);
- ?>
- ?>
Add Comment
Please, Sign In to add comment