EHCachae中的CacheManager类

项目描述:

为提高传统KNN算法在海量数据的文本分类效率,提出了基于K-Medoids聚类的改进KNN算法,实验数据来自复旦大学的语料集,实验结果表明提出的改进算法确实提高了KNN算法的运行效率和分类能力。

开发平台:以Eclipse为集成开发环境,Windows7 64位操作系统、CPU为AMD A8-5600K、内存为8G

主要工作:用Java实现了KNN算法和基于K-Medoids聚类的KNN算法,并得出比较结果。

 

EHCache

关键类

类CacheManager管理着Caches。Caches包含有elements,这些elements都是键值对。Caches包括内存缓存和磁盘缓存。

CacheManager

CacheManager有着包含Elements的Caches。

CacheManager管理着创建、移除、使用缓存

CacheManager有两种实例方法:单例和普通的实例。

 

单例模式 ehcache-1.1只支持一个缓存管理器实例(就是单例)。CacheManager通过使用静态工厂模式来达到目的。

这是它的实现方式:

public static CacheManager create()throws CacheException {

        synchronized (CacheManager.class) {

            if (singleton == null) {

                if (LOG.isDebugEnabled()) {

                    LOG.debug("Creatingnew CacheManager with default config");

                }

                singleton = new CacheManager();

            } else {

               if(LOG.isDebugEnabled()) {

                    LOG.debug("Attemptingto create an existing singleton. Existing singleton returned.");

                }

            }

            return singleton;

        }

   }

普通的实例模式:是从ehcache-1.2开始的CacheManager有各种构造方法,方法。这允许多个Cachemanagers被创建并同时使用。每个缓存管理器都有自己的配置。即使没有给它指定参数,也有默认的参数。

public CacheManager(Configuration configuration)throws CacheException {

        status = Status.STATUS_UNINITIALISED;

        init(configuration, null, null, null);

}

 

publicCacheManager(String configurationFileName) throws CacheException {

        status = Status.STATUS_UNINITIALISED;

        init(null, configurationFileName, null,null);

    }

publicCacheManager(URL configurationURL) throws CacheException {

        status = Status.STATUS_UNINITIALISED;

        init(null, null, configurationURL,null);

    }

publicCacheManager(URL configurationURL) throws CacheException {

        status = Status.STATUS_UNINITIALISED;

        init(null, null, configurationURL,null);

    }

publicCacheManager(InputStream configurationInputStream) throws CacheException {

        status = Status.STATUS_UNINITIALISED;

        init(null, null, null,configurationInputStream);

    }

publicCacheManager() throws CacheException {

        //default config will be done

        status = Status.STATUS_UNINITIALISED;

        init(null, null, null, null);

    }

privatevoid init(Configuration configuration, String configurationFileName, URLconfigurationURL,

                      InputStreamconfigurationInputStream) {

        Configuration localConfiguration =configuration;

        if (configuration == null) {

            localConfiguration =parseConfiguration(configurationFileName, configurationURL,configurationInputStream);

        } else {

           localConfiguration.setSource("Programmatically configured.");

        }

 

        ConfigurationHelper configurationHelper= new ConfigurationHelper(this, localConfiguration);

        configure(configurationHelper);

        status = Status.STATUS_ALIVE;

        if (cacheManagerPeerProvider != null) {

            cacheManagerPeerProvider.init();

        }

       cacheManagerEventListenerRegistry.init();

        addShutdownHookIfRequired();

 

        //do this last

       addConfiguredCaches(configurationHelper);

 

    }

 

如果是内存模式的话,就没有什么特殊的要求,但是如果是磁盘模式的缓存的话,就需要给每个CacheManager配置一个唯一的路径。因为只有这样,才不会造成写冲突,这里面应该没有锁。如果不唯一,则会抛出CacheException.

如果一个应用里有用CacheManager的构造方法生成的CacheManager和用Create()函数生成的单例模式,那么这个单例和实例都可以共存,因为:

    private static CacheManager singleton;这让这些实例和单例不会冲突。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值