三)mybatis 二级缓存,整合ehcache
mybatis-config.xml
<setting name="cacheEnabled" value="true" />
PersonMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="cn.zno.smse.dao.PersonMapper" > <!-- # mybatis ehcache 缓存配置 # ① 应该首先在配置文件中开启缓存功能 ② 以下两个 <cache> 标签二选一,第一个可以输出日志,第二个不输出日志 。 <cache type="org.mybatis.caches.ehcache.LoggingEhcache" /> <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> --> <cache type="org.mybatis.caches.ehcache.LoggingEhcache" /> <!-- 省略代码 --> </mapper>
所处位置
<!ELEMENT mapper (cache-ref | cache | resultMap* | parameterMap* | sql* | insert* | update* | delete* | select* )+>
PersonMapper.xml 中禁用单个sql语句的缓存
useCache="false"
依赖
<dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.2</version> </dependency>
日志
初始化
------------------------------------------------------------------- GMS: address=pc012-44159, cluster=EH_CACHE, physical address=172.16.162.238:14872 ------------------------------------------------------------------- 2829 [localhost-startStop-1] INFO n.s.e.d.j.JGroupsCacheManagerPeerProvider - JGroups Replication started for 'EH_CACHE'. JChannel: local_addr=pc012-44159 cluster_name=EH_CACHE my_view=[pc012-44159|0] [pc012-44159] connected=true closed=false discard_own_messages=true state_transfer_supported=true props=UDP(bind_addr=/172.16.162.238;oob_thread_pool_keep_alive_time=5000;timer_keep_alive_time=3000;external_port=0;oob_thread_pool_enabled=true;max_bundle_size=64000;diagnostics_ttl=8;receive_on_all_interfaces=false;mcast_port=45588;thread_pool_min_threads=2;thread_pool_keep_alive_time=5000;thread_pool_max_threads=8;enable_diagnostics=true;ucast_send_buf_size=640000;ip_ttl=8;oob_thread_pool_queue_max_size=100;enable_bundling=true;thread_pool_queue_enabled=true;timer_rejection_policy=run;diagnostics_port=7500;oob_thread_pool_max_threads=8;wheel_size=200;disable_loopback=false;logical_addr_cache_max_size=500;ip_mcast=true;tick_time=50;logical_addr_cache_expiration=120000;thread_pool_rejection_policy=discard;oob_thread_pool_min_threads=1;port_range=50;stats=true;mcast_send_buf_size=640000;id=21;mcast_recv_buf_size=25000000;diagnostics_addr=/224.0.75.75;bind_port=0;tos=8;oob_thread_pool_rejection_policy=Run;loopback=true;oob_thread_pool_queue_enabled=false;name=UDP;enable_unicast_bundling=false;thread_pool_enabled=true;thread_naming_pattern=cl;ucast_recv_buf_size=20000000;timer_max_threads=10;timer_queue_max_size=500;discard_incompatible_packets=true;ergonomics=true;bundler_capacity=200000;max_bundle_timeout=30;mcast_group_addr=/228.8.8.8;bind_interface_str=;timer_min_threads=4;log_discard_msgs=true;thread_pool_queue_max_size=10000;bundler_type=new;timer_type=new) :PING(id=6;return_entire_cache=false;num_initial_members=20;break_on_coord_rsp=true;stagger_timeout=0;stats=true;name=PING;ergonomics=true;timeout=2000;num_initial_srv_members=0) :MERGE2(id=0;stats=true;merge_fast=true;name=MERGE2;inconsistent_view_threshold=1;min_interval=10000;ergonomics=true;merge_fast_delay=1000;max_interval=30000) :FD_SOCK(bind_addr=/172.16.162.238;external_port=0;port_range=50;stats=true;suspect_msg_interval=5000;client_bind_port=0;ergonomics=true;num_tries=3;id=3;get_cache_timeout=1000;sock_conn_timeout=1000;bind_interface_str=;name=FD_SOCK;keep_alive=true;start_port=0) :FD_ALL(id=29;interval=3000;stats=true;name=FD_ALL;ergonomics=true;msg_counts_as_heartbeat=false;timeout=10000;timeout_check_interval=2000) :VERIFY_SUSPECT(id=13;bind_addr=/172.16.162.238;bind_interface_str=;stats=true;name=VERIFY_SUSPECT;num_msgs=1;ergonomics=true;use_icmp=false;timeout=1500) :BARRIER(id=0;max_close_time=60000;stats=true;name=BARRIER;ergonomics=true) :pbcast.NAKACK2(use_mcast_xmit_req=false;use_mcast_xmit=false;max_msg_batch_size=500;xmit_from_random_member=false;stats=true;xmit_table_max_compaction_time=30000;log_not_found_msgs=true;ergonomics=true;discard_delivered_msgs=true;print_stability_history_on_failed_xmit=false;id=57;max_rebroadcast_timeout=2000;xmit_table_msgs_per_row=2000;xmit_table_num_rows=100;name=NAKACK2;log_discard_msgs=true;xmit_table_resize_factor=1.2;xmit_interval=1000) :UNICAST(max_retransmit_time=60000;max_msg_batch_size=500;xmit_table_max_compaction_time=60000;stats=true;segment_capacity=1000;ergonomics=true;id=12;conn_expiry_timeout=60000;xmit_table_msgs_per_row=2000;xmit_table_num_rows=100;name=UNICAST;timeout=400,800,1600,3200;xmit_table_resize_factor=1.2;xmit_interval=2000) :pbcast.STABLE(id=16;desired_avg_gossip=50000;max_bytes=4000000;stats=true;cap=0.1;name=STABLE;ergonomics=true;stability_delay=1000) :pbcast.GMS(print_local_addr=true;stats=true;max_bundling_time=50;log_collect_msgs=true;resume_task_timeout=20000;log_view_warnings=true;num_prev_views=20;ergonomics=true;use_flush_if_present=true;print_physical_addrs=true;merge_timeout=5000;id=14;num_prev_mbrs=50;leave_timeout=1000;view_bundling=true;name=GMS;join_timeout=3000;handle_concurrent_startup=true;view_ack_collection_timeout=2000;max_join_attempts=0) :UFC(id=45;max_block_time=5000;max_credits=2000000;stats=true;ignore_synchronous_response=true;min_credits=800000;name=UFC;min_threshold=0.4;ergonomics=true) :MFC(id=44;max_block_time=5000;max_credits=2000000;stats=true;ignore_synchronous_response=true;min_credits=800000;name=MFC;min_threshold=0.4;ergonomics=true) :FRAG2(id=5;frag_size=60000;stats=true;name=FRAG2;ergonomics=true) :RSVP(id=55;stats=true;name=RSVP;resend_interval=2000;throw_exception_on_timeout=true;ergonomics=true;ack_on_delivery=true;timeout=10000) :pbcast.STATE_TRANSFER(id=17;stats=true;name=STATE_TRANSFER;ergonomics=true)
首次时缓存
50125 [http-bio-8080-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 50141 [http-bio-8080-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1f689a9] was not registered for synchronization because synchronization is not active 50250 [http-bio-8080-exec-6] DEBUG o.a.i.cache.decorators.LoggingCache - Cache Hit Ratio [cn.zno.smse.dao.PersonMapper]: 0.0 50282 [http-bio-8080-exec-6] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [29424556, URL=jdbc:oracle:thin:@//172.16.50.67:1521/orcl, UserName=E_CHANNEL, Oracle JDBC driver] will not be managed by Spring 50282 [http-bio-8080-exec-6] DEBUG c.z.s.d.PersonMapper.countByExample - ooo Using Connection [29424556, URL=jdbc:oracle:thin:@//172.16.50.67:1521/orcl, UserName=E_CHANNEL, Oracle JDBC driver] 50297 [http-bio-8080-exec-6] DEBUG c.z.s.d.PersonMapper.countByExample - ==> Preparing: select count(*) from PERSON 50454 [http-bio-8080-exec-6] DEBUG c.z.s.d.PersonMapper.countByExample - ==> Parameters: 50547 [http-bio-8080-exec-6] DEBUG n.s.e.d.jgroups.JGroupsCachePeer - Created asynchronous message queue for 1000ms period 50547 [http-bio-8080-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1f689a9] 50547 [http-bio-8080-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 50547 [http-bio-8080-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@17c4e59] was not registered for synchronization because synchronization is not active 50547 [http-bio-8080-exec-6] DEBUG o.a.i.cache.decorators.LoggingCache - Cache Hit Ratio [cn.zno.smse.dao.PersonMapper]: 0.0 50579 [http-bio-8080-exec-6] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [33276711, URL=jdbc:oracle:thin:@//172.16.50.67:1521/orcl, UserName=E_CHANNEL, Oracle JDBC driver] will not be managed by Spring 50579 [http-bio-8080-exec-6] DEBUG c.z.s.d.P.selectByExample - ooo Using Connection [33276711, URL=jdbc:oracle:thin:@//172.16.50.67:1521/orcl, UserName=E_CHANNEL, Oracle JDBC driver] 50579 [http-bio-8080-exec-6] DEBUG c.z.s.d.P.selectByExample - ==> Preparing: select * from ( select row_.*, rownum rownum_ from ( select ID, NAME, AGE, BIRTHDAY, TYPE, SEX from PERSON ) row_ ) where rownum_ > 0 and rownum_ <= 10 50579 [http-bio-8080-exec-6] DEBUG c.z.s.d.P.selectByExample - ==> Parameters: 50625 [http-bio-8080-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@17c4e59] 51547 [EH_CACHE Async Replication Thread] DEBUG n.s.e.d.jgroups.JGroupsCachePeer - Sending 2 JGroupEventMessages from the asynchronous queue.
命中缓存
59829 [http-bio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 59829 [http-bio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1f58af5] was not registered for synchronization because synchronization is not active 59829 [http-bio-8080-exec-10] DEBUG o.a.i.cache.decorators.LoggingCache - Cache Hit Ratio [cn.zno.smse.dao.PersonMapper]: 0.3333333333333333 59829 [http-bio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1f58af5] 59829 [http-bio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 59829 [http-bio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1f12767] was not registered for synchronization because synchronization is not active 59829 [http-bio-8080-exec-10] DEBUG o.a.i.cache.decorators.LoggingCache - Cache Hit Ratio [cn.zno.smse.dao.PersonMapper]: 0.5 59829 [http-bio-8080-exec-10] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1f12767]
当执行更新操作时
361860 [http-bio-8080-exec-7] DEBUG n.s.e.d.j.JGroupsCacheReplicator - Remove all elements called on cn.zno.smse.dao.PersonMapper