ehcache是一个非常可配置的野兽,示例相当复杂,通常涉及多层接口.
有没有人遇到过最简单的例子,它只是在内存中缓存类似单个数字的东西(不是分布式的,没有XML,只有尽可能少的java行).然后将该数字缓存60秒,然后下一个读取请求使其获得新值(例如,通过调用Random.nextInt()或类似)
使用单例和一些同步来编写我们自己的缓存是否更快/更容易?
请不要春天.
这个链接(来自Ehcache的创建者)说当你只有一个Hibernate SessionFactory时你应该使用SingletonEhCacheRegionFactory,当你有多个时,你应该使用EhCacheRegionFactory.
但是当你只有一个SessionFactory时,EhCacheRegionFactory不会是单个实例吗?
那么,SingletonEhCacheRegionFactory有什么好处呢?为什么不一直使用EhCacheRegionFactory,因为它可以用于一个SessionFactory或多个?
仅供参考:我使用的是Ehcache 2.4.2和Hibernate 3.6.5
我有一个util模块,可以生成一个可以在其他应用程序中使用的jar.我希望这个模块使用缓存,并且更喜欢使用Spring的annotation-driven缓存.
所以Util-Module会有这样的事情:
DataManager.java
...
@Cacheable(cacheName="getDataCache")
public DataObject getData(String key) { ... }
...
Run Code Online (Sandbox Code Playgroud)
数据管理器 - ehcache.xml中
...
<cache name="getDataCache" maxElementsInMemory="100" eternal="true" />
...
Run Code Online (Sandbox Code Playgroud)
数据管理器弹簧-config.xml中
...
<cache:annotation-driven cache-manager="data-manager-cacheManager" />
<!-- ???? --->
<bean id="data-manager-cacheManager"
class="org.springframework.cache.ehcache.EhcacheCacheManager"
p:cache-manager="data-manager-ehcache"/>
<bean id="data-manager-ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="data-manager-ehcache.xml"/>
...
Run Code Online (Sandbox Code Playgroud)
我还希望我的可部署单元通过Spring注释进行缓存,同时将上面的jar包含为依赖项.所以我Deployable-Unit会有这样的事情:
MyApp.java
...
@Cacheable(cacheName="getMyAppObjectCache")
public MyAppObject getMyAppObject(String key) { ... }
...
Run Code Online (Sandbox Code Playgroud)
我-APP-ehcache.xml中
...
<cache name="getMyAppObjectCache" maxElementsInMemory="100" eternal="true" />
...
Run Code Online (Sandbox Code Playgroud)
我的应用程序内弹簧-config.xml中
...
<cache:annotation-driven cache-manager="my-app-cacheManager" />
<!-- ???? --->
<bean id="my-app-cacheManager"
class="org.springframework.cache.ehcache.EhcacheCacheManager"
p:cache-manager="my-app-ehcache"/>
<bean id="my-app-ehcache" …Run Code Online (Sandbox Code Playgroud) 12:18:55,541 INFO [UpdateChecker]发现新的更新:2.0.0 [ http://ehcache.org/news.html]
如何禁止ehcache检查新的更新,这是在加载我的j2ee应用程序和ehcache初始化时发生的.
我正在尝试实现Spring 3.1缓存,如此处和此处所述,但它似乎不起作用:我的方法每次都会运行,即使它被标记为@cacheable.我究竟做错了什么?
我已经将它移动到junit测试用例中,并使用自己的配置文件将其与我的应用程序的其余部分隔离开来,但问题仍然存在.以下是相关文件:
弹簧试验servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
ehcache.xml中
<ehcache>
<diskStore path="java.io.tmpdir"/>
<cache name="cache"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
Run Code Online (Sandbox Code Playgroud)
MyTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-test-servlet.xml"})
@Component
public class MyTest extends TestCase {
@Test
public void testCache1(){
for(int i = 0; i < 5; i++){
System.out.println("Calling someMethod...");
System.out.println(someMethod(0));
}
}
@Cacheable("testmethod")
private int someMethod(int val){
System.out.println("Not from cache");
return …Run Code Online (Sandbox Code Playgroud) 我想用Java中的ehcache做一些我认为应该非常简单的事情,但是我花了足够的时间来挫败自己的文档......
将值写入磁盘永久缓存.关掉.
再次启动并读取该值.
这是我的Java函数:
private static void testCacheWrite() {
// create the cache manager from our configuration
URL url = TestBed.class.getClass().getResource("/resource/ehcache.xml");
CacheManager manager = CacheManager.create(url);
// check to see if our cache exits, if it doesn't create it
Cache testCache = null;
if (!manager.cacheExists("test")) {
System.out.println("No cache found. Creating cache...");
int maxElements = 50000;
testCache = new Cache("test", maxElements,
MemoryStoreEvictionPolicy.LFU, true, null, true, 60, 30,
true, Cache.DEFAULT_EXPIRY_THREAD_INTERVAL_SECONDS, null);
manager.addCache(testCache);
// add an element to persist
Element el = new Element("key", …Run Code Online (Sandbox Code Playgroud) 我们有一个使用Hibernate的二级缓存来避免数据库命中的应用程序.
我想知道当一个外部进程如MySQL管理员直接连接修改数据库(更新/插入/删除)时,是否有一些简单的方法可以使Java应用程序的Hibernate二级缓存无效.
我们使用EHCache作为我们的二级缓存实现.
我们混合使用@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)和@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE),并且我们没有在每个实体上使用时间戳启用Optimistic并发控制.
SessionFactory包含管理二级缓存的方法: - 管理缓存
sessionFactory.evict(Cat.class, catId); //evict a particular Cat
sessionFactory.evict(Cat.class); //evict all Cats
sessionFactory.evictCollection("Cat.kittens", catId); //evict a particular collection of kittens
sessionFactory.evictCollection("Cat.kittens"); //evict all kitten collections
Run Code Online (Sandbox Code Playgroud)
但是因为我们使用@Cache注释单个实体类,所以我们没有"可靠"(例如没有手动步骤)将其添加到列表的中心位置.
// Easy to forget to update this to properly evict the class
public static final Class[] cachedEntityClasses = {Cat.class, Dog.class, Monkey.class}
public void clear2ndLevelCache() {
SessionFactory sessionFactory = ... //Retrieve SessionFactory
for (Class entityClass : cachedEntityClasses) {
sessionFactory.evict(entityClass);
}
}
Run Code Online (Sandbox Code Playgroud)
Hibernate的二级缓存没有真正的方法来知道数据库中的实体发生了变化,除非它查询该实体(缓存正在保护您的实体).因此,作为一种解决方案,我们可以简单地调用一些方法来强制二级缓存驱逐一切(再次因为缺乏锁定和并发控制,您可能会因"读取"或更新过时数据而导致进程中的事务风险).
我正在将我们的代码迁移到Spring 3.2版本(从3.1.3开始),我遇到了Spring Cache Abstraction的问题.
我们使用EhCache实现,CacheManager其配置非常简单:
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" />
Run Code Online (Sandbox Code Playgroud)
我的问题是我缺少EhCacheCacheManager类和相应的工厂bean spring-context jar.我想他们将实现转移到其他模块,但我找不到确切的位置.
我正在使用二级缓存和查询缓存.我可以知道如何以编程方式清除所有缓存吗?
我正在尝试添加ehcache(v2.6.0)到我的Hibernate 4.1.5.SP1项目,但有一些配置问题.具体来说,当我尝试使用构建配置时,我得到java.lang.NoClassDefFoundError:org/hibernate/cache/EntityRegion错误Hibernate
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
Run Code Online (Sandbox Code Playgroud)
这是我的Maven依赖...
<hibernate.version>4.1.5.SP1</hibernate.version>
<hibernate.validator.version>4.3.0.Final</hibernate.validator.version>
<ehcacheVersion>2.6.0</ehcacheVersion>
...
<!-- Hibernate dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcacheVersion}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是我用来配置它的Java代码......
Configuration config = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:myprojectTestDb")
.setProperty("hibernate.connection.username", "sa")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.connection.pool_size", "1")
.setProperty("hibernate.connection.autocommit", "true")
.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider")
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
.setProperty("hibernate.show_sql", "true")
.setProperty("hibernate.current_session_context_class", "thread")
.setProperty("hibernate.cache.use_second_level_cache", "true")
.setProperty("hibernate.cache.region.factory_class", "net.sf.ehcache.hibernate.EhCacheRegionFactory")
.addAnnotatedClass(Organization.class)
.addAnnotatedClass(State.class)
.addAnnotatedClass(Country.class)
.addAnnotatedClass(Domain.class)
.addAnnotatedClass(Community.class);
final ServiceRegistry …Run Code Online (Sandbox Code Playgroud) ehcache ×10
hibernate ×4
java ×4
spring ×3
caching ×2
annotations ×1
database ×1
ehcache-2 ×1
maven ×1
orm ×1
persistence ×1
transactions ×1