MyBatis (六)结合多线程实现模拟缓存

版权声明:本文为博主原创文章,允许转载,转载时请说明出处 https://blog.csdn.net/qq_41541619/article/details/79977340

模拟多线程实现一级缓存

设置两个线程,均用于实现查询功能,一级缓存默认打开,不打开二级缓存

package test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSessionManager;

import mapper.AuthorMapper;
import po.Author;

/**
 * 结合多线程实现模拟一级缓存
 * 设置了两个线程,两个线程均用于查询,查询的是不同的内容
 * @author baka
 */
public class test1{
	private static SqlSession session;

	public static SqlSession getSqlSessionTest() {
		if (session == null) {
			String resource = "SqlMapperConfig.xml";
			InputStream iStream = test1.class.getClassLoader().getResourceAsStream(resource);
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(iStream);
			session = factory.openSession();
		}
		return session;
	}

	//从线程
	static class Run implements Runnable{
		@Override
		public void run() {
			try {
				//获取到操作author表的映射类
				AuthorMapper authorMapper = getSqlSessionTest().getMapper(AuthorMapper.class);  
				Author author = authorMapper.selectByPrimaryKey(1);
				System.out.println("-----------------------------------------------");
				System.out.println("从线程:" + author.toString());
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	//主线程
	public static void main(String[] args) throws InterruptedException {
		Run run = new Run();
		for (int i = 0; i < 10; i++) {
			Thread thread = new Thread(run);
			//获取到操作author表的映射类
			AuthorMapper authorMapper = getSqlSessionTest().getMapper(AuthorMapper.class);  
			Author author1 = new Author(1, "baka", i+"123", null, null);
			authorMapper.updateByPrimaryKey(author1);
			session.commit();
			Author author = authorMapper.selectByPrimaryKey(2);
			System.out.println("-----------------------------------------------");
			System.out.println("主线程:" + author.toString());
			thread.sleep(10);
			thread.start();
		}
	}
}

模拟多线程实现二级缓存

设置两个线程,均用于实现查询功能,打开二级缓存

注意,查询之后一定要将一级缓存刷入至二级缓存,不然命中率将为0

package test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSessionManager;

import mapper.AuthorMapper;
import po.Author;

/**
 * 结合多线程实现模拟二级缓存,设置了两个线程,
 * 两个线程均用于查询
 * @author baka
 */
public class test2 {
	private static String resource = "SqlMapperConfig.xml";
	private static InputStream iStream = test1.class.getClassLoader().getResourceAsStream(resource);
	private static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(iStream);

	// 从线程
	static class Run implements Runnable {
		@Override
		public void run() {
			try {
				SqlSession session = factory.openSession();
				AuthorMapper authorMapper = session.getMapper(AuthorMapper.class);
				Author author = authorMapper.selectByPrimaryKey(1);
				session.close();  //一级缓存刷入二级缓存
				System.out.println("从线程:" + author.toString());
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("--------------------------------------");
		}
	}

	// 主线程
	public static void main(String[] args) throws InterruptedException {
		Run run = new Run();
		for (int i = 0; i < 10; i++) {
			Thread thread = new Thread(run);
			SqlSession session = factory.openSession();
			AuthorMapper authorMapper = session.getMapper(AuthorMapper.class);
			Author author = authorMapper.selectByPrimaryKey(2);
			session.close();   //一级缓存刷入二级缓存
			System.out.println("主线程:" + author.toString());
			thread.sleep(10);
			thread.start();
			System.out.println("--------------------------------------");
		}
	}
}

阅读更多

没有更多推荐了,返回首页