キャッシュ機能の強化

ColdFusion では、デフォルトのキャッシュエンジンである Ehcache に加え、次のキャッシュエンジンがサポートされています。

Administrator の設定の変更

ColdFusion Administrator では、サーバーの設定/キャッシュ機能をクリックしてキャッシュエンジンを選択できます。

キャッシュエンジンの選択
キャッシュエンジンの選択

選択するオプションに応じて、一部の設定を変更する必要がありま。

JCS

Java Caching System(JCS)は Apache Jakarta サブプロジェクトを通じてリリースされているオープンソースのキャッシュエンジンです。JCS にはメモリ内キャッシュ機能と、キャッシュ内のオブジェクトを選択的に削除するためのアルゴリズムがあります。また 、 インデックスディスクキャッシュや、分散キャッシュのサポートなど、高度な機能も備えています。

JCS を使用するには、ColdFusion Administrator で上の図に示すオプションを選択します。

JCS はアプリケーションレベルで設定することもできます。キャッシュエンジンとキャッシュプロパティファイルを設定するには、新しいアプリケーション変数を使用します。

Application.cfc

component{
this.name = "appSpecificCacheTest";
this.cache.configfile = "jcsconfig.properties";
this.cache.engine = 'jcs';
this.applicationTimeout = createtimespan(0,0,0,5);
}
component{ this.name = "appSpecificCacheTest"; this.cache.configfile = "jcsconfig.properties"; this.cache.engine = 'jcs'; this.applicationTimeout = createtimespan(0,0,0,5); }
component{
  this.name = "appSpecificCacheTest";
  this.cache.configfile = "jcsconfig.properties";
      this.cache.engine = 'jcs';
  this.applicationTimeout = createtimespan(0,0,0,5); 
}

Application.cfc で定義できる 変数 について詳しくは 、 Application.cfc 変数を参照してください。

jcsconfig.properties

maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5
maxElementsInMemory=5 eternal=false timeToIdleSeconds=30 timeToLiveSeconds=5
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5

下の図に示すように、これらの設定は Administrator(サーバーの設定/キャッシュ機能)でも変更できます。

JCS プロパティ
JCS プロパティ

設定を確認するには、次のような cfm を作成します。

<cfscript>
writedump(cacheGetEngineProperties()); // Returns the cache engine properties
writeoutput(cacheGetEngineProperties().name); // Returns the name of the cache engine
</cfscript>
<cfscript> writedump(cacheGetEngineProperties()); // Returns the cache engine properties writeoutput(cacheGetEngineProperties().name); // Returns the name of the cache engine </cfscript>
<cfscript>
 writedump(cacheGetEngineProperties()); // Returns the cache engine properties
 writeoutput(cacheGetEngineProperties().name); // Returns the name of the cache engine
</cfscript>

JCS での補助キャッシュのサポート

JCS を使用するとデータベース内にキャッシュを常駐させて、複数のノードを経由してキャッシュにアクセスできるようになります。ColdFusion Administrator で、JCS のクラスタリングに使用するデータソースを追加します。

ColdFusion Administrator で、サーバーの設定/キャッシュ機能をクリックします。ドロップダウンからデータソースを選択します。

JCS 補助キャッシュの設定
JCS 補助キャッシュの設定

変更を保存すると、選択したデータソースに JCS_STORE というテーブルが作成されます。

この例では、My SQL を補助キャッシュとして使用しています。

JCS で補助キャッシュをサポートするには、<coldfusion_install_dir>/cfusion/lib にある cache.ccf ファイルを編集します。次の行を追加します。

