Advertisement
Guest User

OpenCart DB CACHED db driver - mysql, mysqli etc

a guest
Jun 11th, 2014
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2. // changed by Doru Brad
  3. // email me at dorubrad@gmail.com
  4. // this will cache queries using the opencart cache
  5. // just replace your db.php file with this one under system->library
  6. class DB {
  7.     private $driver;
  8.     private $cache;
  9.     private $cachedquery;
  10.  
  11.     public function __construct($driver, $hostname, $username, $password, $database) {
  12.         $this->cache = new Cache(DB_CACHED_EXPIRE);
  13.          
  14.         $file = DIR_DATABASE . $driver . '.php';
  15.  
  16.         if (file_exists($file)) {
  17.             require_once($file);
  18.  
  19.             $class = 'DB' . $driver;
  20.  
  21.             $this->driver = new $class($hostname, $username, $password, $database);
  22.         } else {
  23.             exit('Error: Could not load database driver type ' . $driver . '!');
  24.         }
  25.     }
  26.  
  27.     public function query($sql) {
  28.         $md5query = md5($sql);
  29.        
  30.         if ($query = $this->cache->get('sql_' . $md5query))
  31.         {
  32.             if ($query->sql == $sql)
  33.             {  
  34.  
  35.                 if ($resetflag = $this->cache->get('sql_globalresetcache'))
  36.                 {                  
  37.                     if ($resetflag <= $query->time)
  38.                     {
  39.                         $this->cachedquery = $query;
  40.                         return $query->return;
  41.                     };
  42.                 }
  43.                 else
  44.                 {
  45.                     $this->cachedquery = $query;
  46.                     return $query->return;
  47.                 };         
  48.                    
  49.             }
  50.         }
  51.    
  52.         $return = $this->driver->query($sql);
  53.        
  54.         $query = new stdClass();
  55.         $query->return = $return;
  56.         $query->sql = $sql;
  57.         $query->time = time();
  58.        
  59.         unset($this->cachedquery);
  60.        
  61.         $this->cache->set('sql_' . $md5query, $query);
  62.         return $return;
  63.     }
  64.  
  65.     public function escape($value) {
  66.         return $this->driver->escape($value);
  67.     }
  68.  
  69.     public function countAffected() {
  70.         return $this->driver->countAffected();
  71.     }
  72.  
  73.     public function getLastId() {
  74.         return $this->driver->getLastId();
  75.     }
  76. }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement