Redis的使用及缓存

java使用Redsi

1.下载jar包:https://mvnrepository.com/artifact/redis.clients/jedis
2.先配置好redsi的数据,然后在测试,方法如下:

public static Jedis getJedis() {
		// redis连接的ip
		Jedis jedis = new Jedis("192.168.0.243");
		// redis密码
		jedis.auth("ps123456");
		return jedis;
	}
	
	@org.junit.Test
	public void keys() {
		Set<String> keys = getJedis().keys("*");
		for (String string : keys) {
			System.out.println(string);
		}
	}

Mybatis一级和二级缓存

Mybatis一级缓存为:sqlSession级别的缓存 默认开启 相同的sqlsession对象 查询相同条件的结果时 存在一级缓存只会查询一次,sqlSession关闭后缓存失效 调用cleanCache后 缓存被清除,执行过增删改后缓存会被清除
二级缓存为:sqlSessionFactory级别的缓存 默认不开启
开启方式为如下:

<settings>
        <setting name="cacheEnabled" value="true"/>
</settings>

在映射文件中添加cache标签 默认使用LRU算法 也可以不适用默认 的配置

     <cache eviction="FIFO"  //回收策略为先进先出
      flushInterval="60000" //自动刷新时间60s
      size=“512” //最多缓存512个引用对象
     	readOnly="true"></cache> //只读

用redis做缓存:

1.下载jar包:https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-redis

2.配一个redis.properties文件,配好你的redis连接的ip,端口,密码:

host:192.168.0.243
port:6379
password:ps123456

3.映射文件中配置:

														<!-- 连接redis.properties文件获取Redis的信息 -->
<cache eviction="FIFO" flushInterval="60000" size="512" type="org.mybatis.caches.redis.RedisCache"/> 

4.序列化实体类:

import java.io.Serializable;

public class Emp implements Serializable{
	
	private int empno;
	private String ename;
	private String job;
	private int sal;
	
	
	public int getSal() {
		return sal;
	}
	public void setSal(int sal) {
		this.sal = sal;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public int getEmpno() {
		return empno;
	}
	public void setEmpno(int empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	@Override
	public String toString() {
		return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", sal=" + sal + "]";
	}
}

5.测试类:将数据缓存到redis查询中不再连接数据库查询:

// 获取SqlSession对象
	public static SqlSession getSession() {
		String resource = "config.xml";
		InputStream resourceAsStream = TestMybatis.class.getResourceAsStream(resource);
		// session工厂 负责产生会话
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
		// 会话就是打开了和数据库的链接
		SqlSession openSession = sqlSessionFactory.openSession();
		return openSession;
	}
	
	/**1级缓存
	 * 		同一个session对象 调用多次查询相同的名字时获取的数据 发起一次sql语句
	 * 2级缓存
	 * 		同一个SqlSeessionFactory 不同的session对象
	 *	简单测试
	 */
	@Test
	public void test() {
		SqlSession session = getSession();
		EmpMapper mapper = session.getMapper(EmpMapper.class);
		List<Emp> queryEmp = mapper.queryEmp("%小%");
		List<Emp> queryEmp1 = mapper.queryEmp("%小%");
		
		System.out.println(queryEmp==queryEmp1);
		session.close();
	}
	
	@Test
	public void test1() {
		String resource = "config.xml";
		InputStream resourceAsStream = TestMybatis.class.getResourceAsStream(resource);
		// session工厂 负责产生会话
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
		// 会话就是打开了和数据库的链接
		SqlSession openSession = sqlSessionFactory.openSession();
		SqlSession openSession1 = sqlSessionFactory.openSession();
		
		EmpMapper mapper = openSession.getMapper(EmpMapper.class);
		List<Emp> queryEmp = mapper.queryEmp("%小%");
		openSession.commit();
		// 二级缓存必须要在openSession被关闭的情况下,当openSession被关闭后它会将数据存在SqlSessionFactory里面
		// 二级缓存默认不开启,要在映射xml文件中加上标签<cache>
		
		EmpMapper mapper1 = openSession1.getMapper(EmpMapper.class);
		List<Emp> queryEmp1 = mapper1.queryEmp("%小%");
		
		System.out.println(queryEmp == queryEmp1);
	}

猜你喜欢

转载自blog.csdn.net/wufewu/article/details/84204191