# MYSQL disk cache used for flight options
jcs.auxiliary.MYSQL=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheFactory
jcs.auxiliary.MYSQL.attributes=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes
jcs.auxiliary.MYSQL.attributes.userName=<user name>
jcs.auxiliary.MYSQL.attributes.password=<password>
# Make sure the datasource is the one in which JCS_STORE is created
# via the Administrator
jcs.auxiliary.MYSQL.attributes.url=jdbc:mysql://localhost:3306/<datasource>
jcs.auxiliary.MYSQL.attributes.driverClassName=com.mysql.jdbc.Driver
jcs.auxiliary.MYSQL.attributes.tableName=JCS_STORE
jcs.auxiliary.MYSQL.attributes.UseDiskShrinker=false
# MYSQL disk cache used for flight options jcs.auxiliary.MYSQL=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheFactory jcs.auxiliary.MYSQL.attributes=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes jcs.auxiliary.MYSQL.attributes.userName=<user name> jcs.auxiliary.MYSQL.attributes.password=<password> # Make sure the datasource is the one in which JCS_STORE is created # via the Administrator jcs.auxiliary.MYSQL.attributes.url=jdbc:mysql://localhost:3306/<datasource> jcs.auxiliary.MYSQL.attributes.driverClassName=com.mysql.jdbc.Driver jcs.auxiliary.MYSQL.attributes.tableName=JCS_STORE jcs.auxiliary.MYSQL.attributes.UseDiskShrinker=false
# MYSQL disk cache used for flight options
jcs.auxiliary.MYSQL=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheFactory
jcs.auxiliary.MYSQL.attributes=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes
jcs.auxiliary.MYSQL.attributes.userName=<user name>
jcs.auxiliary.MYSQL.attributes.password=<password>
# Make sure the datasource is the one in which JCS_STORE is created 
# via the Administrator
jcs.auxiliary.MYSQL.attributes.url=jdbc:mysql://localhost:3306/<datasource>
jcs.auxiliary.MYSQL.attributes.driverClassName=com.mysql.jdbc.Driver
jcs.auxiliary.MYSQL.attributes.tableName=JCS_STORE
jcs.auxiliary.MYSQL.attributes.UseDiskShrinker=false

また、cache.ccf で jcs.default = MYYSQL を設定します。

ColdFusion を再起動します。

JDBC ディスクキャッシュでは MySQL などのリレーショナルデータベースが永続データストアとして使用されます。キャッシュ要素はシリアル化されて BLOB に書き込まれます。

Memcached

Memcached は Java エンタープライズアプリケーション向けの分散キャッシュソリューションです。1 つの Memcached サーバーを複数の ColdFusion インスタンス間で共有することもできますが、1 つの ColdFusion インスタンスで複数の Memcached サーバーを設定することもできます。

Memcached を使用するには、Memcached をダウンロードし、次のコマンドを使用してコマンドラインからサーバーを起動します。

memcached

Memcached サーバーは デフォルトでポート 11211 を使用します。

ColdFusion Administrator でサーバーの設定/キャッシュ機能をクリックし、Memcached サーバーの詳細を指定します。

Memcached サーバー
Memcached サーバー

Memcached はアプリケーションレベルで設定することもできます。キャッシュエンジンを宣言するために使用する必要がある新しいアプリケーション変数は次の 2 つです。

  • this.cache.engine使用するキャッシュエンジンを指定します。選択できるオプションは jcs または memcached または ehcache です。
  • this.cache.configFile memcached の設定ファイルを指定します。例えば、このファイルには、次のようなキーと値のペアを含めることができます。
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5
maxElementsInMemory=5 eternal=false timeToIdleSeconds=30 timeToLiveSeconds=5
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5

下の図に示すように、これらの設定は Administrator(サーバーの設定/キャッシュ機能)でも変更できます。インターフェイスは他のキャッシュエンジンとほぼ同じです。

Memcached の設定
Memcached の設定

// Application.cfc
component{
this.name='appUsingMemcached';
this.cache.engine='memcached';
this.cache.configFile='memcachedconfig.properties';
this.cache.applicationTimeout=createtimespan(0,0,0,5);
}
// Application.cfc component{ this.name='appUsingMemcached'; this.cache.engine='memcached'; this.cache.configFile='memcachedconfig.properties'; this.cache.applicationTimeout=createtimespan(0,0,0,5); }
// Application.cfc

component{
 this.name='appUsingMemcached';
 this.cache.engine='memcached';
 this.cache.configFile='memcachedconfig.properties';
 this.cache.applicationTimeout=createtimespan(0,0,0,5);
}
// memcachedconfig.properties
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=10
// memcachedconfig.properties maxElementsInMemory=5 eternal=false timeToIdleSeconds=30 timeToLiveSeconds=10
// memcachedconfig.properties

maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=10

Redis

Redis はデータベースおよびキャッシュとして使用されるオープンソース(BSD ベース)のメモリ内データ構造ストアです。文字列、ハッシュ、リスト、セットなどのデータ構造体がサポートされています。

Redis を使用するには、Redis をダウンロードし、次のコマンドを使用してコマンドラインからサーバーを起動します。

redis-server

Redis サーバーはデフォルトでポート 6379 を使用します。

Redis は分散型のソリューションです。クラスター環境では、すべてのノードが同じ Redis ノードと通信できます。

ColdFusion Administrator でサーバーの設定/キャッシュ機能をクリックし、Redis サーバーの詳細を指定します。

Redis の設定
Redis の設定

Redis はアプリケーションレベルで設定することもできます。キャッシュエンジンを宣言するために使用する必要がある新しいアプリケーション変数は次の 2 つです。

  • this.cache.engine:使用するキャッシュエンジンを指定します。選択できるオプションは jcs、redis、memcached または ehcache です。
  • this.cache.configFile:Redis の設定ファイルを指定します。例えば、このファイルには、次のようなキーと値のペアを含めることができます。
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5
maxElementsInMemory=5 eternal=false timeToIdleSeconds=30 timeToLiveSeconds=5
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5
<!--- Application.cfc --->
<cfcomponent>
<cfscript>
this.name = "appSpecificCacheTest";
this.cache.configfile = "redisconfig.properties";
this.cache.engine = "redis";
this.applicationTimeout = createtimespan(0,0,0,5);
</cfscript>
</cfcomponent>
<!--- Application.cfc ---> <cfcomponent> <cfscript> this.name = "appSpecificCacheTest"; this.cache.configfile = "redisconfig.properties"; this.cache.engine = "redis"; this.applicationTimeout = createtimespan(0,0,0,5); </cfscript> </cfcomponent>
<!--- Application.cfc --->

<cfcomponent>
    <cfscript>
        this.name = "appSpecificCacheTest";
        this.cache.configfile = "redisconfig.properties";
        this.cache.engine = "redis";
        this.applicationTimeout = createtimespan(0,0,0,5);
    </cfscript>
</cfcomponent>
// redisconfig.properties
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5
// redisconfig.properties maxElementsInMemory=5 eternal=false timeToIdleSeconds=30 timeToLiveSeconds=5
// redisconfig.properties

maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5

カスタムキャッシュプラグイン

Memcached、Redis、JCS を使用する以外に、カスタムキャッシュプラグインを実装することもできます。カスタムキャッシュエンジンを実装するためのインターフェイス(C:\ColdFusion2021\cfusion\wwwroot\CFIDE\cache\ICustomCache.cfc)も用意されています。

実装は ICustomCache.cfc と同じフォルダーに配置する必要があります。

ColdFusion は Infinispan ライブラリを利用します。安定版リリースから次のファイルをダウンロードします。

  • infinispan -embedded-query-9.1.3.Final.jar
  • infinispan -embedded-9.1.3.Final.jar

これらのファイルを C:\ColdFusion2021\cfusion\lib にコピーして ColdFusion を再起動します。

カスタムプラグインを追加するには:

  1. C:\ColdFusion2021\cfusion\wwwroot\CFIDE\cache にフォルダーを作成します。このフォルダーの名前はカスタムプラグインと同じ名前にする必要があります。

  2. ICustomCache.cfc を実装する <any CFC name>.cfc を作成し、実装を記述します。次に例を示します。

    <cfcomponent implements="ICustomCache">
    <cffunction name="put" >
    <cfargument name="obj" type="struct">
    <cfoutput>"inside put"</cfoutput>
    <cfset defaultCache=Application.defaultCacheManager.getcache()>
    <cfset defaultCache.put(obj.id,#obj.value#)>
    </cffunction>
    </cfcomponent>
    <cfcomponent implements="ICustomCache"> <cffunction name="put" > <cfargument name="obj" type="struct"> <cfoutput>"inside put"</cfoutput> <cfset defaultCache=Application.defaultCacheManager.getcache()> <cfset defaultCache.put(obj.id,#obj.value#)> </cffunction> </cfcomponent>
    <cfcomponent implements="ICustomCache">
        <cffunction name="put" >
            <cfargument name="obj" type="struct">
                <cfoutput>"inside put"</cfoutput>
                <cfset defaultCache=Application.defaultCacheManager.getcache()>
                <cfset defaultCache.put(obj.id,#obj.value#)>
        </cffunction>
    </cfcomponent>
  3. Infinispan ライブラリへの参照を含む config.xml を 作成します。

    <infinispan>
    <cache-container default-cache="local">
    <local-cache name="local"/>
    </cache-container>
    </infinispan>
    <infinispan> <cache-container default-cache="local"> <local-cache name="local"/> </cache-container> </infinispan>
    <infinispan>
        <cache-container default-cache="local">
            <local-cache name="local"/>
        </cache-container>
    </infinispan>
  4. Application.cfc を作成します。

    <cfcomponent>
    <cfset this.name= "xyz">
    <cfscript>
    function onApplicationStart()
    {
    writelog("In onApplicationStart()");
    Application.defaultCacheManager=CreateObject("java","org.infinispan.manager.DefaultCacheManager").init('C:\ColdFusion2021\cfusion\wwwroot\custom_cache\config.xml');
    writelog("In onApplicationStart()");
    }
    function onApplicationEnd() {
    writelog("In onApplicationEnd()");
    }
    </cfscript>
    </cfcomponent>
    <cfcomponent> <cfset this.name= "xyz"> <cfscript> function onApplicationStart() { writelog("In onApplicationStart()"); Application.defaultCacheManager=CreateObject("java","org.infinispan.manager.DefaultCacheManager").init('C:\ColdFusion2021\cfusion\wwwroot\custom_cache\config.xml'); writelog("In onApplicationStart()"); } function onApplicationEnd() { writelog("In onApplicationEnd()"); } </cfscript> </cfcomponent>
    <cfcomponent> 
        <cfset this.name= "xyz"> 
            <cfscript> 
                function onApplicationStart() 
                { 
                    writelog("In onApplicationStart()"); 
                    Application.defaultCacheManager=CreateObject("java","org.infinispan.manager.DefaultCacheManager").init('C:\ColdFusion2021\cfusion\wwwroot\custom_cache\config.xml'); 
                    writelog("In onApplicationStart()"); 
    } 
                    function onApplicationEnd() { 
                    writelog("In onApplicationEnd()"); 
    } 
            </cfscript> 
    </cfcomponent>

次のコードを使用してアプリケーションを作成します。

// Application.cfc
<cfcomponent>
<cfscript>
this.name = "mycache_app";
this.cache.engine = "mycache";
</cfscript>
</cfcomponent>
// Application.cfc <cfcomponent> <cfscript> this.name = "mycache_app"; this.cache.engine = "mycache"; </cfscript> </cfcomponent>
// Application.cfc

<cfcomponent>
    <cfscript>
        this.name = "mycache_app";
        this.cache.engine = "mycache";
    </cfscript>
</cfcomponent>

Admin API

ColdFusion 2018 リリースのキャッシュ機能に関連する Admin API は 次の とおりです。これらの API は runtime.cfc に含まれています。

verifyRedisCacheStorageConnection

説明:Redis キャッシュストレージへの接続を確認します。

シンタックス

void verifyRedisCacheStorageConnection (sessionStorageHost, numeric sessionStoragePort, sessionStoragePassword)
void verifyRedisCacheStorageConnection (sessionStorageHost, numeric sessionStoragePort, sessionStoragePassword)
void verifyRedisCacheStorageConnection (sessionStorageHost, numeric sessionStoragePort, sessionStoragePassword)

パラメーター

必須/オプション

デフォルト

説明

sessionStorageHost

オプション

Any

Redis キャッシュストレージのホスト名。

sessionStoragePort

オプション

Numeric

Redis キャッシュストレージのポート番号。

sessionStoragePassword

オプション

Any

Redis キャッシュストレージのパスワード。

setServerCachingEngine

説明:サーバーレベルでキャッシュエンジンを変更します。

シンタックス

void setServerCachingEngine (required engine)
void setServerCachingEngine (required engine)
void setServerCachingEngine (required engine)

パラメーター

必須/オプション

デフォルト

説明

engine

必須

Any

1.       Ehcache

2.      JCS

3.      Memcached

4.      Redis

setJCSClusterDsnName

説明:JCS クラスターのデータソースを設定します。

シンタックス

void setJCSClusterDsnName (required dsn, required boolean createTables)
void setJCSClusterDsnName (required dsn, required boolean createTables)
void setJCSClusterDsnName (required dsn, required boolean createTables)

パラメーター

必須/オプション

デフォルト

説明

dsn

必須

Any

データソースの名前。

createTables

必須

Any

テーブルを作成するかどうか。

setCachingRedisServer

説明:Redis のキャッシュエンジンを設定します。

シンタックス

void setCachingRedisServer (required host, required port, required password, required boolean cluster)
void setCachingRedisServer (required host, required port, required password, required boolean cluster)
void setCachingRedisServer (required host, required port, required password, required boolean cluster)

パラメーター

必須/オプション

デフォルト

説明

host

必須

any

サーバーのホストアドレス。

port

必須

any

サーバーのポート番号。

password

必須

any

サーバーのパスワード。

cluster

必須

Boolean

Redis でクラスターを有効にするかどうか。

getMemcachedServer

説明:Memcached キャッシュエンジンの詳細を取得します。

シンタックス

any getMemcachedServer ()
any getMemcachedServer ()
any getMemcachedServer ()

ColdFusion のキャッシュ機能について詳しくは、ColdFusion アプリケーションの最適化を参照してください。

Azure Redis Cache

Azure Cache for Redis は、Redis に基づいたメモリ内データストアを提供します。頻繁にアクセスされるデータを高性能ストレージにコピーすることによって、キャッシュパフォーマンスが強化されます。Azure Cache for Redis を使用すると、データベースによってディスクから読み込まれる代わりに、このストレージがメモリ内に配置されます。

Azure Cache for Redis は、次のように使用できます。

  • 分散データキャッシュ
  • セッションストア
  • メッセージブローカー

Azure Cache for Redis は、Azure でホストされ、Azure 内外の任意のアプリケーションからアクセスできます。詳しくは、Azure Cache for Redis を参照してください。

Azure Portal で、Redis Cache を作成します。

キャッシュの詳細ページで、次の情報を入力します。

  • プライマリキー
  • セカンダリキー
  • プライマリ接続文字列
  • セカンダリ接続文字列

これらの接続の詳細を ColdFusion Administrator のキャッシュページで使用します。

接続を確認」をクリックすると、キャッシュエンジンが Azure クラウドに保存されます。

AWS ElastiCache

Amazon ElastiCache は、Amazon Web Services の Caching-as-a-Service(サービスとしてのキャッシュ)です。AWS は、クラウドの分散メモリ内キャッシュ環境を管理およびスケーリングします。AWS は、分散キャッシュ環境のデプロイおよび管理に関する複雑さを取り除きます。

詳しくは、AWS ElastiCache を参照してください。

AWS ElastiCache は、次の機能を提供します。

  • キャッシュノードエラーの自動検出および回復。
  • 失敗したプライマリクラスターの自動フェイルオーバー
  • ノードおよびクラスターの柔軟なアベイラビリティーゾーンの配置

Amazon ElastiCache には、Memcached と Redis の 2 つのキャッシュエンジンが用意されています。既存の Memcached または Redis キャッシュ実装を Amazon ElastiCache に容易に移行できます。アプリケーションの Memcached/Redis エンドポイントを変更するだけです。

AWS では、EC2 インスタンスへの SSL トンネルを作成する必要があります。すると、localhost が AWS のキャッシュエンジンポートにリダイレクトされます。

AWS コンソールで、Redis および Memcached 用にキャッシュを作成してから、ColdFusion Administrator のキャッシュの詳細ページに詳細を入力します。

ヘルプをすばやく簡単に入手

新規ユーザーの場合