Zend Frameworkのキャッシュリソース

みなさんこんにちは。 この記事は、このフレームワークに既に精通している人を対象としています。 Zend Frameworkの新しいバージョンでは、リソースプラグインを介してアプリケーションコンポーネントを初期化する概念が考案されました。 標準の配信には十分な数がありますが、見つかりませんでした。 それがキャッシュです。 インターネットをさまよってアナログを見つけなかったので、自分で書くことにしました。 それでは、始めましょう。







ライブラリ/アプリ/アプリケーション/リソースフォルダーにカスタムリソースを保存します。 ソースコードを接続したCache.phpファイルを配置します。



<?php



class App_Application_Resource_Cache extends Zend_Application_Resource_ResourceAbstract

{

/**

* Default registry key

*/

const DEFAULT_REGISTRY_KEY = 'App_Cache' ;



/**

* Cache instance

*

* @var Zend_Cache

*/

protected $_cache = null ;



/**

* Inititalize cache resource

*

* @return Zend_Cache

*/

public function init ()

{

return $ this ->getCache();

}



/**

* Return cache instance

*

* @return Zend_Cache

*/

public function getCache ()

{

if ( null === $ this ->_cache) {

$options = $ this ->getOptions();



/// create cache instance

$ this ->_cache = Zend_Cache::factory(

$options[ 'frontend' ][ 'adapter' ],

$options[ 'backend' ][ 'adapter' ],

$options[ 'frontend' ][ 'params' ],

$options[ 'backend' ][ 'params' ]

);



/// use as default database metadata cache

if (isset($options[ 'isDefaultMetadataCache' ]) && true === ( bool ) $options[ 'isDefaultMetadataCache' ]) {

Zend_Db_Table_Abstract::setDefaultMetadataCache($ this ->_cache);

}



/// use as default translate cache

if (isset($options[ 'isDefaultTranslateCache' ]) && true === ( bool ) $options[ 'isDefaultTranslateCache' ]) {

Zend_Translate::setCache($ this ->_cache);

}



/// use as default locale cache

if (isset($options[ 'isDefaultLocaleCache' ]) && true === ( bool ) $options[ 'isDefaultLocaleCache' ]) {

Zend_Locale::setCache($ this ->_cache);

}



/// add to registry

$key = (isset($options[ 'registry_key' ]) && !is_numeric($options[ 'registry_key' ])) ? $options[ 'registry_key' ] : self::DEFAULT_REGISTRY_KEY;

Zend_Registry:: set ($key, $ this ->_cache);

}

return $ this ->_cache;

}

}




* This source code was highlighted with Source Code Highlighter .








これで、アプリケーション構成を使用してキャッシュを初期化できます。



#キャッシュ

resources.cache.frontend.adapter = core

resources.cache.frontend.params.lifetime = 7200

resources.cache.frontend.params.automatic_serialization = true

resources.cache.backend.adapter =ファイル

resources.cache.backend.params.lifetime = 7200

resources.cache.backend.params.cache_dir = APPLICATION_PATH "/../trash/cache"

resources.cache.isDefaultMetadataCache = true

resources.cache.isDefaultTranslateCache = true

resources.cache.isDefaultLocaleCache = true

resources.cache.registry_key =キャッシュ



また、カスタムリソースが機能するように、次の行を構成に追加する必要があります。



#カスタムリソース

pluginPaths.App_Application_Resource_ =アプリ/アプリケーション/リソース



良い一日を過ごしてください。ここまで読んでいただければ、私はあなたに興味を持っていただければ幸いです=)



All Articles