1.加入hibernate-memcached.jar及相关jar包(具体见http://code.google.com/p/hibernate-memcached/)
2.在applicationContext.xml中配置
............
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${jpa.showSql}" />
<property name="generateDdl" value="${jpa.generateDdl}" />
<property name="database">
<util:constant
static-field="org.springframework.orm.jpa.vendor.Database.ORACLE" />
</property>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.cache.use_second_level_cache">
true
</prop>
<prop key="hibernate.cache.use_structured_entries">
true
</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">
com.googlecode.hibernate.memcached.MemcachedCacheProvider
</prop>
<prop key="hibernate.memcached.servers">
10.1.19.132:33001 10.1.19.132:33002 10.1.19.132:33003
</prop>
</props>
</property>
</bean>
............
3.在实体类上配置缓存策略
如@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
下面这几种情况就
不适合加载到二级缓存中:
1.经常被修改的数据
2.绝对不允许出现并发访问的数据
3.与其他应用共享的数据
下面这己种情况
合适加载到二级缓存中:
1.数据更新频率低
2.允许偶尔出现并发问题的非重要数据
3.不会被并发访问的数据
4.常量数据
5.不会被第三方修改的数据