JCS的初步探究

公司的项目一直用的是jcs来管理缓存的,最近老板让我们研究一下jcs2.0的使用。小弟不才,研究了一下,虽只学到了一点皮毛,但还是写出来和大家分享一下吧,希望可以给有兴趣的读者提供一些帮助。
首先不会的东西肯定是要去问度娘啦,但搜了一下,发现有关jcs的资料真的很少,即使有的也是介绍1.3版本的。最后只能到JCS的官网去看了,虽然我的英文很差,但也只能硬着头皮看了。apache官网上有关jcs部分的网址是http://commons.apache.org/proper/commons-jcs/getting_started/intro.html。
网站将jcs分成5个部分来进行介绍:
第一步、理解jcs的核心概念。
第二步、下载jcs的jar包。
第三步、下载jcs所要依赖的一些jar包。
第四步、如何去配置和使用jcs。
第五步、通过程序去使用配置好的jcs。
这五个步骤中,第一步、第四步和第五步是重点。

DEFAULT CACHE REGION

jcs.default=DC
jcs.default.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=1000
jcs.default.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.cacheattributes.UseMemoryShrinker=false
jcs.default.cacheattributes.MaxMemoryIdleTime=3600
jcs.default.cacheattributes.ShrinkerInterval=60
jcs.default.elementattributes=org.apache.commons.jcs.engine.ElementAttributes
jcs.default.elementattributes.IsEternal=false
jcs.default.elementattributes.MaxLife=21600
jcs.default.elementattributes.IdleTime=1800
jcs.default.elementattributes.IsSpool=true
jcs.default.elementattributes.IsRemote=true
jcs.default.elementattributes.IsLateral=true

PRE-DEFINED CACHE REGIONS

jcs.region.testCache1=DC
jcs.region.testCache1.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes
jcs.region.testCache1.cacheattributes.MaxObjects=1000
jcs.region.testCache1.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.testCache1.cacheattributes.UseMemoryShrinker=false
jcs.region.testCache1.cacheattributes.MaxMemoryIdleTime=3600
jcs.region.testCache1.cacheattributes.ShrinkerInterval=60
jcs.region.testCache1.cacheattributes.MaxSpoolPerRun=500
jcs.region.testCache1.elementattributes=org.apache.commons.jcs.engine.ElementAttributes
jcs.region.testCache1.elementattributes.IsEternal=false

AVAILABLE AUXILIARY CACHES

jcs.auxiliary.DC=org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory
jcs.auxiliary.DC.attributes=org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes
jcs.auxiliary.DC.attributes.DiskPath=${user.dir}/jcs_swap
jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000000
jcs.auxiliary.DC.attributes.MaxKeySize=1000000
jcs.auxiliary.DC.attributes.MaxRecycleBinSize=5000
jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000
jcs.auxiliary.DC.attributes.ShutdownSpoolTimeLimit=60`

最后一步就是介绍如何在程序中去调用jcs啦。下面是原文给出的例子,程序很简单,就是获取jcs的实例,然后进行读取、存储和清楚。
import java.io.Serializable;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;

public class JcsExample 
{
    public static void main( String[] args ) 
    {
        JcsExample example = new JcsExample();
        example.testCache();
    }

    private CacheAccess<String, City> cache = null;

    public JcsExample() 
    {
        try 
        {
            cache = JCS.getInstance( "default" );
        }
        catch ( CacheException e ) 
        {
            System.out.println( String.format( "Problem initializing cache: %s", e.getMessage() ) );
        }
    }

    public void putInCache( City city ) 
    {
        String key = city.name;
        try 
        {
            cache.put( key, city );
        }
        catch ( CacheException e ) 
        {
            System.out.println( String.format( "Problem putting city %s in the cache, for key %s%n%s",
                    city.name, key, e.getMessage() ) );
        }
    }

    public City retrieveFromCache( String cityKey ) 
    {
        return cache.get( cityKey );
    }

    public void testCache() 
    {
        City zurich = new City( "Zürich", "Switzerland", 366765 );
        putInCache( zurich );
        
        City berlin = new City( "Berlin", "Germany", 3502000 );
        putInCache( berlin );
        
        City johannesburg = new City( "Johannesburg", "South Africa", 12200000 );
        putInCache( johannesburg );

        City retrievedCity1 = retrieveFromCache( "Berlin" );
        if ( retrievedCity1 != null ) 
        {
            System.out.println( retrievedCity1.toString() );
        }
        else 
        {
            System.out.println( "No object was found in the cache for the key \"Berlin\"" );
        }

        City retrievedCity2 = retrieveFromCache( "New York" );
        if ( retrievedCity2 != null ) 
        {
            System.out.println( retrievedCity2.toString() );
        }
        else 
        {
            System.out.println( "No object was found in the cache for the key \"New York\"" );
        }
    }

    // defined as a nested inner class to reduce number of .java files in the example
    public class City implements Serializable 
    {
        private static final long serialVersionUID = 6392376146163510146L;
        public String name;
        public String country;
        public int population;

        public City( String name, String country, int population ) 
        {
            this.name = name;
            this.country = country;
            this.population = population;
        }

        @Override
        public String toString() 
        {
            return String.format( "%s is a city in the country %s with a population of %d", name, country, population );
        }
    }
}

这里还有个地方需要注意的一点是,在配置tcp辅助缓存时,按照网址上的配置:
jcs.auxiliary.LTCP=org.apache.commons.jcs.auxiliary.lateral.LateralCacheFactory
jcs.auxiliary.LTCP.attributes=org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes
当启动时,会报错误,后来看了一下,发现jar里根本没有org.apache.commons.jcs.auxiliary.lateral.LateralCacheFactory这个类,后来改成一下配置,就不报错了。初步分析了一下,可能上面的配置是针对1.3版本的,2.0版本已经舍弃掉了原有的一些类。修改成以下配置就行了。
jcs.auxiliary.LTCP=org.apache.commons.jcs.auxiliary.lateral.socket.tcp.LateralTCPCacheFactory
jcs.auxiliary.LTCP.attributes=org.apache.commons.jcs.auxiliary.lateral.socket.tcp.TCPLateralCacheAttributes

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值