Hibernate ehcache diskstore 缓存文件路径可以放在配置文件中吗?

bittracer 2016-11-06 10:11:32
看文档中只能按照以下方式设置:

The following properties are translated:
* user.home - User's home directory
* user.dir - User's current working directory
* java.io.tmpdir - Default temp file path
* ehcache.disk.store.dir - A system property you would normally specify on the command line
e.g. java -Dehcache.disk.store.dir=/u01/myapp/diskdir ...

Subdirectories can be specified below the property e.g. java.io.tmpdir/one

只能写死在ehcache.xml文件中。比如:
<diskStore path="D:/ehcache"/>

能否在config.properties添加键值对:ehcache.disk.store.dir = D:/ehcache 然后在ehcache.xml中引用?比如:
<diskStore path="${ehcache.disk.store.dir}" />
发现这样并没有存储在期望的“D:/ehcache”,而是在项目根路径下创建了一个子目录“${ehcache.disk.store.dir}”

求解,难道这个存储路径配置不能放在独立的配置文件中去吗?
...全文
449 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱好打劫 2019-06-04
  • 打赏
  • 举报
回复
不能用xml配置的方式了。需要用bean注入

@Configuration
@EnableCaching
public class CachingConfiguration implements CachingConfigurer {

  @Value("${city}")
  private String city;

  @Bean(destroyMethod = "shutdown")
  public net.sf.ehcache.CacheManager ehCacheManager() {
    net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
    DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
    diskStoreConfiguration.setPath("E:\\Release\\HouseRank\\cache_" + city + "\\default");
    config.diskStore(diskStoreConfiguration);

    CacheConfiguration defaultCache = new CacheConfiguration();
    defaultCache.setMaxEntriesLocalHeap(0);
    defaultCache.setEternal(false);
    defaultCache.setTimeToIdleSeconds(1200);
    defaultCache.setTimeToLiveSeconds(1200);
    config.addDefaultCache(defaultCache);

    CacheConfiguration averagePriceCache = new CacheConfiguration();
    averagePriceCache.setName("averagePriceCache");
    averagePriceCache.setMaxEntriesLocalHeap(1000);
    averagePriceCache.setMaxEntriesLocalDisk(10000);
    averagePriceCache.setEternal(false);
    averagePriceCache.setDiskSpoolBufferSizeMB(20);
    averagePriceCache.setTimeToIdleSeconds(20);
    averagePriceCache.setTimeToLiveSeconds(20);
    averagePriceCache.setMemoryStoreEvictionPolicy("LFU");
    averagePriceCache.setTransactionalMode("off");
    PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
    persistenceConfiguration.setStrategy("localTempSwap");
    averagePriceCache.persistence(persistenceConfiguration);
    config.addCache(averagePriceCache);

    CacheConfiguration hibernateCache = new CacheConfiguration();
    hibernateCache.setName("org.hibernate.cache.internal.StandardQueryCache");
    hibernateCache.setMaxEntriesLocalHeap(5);
    hibernateCache.setEternal(false);
    hibernateCache.setTimeToLiveSeconds(120);
    hibernateCache.persistence(persistenceConfiguration);
    config.addCache(hibernateCache);

    CacheConfiguration updateTimestampCache = new CacheConfiguration();
    updateTimestampCache.setName("org.hibernate.cache.spi.UpdateTimestampsCache");
    updateTimestampCache.setMaxEntriesLocalHeap(5000);
    updateTimestampCache.setEternal(true);
    hibernateCache.persistence(persistenceConfiguration);
    config.addCache(updateTimestampCache);

    return net.sf.ehcache.CacheManager.newInstance(config);
  }

  @Bean
  @Override
  public CacheManager cacheManager() {
    return new EhCacheCacheManager(ehCacheManager());
  }

  @Override
  public CacheResolver cacheResolver() {
    return null;
  }

  @Override
  public KeyGenerator keyGenerator() {
    return new SimpleKeyGenerator();
  }

  @Override
  public CacheErrorHandler errorHandler() {
    return null;
  }
}
用代码注入的方式代替xml 即可用${}变量
bcsflilong 2016-11-10
  • 打赏
  • 举报
回复
<span style="color: #FF0000;">*/*.properties</span>
bcsflilong 2016-11-10
  • 打赏
  • 举报
回复
最后说一下 你单独拿出来的那个文件 如果是在SSH下的话 在web.xml 应该让它加载进来

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/*.xml,classpath*:applicationContext.xml</param-value>
    </context-param>
你的config.properties 应该加载进来

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/*.xml,classpath*:applicationContext.xml:classpath:*/*.properties</param-value>
    </context-param>
bcsflilong 2016-11-10
  • 打赏
  • 举报
回复
引用 2 楼 bittracer 的回复:
[quote=引用 1 楼 bcsflilong 的回复:] 应该是可以的
求方法,谢谢![/quote] 有的人写hibernate.cfg.xml 的时候吧数据库链接部分的东西单独拿出来写在.properties文件里 你找个类似的参考过一下试试 因为我的确没有那么做过 所以感觉有点帮不上你了
bittracer 2016-11-10
  • 打赏
  • 举报
回复
引用 1 楼 bcsflilong 的回复:
应该是可以的
求方法,谢谢!
bcsflilong 2016-11-08
  • 打赏
  • 举报
回复
应该是可以的

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