[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/ -> lmbDbCachedInfo.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/core/src/lmbSerializable.class.php');
  10  lmb_require('limb/core/src/lmbProxy.class.php');
  11  lmb_require('limb/fs/src/lmbFs.class.php');
  12  
  13  /**
  14   * class lmbDbCachedInfo.
  15   *
  16   * @package dbal
  17   * @version $Id: lmbDbCachedInfo.class.php 6003 2007-06-19 21:11:23Z pachanga $
  18   */
  19  class lmbDbCachedInfo extends lmbProxy
  20  {
  21    protected $conn;
  22    protected $db_info;
  23    protected $cache_file;
  24  
  25    function __construct($conn, $cache_dir = null)
  26    {
  27      $this->conn = $conn;
  28      if($cache_dir)
  29        $this->cache_file = $cache_dir . '/db_info.' . $conn->getHash() . '.cache';
  30    }
  31  
  32    function flushCache()
  33    {
  34      if($this->cache_file && file_exists($this->cache_file))
  35        unlink($this->cache_file);
  36    }
  37  
  38    protected function _createOriginalObject()
  39    {
  40      if($db_info = $this->_readFromCache())
  41        return $db_info;
  42  
  43      //forcing to load all metainfo
  44      $db_info = $this->conn->getDatabaseInfo();
  45      $tables = $db_info->getTableList();
  46      foreach($tables as $table)
  47        $db_info->getTable($table)->loadColumns();
  48  
  49      $this->_writeToCache($db_info);
  50      return $db_info;
  51    }
  52  
  53    protected function _readFromCache()
  54    {
  55      if($db_info = $this->_readFromMemCache())
  56        return $db_info;
  57  
  58      if($db_info = $this->_readFromFileCache())
  59      {
  60        $this->_writeToMemCache($db_info);
  61        return $db_info;
  62      }
  63    }
  64  
  65    protected function _readFromMemCache()
  66    {
  67      if(isset($this->db_info))
  68        return $this->db_info;
  69    }
  70  
  71    protected function _readFromFileCache()
  72    {
  73      if($this->_isFileCachingEnabled() && file_exists($this->cache_file))
  74      {
  75        $container = unserialize(file_get_contents($this->cache_file));
  76        $db_info = $container->getSubject();
  77        return $db_info;
  78      }
  79    }
  80  
  81    protected function _writeToCache($db_info)
  82    {
  83      $this->_writeToMemCache($db_info);
  84      $this->_writeToFileCache($db_info);
  85    }
  86  
  87    protected function _writeToMemCache($db_info)
  88    {
  89      $this->db_info = $db_info;
  90    }
  91  
  92    protected function _writeToFileCache($db_info)
  93    {
  94      if($this->_isFileCachingEnabled())
  95        lmbFs :: safeWrite($this->cache_file, serialize(new lmbSerializable($db_info)));
  96    }
  97  
  98    protected function _isFileCachingEnabled()
  99    {
 100      return ($this->cache_file &&
 101              (defined('LIMB_CACHE_DB_META_IN_FILE') && constant('LIMB_CACHE_DB_META_IN_FILE')));
 102    }
 103  }
 104  
 105  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7