Using Zend_Cache in CodeIgniter (Sample)
Sample Code (may be buggy)
Time line (about this code):
http://twitter.com/mataga/status/17738228243
http://twitter.com/mataga/status/17738331964
http://twitter.com/mataga/status/17738497243
http://twitter.com/mataga/status/17738815125
http://twitter.com/mataga/status/17739119936
<?php // Using Zend_Cache in CodeIgniter ( Sample ) // Version : 0.0.0.1 // see: http://framework.zend.com/manual/en/zend.cache.introduction.html // see: http://framework.zend.com/manual/ja/zend.cache.introduction.html class Lib_cache { private $_lifetime = 7200; //cache lifetime 2hours private $_automatic_serialization = true; //serialize data private $_cache_dir_name = 'data/cache/zendcache'; //cache dir (mkdir application/data/cache/zendcache) private $_default_tag_name_list = 'SYSTEM_CORE'; //Default Tag name for clean cache private $CI; private $_cache_use = false; private $_cache; private $_last_status = NULL; public function __construct( $params = array() ) { $this -> CI = &get_instance(); if ( $this -> CI -> config -> item( 'lib_cache_use' ) ) { # Put $config['lib_cache_use'] = true; on ./application/config.php // ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.BASEPATH.'application/my_classes/');//Change to the appropriate path require_once( 'Zend/Cache.php' ); $frontendOptions = ( isset( $params['frontendOptions'] ) )?$params['frontendOptions'] : array( 'lifetime' => $this -> _lifetime, 'automatic_serialization' => $this -> _automatic_serialization ); $backendOptions = ( isset( $params['backendOptions'] ) )?$params['backendOptions'] : array( 'cache_dir' => APPPATH . '/' . $this -> _cache_dir_name ); //application/data/cache/zendcache $this -> _cache = Zend_Cache :: factory( 'Core', 'File', $frontendOptions, $backendOptions ); $this -> _cache_use = true; } } // Get cache(load cache) public function get( $cache_id ) { return $this -> _cache_use ? $this -> _cache -> load( $cache_id ) : false ; } // Remove cache public function remove( $cache_id ) { if ( $this -> _cache_use ) { $this -> _last_status = $this -> _cache -> remove( $cache_id ) ; } } // Clean cache public function clean( $tag_name_list = NULL ) { if ( $this -> _cache_use ) { if ( is_null( $tag_name_list ) ) { $tag_name_list = $this -> _default_tag_name_list; } $tag_arr = explode( ',', $tag_name_list ); $this -> _last_status = $this -> _cache -> clean( Zend_Cache :: CLEANING_MODE_MATCHING_TAG , $tag_arr ); } } // Save cache public function save( $result, $cache_id , $tag_name_list = NULL ) { if ( $this -> _cache_use ) { if ( is_null( $tag_name_list ) ) { $tag_name_list = $this -> _default_tag_name_list; } $tag_arr = explode( ',', $tag_name_list ); $this -> _last_status = $this -> _cache -> save( $result, $cache_id , $tag_arr ); } } // Get last status public function get_last_status() { return $this -> _last_status; } }//endofclass /** #Example1 : Caching query result - in Some Class protected function load_table( $table_name, $fields = array() ) { $this -> load -> library( 'lib_cache' ); # Loading Lib_cache.php $this -> _set_table_name( $table_name ) ; if ( empty( $fields ) ) { $cache_id = 'base_model__load_table_' . md5( $table_name ); # Setting cache id if ( !$result = $this -> lib_cache -> get( $cache_id ) ) { # Getting cache data $result = $this -> db -> list_fields( $table_name ); $this -> lib_cache -> save( $result, $cache_id ); #Saving cache data } $this -> fields = $result; }else { $this -> fields = $fields ; } } */ /** * End of file Lib_cache.php */ /** * Location: ./application/libraries/Lib_cache.php */
(つぶやいてたら興味を持たれた方がいたので、取り急ぎアップ。作成時間1時間ちょっとなのでバグがあるかも。実験レベルです。画面ダンプ付きの詳しい説明は後日するかも)