Guest User

Untitled

a guest
Jun 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. <?php
  2. /****
  3. inc_db.php
  4. ****/
  5. $server = "localhost";
  6. $login = "root";
  7. $password = "";
  8. $database = "tests_cache";
  9.  
  10.  
  11.  
  12. $conn = mysql_connect($server, $login, $password);
  13. mysql_select_db($database,$conn);
  14.  
  15. /**
  16.  * Make a SQL Simple Query (no caching)
  17.  * @author eka808
  18.  * @param $sql : SQL Statement
  19.  */
  20. function SQLQrySimple($sql)
  21. {
  22.     //Simple statement, no cachin
  23.     $req = mysql_query($sql) or die(mysql_error());
  24.     while ($datarow = mysql_fetch_assoc($req))
  25.         $data[] = $datarow;
  26.     return $data;
  27. }
  28.  
  29. /**
  30.  * Generate a cache file corresponding to a cached data
  31.  * @param unknown_type $cachefile : the cache file URI
  32.  * @param unknown_type $sql : the SQL statement of the data cached
  33.  */
  34. function GenerateCacheFile($cachefile, $sql)
  35. {
  36.     $cache_timelimit = 5;
  37.    
  38.     $cacheobject = new CachedLine();
  39.     $cacheobject->generation_time = time();
  40.     $cacheobject->data = SQLQrySimple($sql);
  41.     $cacheobject->time_limit = $cache_timelimit;
  42.  
  43.     $fh = fopen($cachefile, 'w');
  44.     file_put_contents($cachefile, serialize($cacheobject));
  45.            
  46.     return $cacheobject;
  47.    
  48. }
  49.  
  50.  
  51. /**
  52.  * Make a sql query with caching capabilities
  53.  * @param $sql : the sql statement of the data
  54.  * @param $query_id_forcaching : a UNIQUE id of a statement for caching
  55.  */
  56. function SQLQry($sql, $query_id_forcaching=null)
  57. {
  58.     //Folder when put the cache
  59.     $cache_folder = $_SERVER['DOCUMENT_ROOT'] . '/ILikeCache/cachetmp/';
  60.    
  61.    
  62.     if ($query_id_forcaching == null)
  63.     {
  64.         //
  65.         //No caching query
  66.         //
  67.         print_rr("NO CACHING");
  68.         return SQLQrySimple($sql);             
  69.     }
  70.     else
  71.     {
  72.         //
  73.         //Cached query
  74.         //
  75.         //Cache file generated (one file per query)
  76.         $cachefile = $cache_folder.$query_id_forcaching.".cache";
  77.        
  78.        
  79.         if (file_exists($cachefile))
  80.         {
  81.             //The file exists, there is a cache file for this query
  82.             print_rr("CACHED");
  83.            
  84.             $cached = file_get_contents($cachefile);
  85.             $cached = unserialize($cached);
  86.            
  87.             $current_timestamp = time();
  88.            
  89.             if (($current_timestamp - $cached->generation_time) > $cached->time_limit)
  90.             {
  91.                 //If the current timestamp minus generation time timestamp > time limit,
  92.                 //it's that the cache is expired, so regenerate one
  93.                 print_rr("EXPIRED");
  94.                
  95.                 unlink($cachefile);
  96.                 $cacheobject = GenerateCacheFile($cachefile, $sql);
  97.                 return $cacheobject;
  98.             }
  99.  
  100.             return $cached->data;
  101.            
  102.            
  103.         }
  104.         else
  105.         {
  106.             //
  107.             //Not cache for the query and it should ! (first generation of the cache ?)
  108.             //Generate the line
  109.             //
  110.             print_rr("NOT CACHED");
  111.             $cacheobject = GenerateCacheFile($cachefile, $sql);
  112.             return $cacheobject->data;
  113.         }
  114.        
  115.        
  116.     }
  117.    
  118. }
  119.  
  120. /**
  121.  * Tool class used by generation : represent a line
  122.  * @author eka808
  123.  *
  124.  */
  125. class CachedLine
  126. {
  127.     public $data;
  128.     public $generation_time;
  129.     public $time_limit;
  130. }
  131. ?>
  132.  
  133. <?php
  134. /****
  135. inc_fonc.php
  136. ****/
  137. function print_rr($statement)
  138. {
  139.     echo "<pre>";
  140.     print_r($statement);
  141.     echo "</pre>";
  142. }
  143. ?>
  144.  
  145. <?php
  146. /****
  147. indexphp
  148. ****/
  149. include_once ("inc/inc_db.php");
  150. include_once ("inc/inc_fonc.php");
  151.  
  152. $sql = "select * from News";
  153. $data = SQLQry($sql, 1);
  154. print_rr($data);
  155. ?>
  156. ?>
Add Comment
Please, Sign In to add comment