教学来源:
Redis课程介绍导学_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1cr4y1671t?p=1一、Redis 入门
1.认识NoSQL
2.Redis在虚拟机中的安装和开机自启
Redis在虚拟机中安装和配置开机自启-CSDN博客https://blog.csdn.net/qq_69183322/article/details/138168301
3.Redis的数据结构
4.Redis的基本类型
(1) 字符串String类型
(2)哈希Hash类型
(3) 列表List类型
(4)集合Set类型
(5)有序集合类型
二、Redis的Java客户端Jedis
(1)Jedis简单示例
引入依赖:
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.0</version></dependency>
代码示例:
package com.example.redisdemo;import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;import java.util.Map;@SpringBootTest
class RedisDemoApplicationTests {private Jedis jedis;@BeforeEachvoid setUp(){jedis = new Jedis("localhost",6379);
//密码
// jedis.auth("123");jedis.select(0);}@Testvoid testString(){String result = jedis.set("name","张四");System.out.println("result="+result);String name = jedis.get("name");System.out.println("name="+name);}@Testvoid testHash(){jedis.hset("user:1","name","Jack");jedis.hset("user:1","age","21");Map<String,String> map = jedis.hgetAll("user:1");System.out.println(map);}@AfterEachvoid tearDown(){if(jedis !=null){jedis.close();}}}
(2)Jedis连接池
在utils中写Jedis连接池的配置:
package com.example.redisdemo.utils;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public class JedisConnectionFactory {private static final JedisPool jedisPool;static {JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxTotal(8);poolConfig.setMaxIdle(8);poolConfig.setMaxIdle(0);poolConfig.setMaxWaitMillis(1000);jedisPool = new JedisPool(poolConfig,"192.168.92.136",6379,1000,"123456");}public static Jedis getJedis(){return jedisPool.getResource();}
}
在test中进行测试:
package com.example.redisdemo;import com.example.redisdemo.utils.JedisConnectionFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;import java.util.Map;@SpringBootTest
class RedisDemoApplicationTests {private Jedis jedis;@BeforeEachvoid setUp(){
// jedis = new Jedis("192.168.92.136",6379);jedis = JedisConnectionFactory.getJedis();jedis.auth("123456");jedis.select(0);}@Testvoid testString(){String result = jedis.set("name","张四");System.out.println("result="+result);String name = jedis.get("name");System.out.println("name="+name);}@Testvoid testHash(){jedis.hset("user:1","name","Jack");jedis.hset("user:1","age","21");Map<String,String> map = jedis.hgetAll("user:1");System.out.println(map);}@AfterEachvoid tearDown(){if(jedis !=null){jedis.close();}}}
三、SpringDataRedis
(1)快速入门实例:
1.引入依赖
<!-- Redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
<!-- 连接池依赖--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>
2.在application.yml中进行配置
server:port: 8080spring:redis:host: 192.168.92.136port: 6379password: 123456lettuce:pool:max-active: 8 #最大连接max-idle: 8 #最大空闲连接min-idle: 0 #最小空闲连接max-wait: 100 #连接等待时间
3.Test中编写示例
package com.example.redisdemo;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;@SpringBootTest
class RedisDemoApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Testvoid testString(){redisTemplate.opsForValue().set("name","小龙");Object name = redisTemplate.opsForValue().get("name");System.out.println("name="+name);}}
(2)RedisTemplate序列化
配置序列化 config.RedisConfig
package com.example.redisdemo.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){RedisTemplate<String,Object> template = new RedisTemplate<>();//设置连接工厂template.setConnectionFactory(connectionFactory);//创建Json序列化工具GenericJackson2JsonRedisSerializer jsonRedisSerializer =new GenericJackson2JsonRedisSerializer();//设置Key的序列化template.setKeySerializer(RedisSerializer.string());template.setHashKeySerializer(RedisSerializer.string());//设置Value的序列化template.setValueSerializer(jsonRedisSerializer);template.setHashValueSerializer(jsonRedisSerializer);return template;}}
创建实体 pojo.User
package com.example.redisdemo.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {private String name;private Integer age;
}
在Test中编写测试用例
package com.example.redisdemo;import com.example.redisdemo.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;@SpringBootTest
class RedisDemoApplicationTests {@Autowiredprivate RedisTemplate<String,Object> redisTemplate;@Testvoid testString(){redisTemplate.opsForValue().set("name","小龙");Object name = redisTemplate.opsForValue().get("name");System.out.println("name="+name);}@Testvoid testSaveUser(){redisTemplate.opsForValue().set("user:100",new User("小虎",21));User user = (User) redisTemplate.opsForValue().get("user:100");System.out.println("user="+user);}}
报错可能没有引入Jackson依赖
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
(3)序列化的另一种方式StringRedisTemplate
示例代码:
package com.example.redisdemo;import com.example.redisdemo.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;import java.util.Map;@SpringBootTest
class RedisDemoApplicationTests {@Autowiredprivate StringRedisTemplate stringRedisTemplate;//Json工具private static final ObjectMapper mapper = new ObjectMapper();@Testvoid testStringTemplate() throws JsonProcessingException{User user = new User("小明",20);//手动序列化String json = mapper.writeValueAsString(user);stringRedisTemplate.opsForValue().set("user:200",json);String val = stringRedisTemplate.opsForValue().get("user:200");//反序列化User user1 = mapper.readValue(val, User.class);System.out.println("user1:"+user1);}@Testvoid testHash(){stringRedisTemplate.opsForHash().put("user:400","name","小胡");stringRedisTemplate.opsForHash().put("user:400","age","21");Map<Object,Object> entries = stringRedisTemplate.opsForHash().entries("user:400");System.out.println("entries:"+entries);}}