キャッシュモデル

最近では、データキャッシングを整理するタスクになりました。 そして、それは非常に便利なことでした。 habrakatでの実装を参照してください...



以下に説明する抽象クラスは、モデルのCacheフォルダーに配置されます。 私の場合、これは/Default/Models/Cache/Abstract.phpです



abstract class Default_Models_Cache_Abstract { protected static $_cache; protected static $_staticCache; protected static $_tags = null; protected static $_className = null; protected static $_nonCachedMethods = array(); protected static $_frontendName; protected static $_backendName; protected static $_frontendOptions = array(); protected static $_backendOptions = array(); public function __construct() { static::$_cache = null; static::$_staticCache = null; static::_init(); } public function __call($name, $arguments) { return static::_call($name, $arguments); } public static function __callStatic($name, $arguments) { return static::_call($name, $arguments, true); } protected static function _call($name, $arguments, $useStatic = false) { static::_init($useStatic); $cache = $useStatic ? static::$_staticCache : static::$_cache; if (in_array($name, static::$_nonCachedMethods)) { return call_user_func_array(array($cache, $name), $arguments); } $id = $cache->makeId($name, $arguments); if (!($result = $cache->load($id))) { $result = $cache->__call($name, $arguments); $cache->save($result, $id, static::$_tags); } return $result; } protected static function _initConfig() { static $inited = null; if (null === $inited) { if (null === static::$_className) { throw new Exception("You must provide a <code>protected static" . " \$_className = ...;</code> statement in your class!"); } static::$_backendName = 'File'; static::$_frontendName = 'class'; static::$_frontendOptions = array(); static::$_backendOptions = array(); $inited = true; } } protected static function _init($useStatic = false) { $ref = null; if ($useStatic && null === static::$_staticCache) { $ref = static::$_className; } elseif (null === static::$_cache) { if (!class_exists(static::$_className)) { require_once 'Zend/Loader.php'; Zend_Loader::loadClass(static::$_className); } $ref = new static::$_className; } if (null !== $ref) { static::_initConfig(); static::$_tags = array(static::$_className); static::$_frontendOptions['cached_entity'] = $ref; if (!empty(static::$_nonCachedMethods)) { static::$_frontendOptions['non_cached_methods'] = is_array(static::$_nonCachedMethods) ? static::$_nonCachedMethods : array(static::$_nonCachedMethods); } $cache = Zend_Cache::factory( static::$_frontendName, static::$_backendName, static::$_frontendOptions, static::$_backendOptions ); if ($useStatic) { static::$_staticCache = $cache; } else { static::$_cache = $cache; } } } public function cleanCache() { $ids = static::$_cache->getIdsMatchingTags(); foreach($ids as $id) { static::$_cache->remove($id); } static::$_cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, static::$_tags); } }
      
      





作業するには、Default_Models_Productsなどの作業モデルが必要です。

モデルのファイルに関連するCacheフォルダーで、次のプロパティ$ _classNameおよび$ _nonCachedMethodsを使用してDefault_Models_Cache_Productsクラスを作成します。

 class Default_Models_Cache_Products extends Default_Models_Cache_Abstract { protected static $_className = 'Default_Models_Products'; protected static $_nonCachedMethods = array('addProduct'); }
      
      





$ _classNameで、キャッシュされたクラスの名前を指定します。 $ _nonCachedMethodsは必須プロパティではありません。キャッシュする必要のないメソッドをリストします。



Default_Models_Productsクラスへのすべての呼び出しをDefault_Models_Cache_Productsに置き換えます。Default_Models_Productsと同様にそれを使用します。

たとえば、このモデルはgetListメソッドを実装します。

 class Default_Models_Products { ... public function getList() { ... return $list; } ... }
      
      





次のようにデータを取得できます。

 $productsModel = new Default_Models_Cache_Products(); $productList = $productsModel->getList();
      
      





静的メソッドを呼び出す可能性も実装されています。 getListメソッドが静的な場合:

 class Default_Models_Products { ... public static function getList() { ... return $list; } ... }
      
      





次のように呼び出すことができます。

 $productList = Default_Models_Cache_Products::getList();
      
      





キャッシュを消去するには、cleanCache()メソッドが実装されます;呼び出されると、このモデルに関連するキャッシュがクリアされます。これは非常に便利です。



パラメーター$ _frontendOptionsおよび$ _backendOptionsの設定については、 こちらこちらをご覧ください。



結果のクラスは非常に用途が広く、モデルだけでなく適切です。



PS明確化。 ヒントをくれたVictor(ええ)のおかげで、このクラス 5.3より前のPHPバージョンでは動作ません 。 マジックメソッド__callStatic()は、このバージョンのphpで登場したためです。



All Articles