ehcache 详解 实例
1.EhCache是什么
EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力;
2.EhCache的使用注意点
当用Hibernate的方式修改表数据(save,update,delete等等),这时EhCache会自动把缓存中关于此表的所有缓存全部删除掉(这样能达到同步)。但对于数据经常修改的表来说,可能就失去缓存的意义了(不能减轻数据库压力);
3.EhCache使用的场合
3.1比较少更新表数据
EhCache一般要使用在比较少执行write操作的表(包括update,insert,delete等)[Hibernate的二级缓存也都是这样];
3.2对并发要求不是很严格的情况
两台机子中的缓存是不能实时同步的;
4.在项目做的实现
4.1在工程的src目录下添加ehcache.xml文件,内容如下:
<!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property> <!-- The property below is commented out b/c it doesn't work when run via Ant in Eclipse. It works fine for individual JUnit tests and in IDEA ?? <property name="mappingJarLocations"> <list><value>file:dist/appfuse-dao.jar</value></list> </property> --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">@HIBERNATE-DIALECT@</prop> <!--<prop key="hibernate.show_sql">true</prop>--> <prop key="hibernate.max_fetch_depth">3</prop> <prop key="hibernate.hibernate.use_outer_join">true</prop> <prop key="hibernate.jdbc.batch_size">10</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <!-- <prop key="hibernate.use_sql_comments">false</prop> --> <!-- Create/update the database tables automatically when the JVM starts up <prop key="hibernate.hbm2ddl.auto">update</prop> --> <!-- Turn batching off for better error messages under PostgreSQL <prop key="hibernate.jdbc.batch_size">0</prop> --> </props> </property> <property name="entityInterceptor"> <ref local="auditLogInterceptor"/> </property> </bean>
6、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 (org.hibernate.cache.StandardQueryCache); 另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。请注意:在查询 缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。需要将打印sql语句与最近的cache内 容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。
CacheManager manager = new CacheManager("src/config/other.xml");
缓存的创建,采用自动的方式
CacheManager singletonManager = CacheManager.create();
singletonManager.addCache("testCache");
Cache test = singletonManager.getCache("testCache");
或者直接创建Cache
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
manager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache("testCache");
删除cache
CacheManager singletonManager = CacheManager.create();
singletonManager.removeCache("sampleCache1");
在使用ehcache后,需要关闭
CacheManager.getInstance().shutdown()
caches 的使用
Cache cache = manager.getCache("sampleCache1");
执行crud操作
Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1");
cache.put(element);
update
Cache cache = manager.getCache("sampleCache1");
cache.put(new Element("key1", "value1");
//This updates the entry for "key1"
cache.put(new Element("key1", "value2");
get Serializable
Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Serializable value = element.getValue();
get non serializable
Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Object value = element.getObjectValue();
remove
Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1"
cache.remove("key1");