分布式缓存
对缓存数据进行集中管理使用分布式缓存框架 redis、memcached、ehcache等。mybatis无法实现分布式缓存,需要和其它分布式缓存框架进行整合。
不使用分布缓存,缓存的数据在各各服务单独存储,不方便系统 开发。所以要使用分布式缓存对缓存数据进行集中管理。
分布式缓存工作图
mybatis本身来说是无法实现分布式缓存的,所以要与分布式缓存框架进行整合。
mybatis整合分布式缓存ehcache
首先加入ehcache的jar包
上图为jar包。
在User.xml中加入一行代码
配置缓存
- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
- <settings>
- <setting name="cacheEnabled" value="true"/>
- </settings>
- package cn.bj.mybatis.test;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import cn.bj.mybatis.model.IUserOperation;
- import cn.bj.mybatis.model.User;
- import cn.bj.mybatis.model.UserCustom;
- import cn.bj.mybatis.model.UserQueryVO;
- public class MybatisTest {
- public static void main(String[] args){
- SqlSessionFactory sqlSessionFactory = null;
- SqlSession session1 = null;
- SqlSession session2 = null;
- SqlSession session3 = null;
- SqlSession session4 = null;
- String resource = "Configuration.xml";
- InputStream inputStream;
- try {
- inputStream = Resources.getResourceAsStream(resource);
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- session1 = sqlSessionFactory.openSession();
- session2 = sqlSessionFactory.openSession();
- session3 = sqlSessionFactory.openSession();
- session4 = sqlSessionFactory.openSession();
- IUserOperation userOperation1 = (IUserOperation)session1.getMapper(IUserOperation.class);
- IUserOperation userOperation2 = (IUserOperation)session2.getMapper(IUserOperation.class);
- IUserOperation userOperation3 = (IUserOperation)session3.getMapper(IUserOperation.class);
- IUserOperation userOperation4 = (IUserOperation)session4.getMapper(IUserOperation.class);
- User user1 = (User)userOperation1.selectUser(5);
- System.out.println(user1.getId());
- System.out.println(user1.getUsername());
- session1.close();
- User user2 = (User)userOperation2.selectUser(5);
- user2.setAge(34);
- userOperation2.updateUser(user2);
- session2.commit();
- session2.close();
- User user3 = (User)userOperation3.selectUser(5);
- System.out.println(user3.getId());
- System.out.println(user3.getUsername());
- session3.close();
- User user4 = (User)userOperation4.selectUser(5);
- System.out.println(user4.getId());
- System.out.println(user4.getUsername());
- session4.close();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- }
- }
- }
看输出效果:
- DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
- DEBUG [main] - Configuring ehcache from ehcache.xml found in the classpath: file:/D:/workspace/MyBatisTest/bin/ehcache.xml
- DEBUG [main] - Configuring ehcache from URL: file:/D:/workspace/MyBatisTest/bin/ehcache.xml
- DEBUG [main] - Configuring ehcache from InputStream
- DEBUG [main] - Ignoring ehcache attribute xmlns:xsi
- DEBUG [main] - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
- DEBUG [main] - Disk Store Path: F:\develop\ehcache
- DEBUG [main] - Creating new CacheManager with default config
- DEBUG [main] - propertiesString is null.
- DEBUG [main] - No CacheManagerEventListenerFactory class specified. Skipping...
- DEBUG [main] - No BootstrapCacheLoaderFactory class specified. Skipping...
- DEBUG [main] - CacheWriter factory not configured. Skipping...
- DEBUG [main] - No CacheExceptionHandlerFactory class specified. Skipping...
- DEBUG [main] - Initialized net.sf.ehcache.store.NotifyingMemoryStore for cn.bj.mybatis.model.IUserOperation
- DEBUG [main] - Initialised cache: cn.bj.mybatis.model.IUserOperation
- DEBUG [main] - CacheDecoratorFactory not configured for defaultCache. Skipping for 'cn.bj.mybatis.model.IUserOperation'.
- DEBUG [main] - <span style="color:#ff0000;">Cache Hit Ratio [cn.bj.mybatis.model.IUserOperation]: 0.0</span>
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Created connection 28235257.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - ==> Preparing: select * from t_user where id = ?
- DEBUG [main] - ==> Parameters: 5(Integer)
- DEBUG [main] - <== Total: 1
- 5
- kksskdk
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - Returned connection 28235257 to pool.
- DEBUG [main] - <span style="color:#ff6666;">Cache Hit Ratio [cn.bj.mybatis.model.IUserOperation]: 0.5</span>
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Checked out connection 28235257 from pool.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - ==> Preparing: update t_user set username=?,age=? where id=?
- DEBUG [main] - ==> Parameters: kksskdk(String), 34(Integer), 5(Integer)
- DEBUG [main] - <== Updates: 1
- DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - Returned connection 28235257 to pool.
- DEBUG [main] - <span style="color:#ff6666;">Cache Hit Ratio [cn.bj.mybatis.model.IUserOperation]: 0.3333333333333333</span>
- DEBUG [main] - Opening JDBC Connection
- DEBUG [main] - Checked out connection 28235257 from pool.
- DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - ==> Preparing: select * from t_user where id = ?
- DEBUG [main] - ==> Parameters: 5(Integer)
- DEBUG [main] - <== Total: 1
- 5
- kksskdk
- DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1aed5f9]
- DEBUG [main] - Returned connection 28235257 to pool.
- DEBUG [main] - Cache Hit Ratio [cn.bj.mybatis.model.IUserOperation]: 0.5
- 5
- kksskdk
命中率不小的。