Advertisement
kotvalera83

CodeIgniter Caching Class Subfolder

Feb 13th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.16 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package     CodeIgniter
  8.  * @author      ExpressionEngine Dev Team
  9.  * @copyright   Copyright (c) 2006 - 2012 EllisLab, Inc.
  10.  * @license     http://codeigniter.com/user_guide/license.html
  11.  * @link        http://codeigniter.com
  12.  * @since       Version 2.0
  13.  * @filesource 
  14.  */
  15.  
  16. // ------------------------------------------------------------------------
  17.  
  18. /**
  19.  * CodeIgniter Caching Class
  20.  *
  21.  * @package     CodeIgniter
  22.  * @subpackage  Libraries
  23.  * @category    Core
  24.  * @author      ExpressionEngine Dev Team
  25.  * @author      Valerii Kot
  26.  * $this->cache->model('model name', 'model method', 'model param', 'subfolder/chache file name', time);
  27.  * $this->cache->save('subfolder/chache file name', $data, time);
  28.  * support subfolder/subfolder/subfolder/subfolder/subfolder/ ...
  29.  * $this->cache->delete('subfolder/chache file name'); delete chache file
  30.  * $this->cache->clean(); delete all cache
  31.  * $this->cache->clean('subfolder'); delete all cache in subfolder 
  32.  * @link       
  33.  */
  34. class CI_Cache extends CI_Driver_Library {
  35.    
  36.     protected $valid_drivers    = array(
  37.         'cache_apc', 'cache_file', 'cache_memcached', 'cache_dummy'
  38.     );
  39.  
  40.     protected $_cache_path      = NULL;     // Path of cache files (if file-based cache)
  41.     protected $_adapter         = 'dummy';
  42.     protected $_backup_driver;
  43.    
  44.     // ------------------------------------------------------------------------
  45.  
  46.     /**
  47.      * Constructor
  48.      *
  49.      * @param array
  50.      */
  51.     public function __construct($config = array())
  52.     {
  53.         if ( ! empty($config))
  54.         {
  55.             $this->_initialize($config);
  56.         }
  57.     }
  58.  
  59.     // ------------------------------------------------------------------------
  60.  
  61.     /**
  62.      * Get
  63.      *
  64.      * Look for a value in the cache.  If it exists, return the data
  65.      * if not, return FALSE
  66.      *
  67.      * @param   string 
  68.      * @return  mixed       value that is stored/FALSE on failure
  69.      */
  70.     public function get($id)
  71.     {  
  72.         return $this->{$this->_adapter}->get($id);
  73.     }
  74.  
  75.     // ------------------------------------------------------------------------
  76.  
  77.     /**
  78.      * Cache Save
  79.      *
  80.      * @param   string      Unique Key
  81.      * @param   mixed       Data to store
  82.      * @param   int         Length of time (in seconds) to cache the data
  83.      *
  84.      * @return  boolean     true on success/false on failure
  85.      */
  86.     public function save($id, $data, $ttl = 60)
  87.     {
  88.         if($this->_adapter == 'cache_file'){
  89.             return $this->{$this->_adapter}->save($id, $data, $ttl);
  90.         }else{
  91.             $folder = explode('/', $id);
  92.             if (count($folder) > 1 ) {
  93.                 $subfolder = '';
  94.                 for ($i=0; $i < (count($folder) - 1); $i++) {
  95.                     $subfolder .= $folder[$i] . '/';
  96.                 }
  97.                 if (!is_dir('application/cache/'.$subfolder)) {
  98.                     mkdir('./application/cache/'.$subfolder, 0777, TRUE);
  99.                 }
  100.             }
  101.             return $this->{$this->_adapter}->save($id, $data, $ttl);   
  102.         }
  103.     }
  104.  
  105.     public function model($model, $method, $param = array(), $id, $ttl = 60)
  106.     {      
  107.         if ($this->get($id)) {
  108.             return $this->get($id);
  109.         } else {
  110.             $ci=& get_instance();
  111.             $ci->load->model($model);
  112.             $data = $ci->{$model}->{$method}($param);
  113.             if ($this->save($id, $data, $ttl)) {
  114.                 return $data;
  115.             } else {
  116.                 return FALSE;
  117.             }          
  118.         }
  119.     }
  120.  
  121.     // ------------------------------------------------------------------------
  122.  
  123.     /**
  124.      * Delete from Cache
  125.      *
  126.      * @param   mixed       unique identifier of the item in the cache
  127.      * @return  boolean     true on success/false on failure
  128.      */
  129.     public function delete($id)
  130.     {
  131.         return $this->{$this->_adapter}->delete($id);
  132.     }
  133.  
  134.     // ------------------------------------------------------------------------
  135.  
  136.     /**
  137.      * Clean the cache
  138.      *
  139.      * @return  boolean     false on failure/true on success
  140.      */
  141.     public function clean($folder = '')
  142.     {
  143.         if ($folder != '') {
  144.             $ci=& get_instance();
  145.             $ci->load->helper('file');
  146.             return delete_files('application/cache/'.$folder, TRUE);
  147.         }else{
  148.             return $this->{$this->_adapter}->clean();
  149.         }
  150.  
  151.     }
  152.  
  153.     // ------------------------------------------------------------------------
  154.  
  155.     /**
  156.      * Cache Info
  157.      *
  158.      * @param   string      user/filehits
  159.      * @return  mixed       array on success, false on failure 
  160.      */
  161.     public function cache_info($type = 'user')
  162.     {
  163.         return $this->{$this->_adapter}->cache_info($type);
  164.     }
  165.  
  166.     // ------------------------------------------------------------------------
  167.    
  168.     /**
  169.      * Get Cache Metadata
  170.      *
  171.      * @param   mixed       key to get cache metadata on
  172.      * @return  mixed       return value from child method
  173.      */
  174.     public function get_metadata($id)
  175.     {
  176.         return $this->{$this->_adapter}->get_metadata($id);
  177.     }
  178.    
  179.     // ------------------------------------------------------------------------
  180.  
  181.     /**
  182.      * Initialize
  183.      *
  184.      * Initialize class properties based on the configuration array.
  185.      *
  186.      * @param   array  
  187.      * @return  void
  188.      */
  189.     private function _initialize($config)
  190.     {        
  191.         $default_config = array(
  192.                 'adapter',
  193.                 'memcached'
  194.             );
  195.  
  196.         foreach ($default_config as $key)
  197.         {
  198.             if (isset($config[$key]))
  199.             {
  200.                 $param = '_'.$key;
  201.  
  202.                 $this->{$param} = $config[$key];
  203.             }
  204.         }
  205.  
  206.         if (isset($config['backup']))
  207.         {
  208.             if (in_array('cache_'.$config['backup'], $this->valid_drivers))
  209.             {
  210.                 $this->_backup_driver = $config['backup'];
  211.             }
  212.         }
  213.     }
  214.  
  215.     // ------------------------------------------------------------------------
  216.  
  217.     /**
  218.      * Is the requested driver supported in this environment?
  219.      *
  220.      * @param   string  The driver to test.
  221.      * @return  array
  222.      */
  223.     public function is_supported($driver)
  224.     {
  225.         static $support = array();
  226.  
  227.         if ( ! isset($support[$driver]))
  228.         {
  229.             $support[$driver] = $this->{$driver}->is_supported();
  230.         }
  231.  
  232.         return $support[$driver];
  233.     }
  234.  
  235.     // ------------------------------------------------------------------------
  236.  
  237.     /**
  238.      * __get()
  239.      *
  240.      * @param   child
  241.      * @return  object
  242.      */
  243.     public function __get($child)
  244.     {
  245.         $obj = parent::__get($child);
  246.  
  247.         if ( ! $this->is_supported($child))
  248.         {
  249.             $this->_adapter = $this->_backup_driver;
  250.         }
  251.  
  252.         return $obj;
  253.     }
  254.    
  255.     // ------------------------------------------------------------------------
  256. }
  257. // End Class
  258.  
  259. /* End of file Cache.php */
  260. /* Location: ./system/libraries/Cache/Cache.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement