Global Cache Service

グローバルキャッシュサービス

The Global Cache Service provides an object cache that you can call from anywhere in your application to temporarily store objects. An example of its use may be to store the names of Countries that you use throughout your application to populate a look up list. If you normally store the information about Countries in a database, you can increase the performance of your application by using the Cache and decreasing the number of hits against the database.

グローバルキャッシュサービスは、アプリケーションのどこからでも呼び出せる、一時的にオブジェクトを格納するためのオブジェクトキャッシュを提供します。 使用例としては、国々の名前を格納するような、アプリケーションの全体から使用される索引リストを入れておくようなことです。 データベースに国々の情報を格納している場合、キャッシュを使用することでデータベースへのアクセス数を減少させ、パフォーマンスを向上させることが可能です。

Configuration

設定

# -------------------------------------------------------------------
#
#  S E R V I C E S
#
# -------------------------------------------------------------------

# Classes for Turbine Services should be defined here.
# Format: services.[name].classname=[implementing class]
#
# To specify properties of a service use the following syntax:
# service.[name].[property]=[value]

# Turbineのサービスのためのクラス群はここに定義します。
# フォーマット: services.[name].classname=[implementing class]
#
# サービスのプロパティを設定するには、以下の書式を使用します:
# service.[name].[property]=[value]

services.GlobalCacheService.classname=org.apache.fulcrum.cache.TurbineGlobalCacheService.
.
.

# -------------------------------------------------------------------
#
#  C A C H E   S E R V I C E
#
# -------------------------------------------------------------------


# Interval at which the cache will be checked. The default is
# 5000ms or 5 seconds.

# キャッシュがチェックされるインターバル。
# デフォルトは5000ミリ秒、つまり5秒です。

services.GlobalCacheService.cache.check.frequency = 5000

Usage

使用方法


GlobalCacheService gs = null;
try
{
    /*

     * Look for the item in the cache.
     * If it doesn't exist or the item is stale,
     * the cache will throw an exception.

     * キャッシュ中のアイテムを参照します。
     * 無かった場合やアイテムが期限切れとなっていた場合、
     * キャッシュから例外が送出されます。
     */
    gs = (GlobalCacheService)TurbineServices.getInstance()
        .getService(GlobalCacheService.SERVICE_NAME);

    CachedObject obj = gs.getObject("cached_object");

    data.setMessage( data.getScreen() + " Got " +
        obj.getContents().toString() + " from global cache!" );
}
catch(ObjectExpiredException gone)
{
    /*

     * Add the item to the cache.

     * キャッシュにアイテムを追加します。
     */
    gs.addObject("cached_object",
        new CachedObject("in_the_cache",5000));

    data.setMessage( data.getScreen() +
        " Refreshed/or added new item to" +
        " the cache! Expires in 5 seconds" );
}

You can also place an expiration time on your objects so the Service will automatically remove them when they expire. If you don't specify an expiration time, the Service uses the default time set by the property cache.check.frequency in the Fulcrum.properties file. To see an example, look at the file TestGlobalCache.java located in the actions folder.

期限切れとなった時に、サービスが自動的にそれらを削除するように、オブジェクトに有効期限を設定することができます。 有効期限を指定しなければ、サービスはFulcrum.propertiesファイルのcache.check.frequencyプロパティによって設定される、デフォルトの有効期限を使用します。 例を見るには、actionsフォルダのファイル、TestGlobalCache.javaを参照してください。