说明:在SpringBoot2.x之后,原来使用jedis被替换成了letttuce!
jedis:采用的时直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用jedis pool连接池!更像bio模式
lettrce:采用netty,实例可以在多个线程中共享,不存在线程不安全的情况!可以减少线程数,更像nio模式
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/iwmstest?characterEncoding=UTF-8
username: root
password: 123456
# 配置redis
redis:
host: 192.168.184.135
port: 6379
mybatis:
# config-location: classpath:sqlMapper.xml
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
type-aliases-package: com.model
redisTemplate:操作数据类型api和指令是相同的
//opsForValue:操作字符串
//opsForList:操作list
//opsForSet:操作set
//opsForZSet:操作zset
//opsForHash:操作hash
@SpringBootTest(classes = DataSourceStarter.class)
@RunWith(SpringRunner.class)
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void contextLoads(){
//opsForValue:操作字符串
//opsForList:操作list
//opsForSet:操作set
//opsForZSet:操作zset
//opsForHash:操作hash
System.out.println(redisTemplate.opsForValue().get("user"));
}
}
除了基本的操作,我们常用的方法都可以直接通过redisTemplate操作,比如事务和基本的crud
@Test
public void contextLoads(){
//获取redis的连接对象
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.flushDb();
}
关于对象的保存
直接保存对象必须把对象进行序列化
@Test
public void test(){
//真实的开发一般都使用json来传递对象
User user = new User("zzq", 25);
try {
String userJson = new ObjectMapper().writeValueAsString(user);
redisTemplate.opsForValue().set("user",userJson);
System.out.println(redisTemplate.opsForValue().get("user"));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
@Configuration
public class RedisConfig {
//自定义的RedisTemplate
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
//我们为了自己开发方便,一般直接使用
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
//配置具体的序列化方式
//json序列化配置
Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jsonRedisSerializer.setObjectMapper(om);
// String的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
//hash的key也采用string的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
//value序列化方式采用jackson
template.setValueSerializer(jsonRedisSerializer);
//hash的value序列化方式采用jackson
template.setHashKeySerializer(jsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
测试
@SpringBootTest(classes = DataSourceStarter.class)
@RunWith(SpringRunner.class)
public class RedisTest {
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate redisTemplate;
@Test
public void contextLoads(){
redisTemplate.opsForValue().set("username","zzq");
}
@Test
public void test(){
//真实的开发一般都使用json来传递对象
User user = new User("zzq", 25);
redisTemplate.opsForValue().set("user",user);
// System.out.println(redisTemplate.opsForValue().get("user"));
}
}