【转】使用 EhCache 关于临时目录的一个注意事项

http://m.oschina.net/question/12_2368

 

一般 ehcache 的配置中默认的 diskStore 的路径设置的是 java.io.tmpdir ,等于是当前系统的临时目录。

但是在 Tomcat  和 Resin 这两个应用服务器上,临时目录是有区别的,在 Tomcat 上运行的应用通过 java.io.tmpdir 系统变量获取到的路径是Tomcat 目录下的 temp 子目录,而 Resin 返回的是系统的临时目录,linux下可能就是 /tmp

在 Linux 下如果我们使用的是 root 账号来启动 Tomcat 和 Resin 的话,那这个问题就不存在。但是我们非常不建议用 root 来启动 Tomcat 和 Resin,这时候我们会单独的创建一个非特权账号,假设该账号名为 www 来运行应用服务器。

我们需要将 Tomcat 和 Resin 所在的目录授权给 www 账号,这样应用服务器的日志文件才能正常的写入,但是由于 Resin 的临时目录是对应系统的 /tmp 目录,因此如果应用中使用了 ehcache 并设置了存储路径为 java.io.tmpdir ,你就会发现启动的时候报错,提示没有在 /tmp 目录下创建文件的权限,这是因为 www 账号没有写 tmp 目录的权限。

解决的办法就是修改 ehcache 的 diskStore 配置的值为  user.home ,将存储文件路径指定到用户的主目录下即可。

而 Tomcat 就没有这个问题,因为它的临时目录在 {tomcat}/temp ,而整个 {tomcat} 都已经授权给 www 账号了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-distribution.tar.gz”压缩文件中找到 2、由于要实现Ehcache缓存页面,所以必须要添加“ehcache-web-2.0.4.jar” jar包,该jar包主要用于辅助Ehcache实现页面缓存 注意: 本web工程的发布不要使用Tomcat7,否则会出现如下异常: 2015-3-25 9:53:50 org.apache.catalina.loader.WebappClassLoader loadClass 信息: Illegal access: this web application instance has been stopped already. Could not load net.sf.ehcache.store.disk.DiskStore$KeySet. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at net.sf.ehcache.store.disk.DiskStore.keySet(DiskStore.java:560) at net.sf.ehcache.store.disk.DiskStorageFactory$DiskExpiryTask.run(DiskStorageFactory.java:838) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) 相关jar包下载地址: Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache
以下是使用Ehcache作为缓存的Spring Boot应用程序的示例: 1.添加Ehcache依赖项 在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> ``` 2.配置Ehcache 在application.properties文件中添加以下配置: ```properties spring.cache.type=ehcache ``` 3.创建缓存管理器 创建一个名为EhCacheConfig的类,并在其中创建一个名为cacheManager的方法,该方法返回一个CacheManager对象。 ```java @Configuration @EnableCaching public class EhCacheConfig { @Bean public CacheManager cacheManager() { return new EhCacheCacheManager(ehCacheManager().getObject()); } @Bean public EhCacheManagerFactoryBean ehCacheManager() { EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean(); factory.setConfigLocation(new ClassPathResource("ehcache.xml")); factory.setShared(true); return factory; } } ``` 4.创建缓存 在需要缓存的方法上添加@Cacheable注释,并指定缓存名称和缓存键。 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override @Cacheable(value = "users", key = "#id") public User getUserById(Long id) { return userRepository.findById(id).orElse(null); } } ``` 以上就是使用Ehcache作为缓存的Spring Boot应用程序的示例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值