我需要做出的决定之一是在我的系统中使用的缓存框架.有这么多人可供选择,我目前正在调查redis,ehcache和memcached.
任何人都可以指出这三个特定框架的性能基准吗?还有他们的功能概述 - 我对缺点特别感兴趣,即.你可以使用一个而不是另一个的情况.
我该如何决定CacheConcurrencyStrategy
使用哪个?
NonstrictReadWriteCache
,ReadOnlyCache
,ReadWriteCache
,TransactionalCache
.我阅读了https://www.hibernate.org/hib_docs/v3/api/org/hibernate/cache/CacheConcurrencyStrategy.html,但没有详细解释.
我有一个带有Spring Data JPA(hibernate后端)存储库类的Spring Boot应用程序.我添加了一些自定义查找器方法,其中一些具有特定@Query
注释,以告诉它如何获取数据.我已经为hibernate二级缓存设置了EhCache,但到目前为止,我可以获得这些结果缓存的唯一方法是启用hibernate查询缓存.我更喜欢定义一个特定的缓存并将实际的域对象存储在那里,就像它是一个普通的查找程序一样.以下是我的回购代码:
public interface PromotionServiceXrefRepository extends PagingAndSortingRepository<PromotionServiceXref, Integer> {
@Query("SELECT psx FROM Customer c " +
"JOIN c.customerProductPromotions cpp " +
"JOIN cpp.productPromotion pp " +
"JOIN pp.promotion p JOIN p.promotionServiceXrefs psx " +
"WHERE c.customerId = ?1")
@QueryHints(@QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "promotionServiceXrefByCustomerId")
Set<PromotionServiceXref> findByCustomerId(int customerId);
}
Run Code Online (Sandbox Code Playgroud)
这里是我定义的"promotionServiceXrefByCustomerId"缓存,它没有被使用:
<cache name="promotionServiceXrefByCustomerId" overflowToDisk="true" diskPersistent="true"
maxEntriesLocalHeap="3000000" eternal="true" diskSpoolBufferSizeMB="20" memoryStoreEvictionPolicy="LFU"
transactionalMode="off" statistics="true">
</cache>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如果我启用,StandardQueryCache
则此数据将缓存在那里,而hibernate不会执行查询.但是当我禁用查询缓存时,这不会被缓存.我在这做错了什么?请帮忙!
我的错误是:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot find cache named 'getActionsBycasId' for CacheableOperation[public java.util.List com.codinko.database.DataBaseConnection.getActionsByCasId(int)] caches=[getActionsBycasId] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless=''
at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:81)
at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:214)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:553)
at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:227)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:498)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.codinko.database.DataBaseConnection$$EnhancerBySpringCGLIB$$21a0d8a.getActionsByCasId(<generated>)
at com.codinko.caching.EmployeeDAO.getActionBycasId(EmployeeDAO.java:47)
at com.codinko.caching.EmployeeDAO$$FastClassBySpringCGLIB$$191aa49b.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
at com.codinko.caching.EmployeeDAO$$EnhancerBySpringCGLIB$$3399d753.getActionBycasId(<generated>)
at com.codinko.caching.Main.main(Main.java:22)
Run Code Online (Sandbox Code Playgroud)
我的功能是:
@Cacheable("getActionsBycasId")
public List<SMSAction> getActionsByCasId(int casId){
System.out.println("Inside getActionsByCasId");
//My logic
return list;
}
Run Code Online (Sandbox Code Playgroud)
当我在ehcache.xml下面添加时,上面的错误没有来,但不知道为什么会出现这个错误.
<cache name="getActionsBycasId" maxElementsInMemory="50" eternal="false"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" …
Run Code Online (Sandbox Code Playgroud) 我正在配置我的hibernate项目以使用二级缓存提供程序,以便我可以利用查询缓存.
我在ehcache中添加了一个依赖项:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.2.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我认为我想要使用的提供程序类是:
net.sf.ehcache.hibernateEhCacheProvider
Run Code Online (Sandbox Code Playgroud)
当我在eclipse中查看引用的库时,我会看到@Deprecated
注释EhCacheProvider
,同时也会看到注释SingletonEhCacheProvider
.是什么赋予了?我可以使用最新的替代供应商吗?
我正在使用hibernate版本3.4.0.GA,以防万一.
将EhCache v2.4.5配置为hibernate v3.6.7的二级缓存后,在尝试使用hibernate会话加载特定实体的所有对象时出现以下错误.(第一次加载对象没有错误)
java.lang.IllegalStateException: The country Cache is not alive.
at net.sf.ehcache.Cache.checkStatus(Cache.java:2438)
at net.sf.ehcache.Cache.get(Cache.java:1541)
at net.sf.ehcache.hibernate.regions.EhcacheTransactionalDataRegion.get(EhcacheTransactionalDataRegion.java:105)
at net.sf.ehcache.hibernate.strategy.AbstractReadWriteEhcacheAccessStrategy.putFromLoad(AbstractReadWriteEhcacheAccessStrategy.java:90)
at net.sf.ehcache.hibernate.nonstop.NonstopAwareEntityRegionAccessStrategy.putFromLoad(NonstopAwareEntityRegionAccessStrategy.java:180)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:195)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:982)
at org.hibernate.loader.Loader.doQuery(Loader.java:857)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at dataAccess.CountryDAO.loadAll(CountryDAO.java:80)
Run Code Online (Sandbox Code Playgroud)
我的hibernate配置是:
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
</property>
<property name="hibernate.cache.provider_configuration">
/ehcache.xml
</property>
<property name="hibernate.cache.use_second_level_cache">
true
</property>
<property name="hibernate.cache.use_query_cache">
true
</property>
Run Code Online (Sandbox Code Playgroud)
我的EhCache配置是:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<diskStore path="java.io.tmpdir" />
<transactionManagerLookup
class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
properties="jndiName=java:/TransactionManager" propertySeparator=";" />
<cacheManagerEventListenerFactory
class="" properties="" />
<defaultCache maxElementsInMemory="0" …
Run Code Online (Sandbox Code Playgroud) 我有一个服务项目和一个Web项目.我需要在两个项目中都有eh-cache.
这个想法是,如果服务项目更新,它的缓存相关更改(如密钥和失效规则)也将可用,而不会对Web项目进行任何更改.如此独立,服务项目也可以与其他项目一起使用,甚至不知道eh-cache.
此时,我的Web项目也将eh-cache用于自己的目的.我对eh-cache不太熟悉,我担心这两个项目在部署时可能会发生冲突.我也没有在eh-cache网站上找到相关信息.
您能否为我提供一些如何最好地配置这两个项目的信息,以便我能达到上述要求?
编辑:
我正在使用Spring,因此我更愿意将它用于我的缓存管理器.
我在context.xml中为使用ehcache的每个jar使用以下内容,例如对于jar 1我有:
<ehcache:annotation-driven cache-manager="ehCacheManager1" />
<bean id="ehCacheManager1" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache-1.xml" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我有罐子2
<ehcache:annotation-driven cache-manager="ehCacheManager2" />
<bean id="ehCacheManager2" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache-2.xml" />
</bean>
Run Code Online (Sandbox Code Playgroud)
那么,两个缓存都会起作用吗?我担心ehcache:annotation-driven
上次读取的上下文会覆盖它,只有一个缓存可以运行.我错了,还是错过了什么?
我在高流量网站上使用Hibernate 3.6.8,ehcache 2.4.5(也尝试过最新的2.8.0),jvm 1.6.0_22,有时我会体验
ObjectNotFoundException:不存在具有给定标识符的行:[com.example.Foo#123]`
Foo
通过最简单的代码创建一个新的(在这种情况下为id 123):
Foo foo = new Foo();
session.save(foo);
Run Code Online (Sandbox Code Playgroud)
原因是在这个高流量网站的所有页面中,我得到Foo
的是这样的:
session.createQuery("from Foo").setCacheable(true).list();
Run Code Online (Sandbox Code Playgroud)
存储Foo
s 的表包含1000行,实体缓存在ehcache中:
<class-cache class="com.example.Foo" usage="read-write" />
Run Code Online (Sandbox Code Playgroud)
我的Hibernate配置的其他可能相关的部分是:
<property name="connection.url">jdbc:mysql://localhost:3306/example?characterEncoding=UTF-8</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">60</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.timeout">0</property>
<property name="hibernate.c3p0.acquireRetryAttempts">1</property>
<property name="hibernate.c3p0.acquireRetryDelay">1</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.jdbc.use_scrollable_resultset">true</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>
<property name="hibernate.cache.use_query_cache">true</property>
Run Code Online (Sandbox Code Playgroud)
错误发生一次然后消失.我怀疑ehcache查询缓存是使用新的实体id(123)id更新的,但实体缓存尚未使用该实体的内容进行更新.我使用JMeter在本地轻松地重现了这一点.
关于如何解决这个问题的任何想法?
在Foo
创作时,ObjectNotFoundException
抛出一次.另一方面,如果我删除了一个实例,Foo
那么我经常(并且永远)得到ObjectNotFoundException
每次执行.list()
.堆栈跟踪可以在http://pastebin.com/raw.php?i=dp3HBgDB …
哪个更适合以下环境:
我现在会倒出脑子里的东西.我可能错了.
我知道Redis需要一个单独的服务器(?),Ehcache提供本地缓存,因此它必须更快,但会跨服务器复制缓存(?).使用Ehcache可以在更新一个缓存后更新所有缓存.
我的问题是哪个更符合我提到的环境?
谁的表现会更好,或者什么样的表现可能比另一个表现更好?
提前致谢.
ehcache ×10
caching ×5
hibernate ×5
java ×5
redis ×2
spring ×2
spring-boot ×2
concurrency ×1
memcached ×1
orm ×1
oscache ×1
spring-data ×1
terracotta ×1
war ×1