私はかつてCIキャッシングメカニズムの一部を書き直しました。多分これはあなたの助けになるかもしれません。これは「キャッシュ」すべての関数です。システムファイルのオーバーライドとして作成しました。
その中に使用例があります。とてもシンプルなはずです。このコードを使用すると、セッション/リクエスト間で共有することもでき、関数の結果をキャッシュできます。
http://codeigniter.com/forums/viewthread/221313/
またはこれ:
https://github.com/EllisLab/CodeIgniter/issues/1646
この新機能が必要ない場合は、標準のCIキャッシングメカニズムの使用方法の例として使用できます。
このような:
class your_class extends CI_Model
{
// ------------------------------------------------------------------------
function __construct( )
{
$cache_adapter = 'apc';
$this->load->driver( 'cache', array( 'adapter' => $cache_adapter, 'backup' => 'dummy' ) );
$this->cache->{$cache_adapter}->is_supported( );
}
// ------------------------------------------------------------------------
public function your_function( $arg )
{
$result = $this->cache->get( __CLASS__ . __METHOD__ . serialize( $arg ) );
if ( empty( $result ) )
{
$result = ... /* your calculation here */
$this->cache->save( __CLASS__ . __METHOD__ . serialize( $arg ) );
}
return $result;
}
}
キャッシュに使用するキーは、いわゆるマングル関数名です。関数の結果が(そうあるべきであるように)その引数のみに依存している場合は、そのまま使用できます。キーをコンパクトにするために、ハッシュすることができます。このような:
public function your_function( $arg )
{
$result = $this->cache->get( md5( __CLASS__ . __METHOD__ . serialize( $arg ) ) );
if ( empty( $result ) )
{
$result = ... /* your calculation here */
$this->cache->save( md5( __CLASS__ . __METHOD__ . serialize( $arg ) ) );
}
return $result;
}