springboot整合redis_spring

1.redis简介

redis是一个支持key-value的数据库,数据全部在内存中处理,在在一定时间间隔中将数据固化到磁盘。因为是内存操作,所以速度特别快。(这里我们主要介绍redis作为缓存使用)

总结一下他有以下特点:


  1. 速度快,Redis能读的速度是110000次/s,写的速度是81000次/s 。
  2. 能够存储String,hash,set,map,zset(有序集合)。且单个value的可以1G。
  3. 功能多,如做缓存(使用最多),消息队列等。转载一篇应用场景
  4. 可以配置集群。
  5. 原子性。

2.为什么使用redis做缓存?

因为可以提高效率,当客户端多次访问同一个接口时,会查询同一个sql多次,如果这个sql比较慢就会使用户体验大大降低。那么我们把上一次查询出的结果放于缓存中,接下来我们直接从缓存中得到数据就会大大提供速度。

缺点:容易被物理环境所影响。

3.springboot使用redis存储数据

首先我们先安装redis。具体不多介绍。然后也可以安装图形界面。这样可以使操作更加的方便。这里我使用的是RedisDesktopManager。

1.修改pom文件

然后我们创建一个spring boot项目。然后在pom中加入redis依赖。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.新增配置类

然后需要配置类序列化对象。(如果没有存储的是乱码)

@Configuration
public class RedisConfig {

@Autowired
private RedisTemplate redisTemplate;

@Bean
public RedisTemplate redisTemplateInit() {
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// 设置序列化Key的实例化对象
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 设置序列化Value的实例化对象
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}

3.修改application.yml

然后在配置application.properties连接redis。

spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=

如果想看到sql,还需要在application.properties中配置以下代码。

#指定dao所在位置
logging.level.com.example.redis.dao=debug

4.controller实现业务

之后就可以对redis进行存取了,这里我们使用RedisTemplate ,RedisTemplate 封装了对redis操作的方法。

public class RedisController {

Map<String, City> cityMap = new HashMap<String, City>();
@Autowired
private RedisTemplate redisTemplate;

//增删改查String类型 为json类型
@RequestMapping("/insertjson")
@ResponseBody
public void insertData() {
ValueOperations<String, City> operations = redisTemplate.opsForValue();
String key = "city_" + 66;
boolean hasKey = redisTemplate.hasKey(key);//判断是否有
if (hasKey) {
City city = operations.get(key); //查询
//operations.getOperations().delete(key);//删除
return;
}
//再次赋值就可以修改
City entity = new City();
entity.setId((long) 1);
entity.setDescription("111111");
entity.setProvinceId((long) 111111);
// 插入缓存
operations.set(key, entity, 10, TimeUnit.SECONDS);
}

//增删改查hash
@RequestMapping("/insertmap")
@ResponseBody
public void insertMap() {
HashOperations operations = redisTemplate.opsForHash();
String key = "city_" + 33;
int hashKey = 1;
boolean hasKey = redisTemplate.hasKey(key);// 判断是否有
if (hasKey) {
Map<Integer, String> map1 = new HashMap<>();
operations.get(key, hashKey); // 查询
// operations.getOperations().delete(key);//删除
return;
}
// 修改map在插入就可以
Map<Integer, String> map = new HashMap<>();
map.put(1, "qqq");
map.put(2, "www");
map.put(3, "eee");
map.put(4, "rrr");
operations.putAll(key, map);

}

//增删改查list
@RequestMapping("/insertlist")
@ResponseBody
public void insertList() {
ListOperations operations = redisTemplate.opsForList();
String key = "city_" + 44;
boolean hasKey = redisTemplate.hasKey(key);// 判断是否有
if (hasKey) {
operations.range(key, 0, 3);//查询
operations.getOperations().delete(key);//删除
return;
}
List list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
operations.leftPushAll(key, list);
}

//增删改查set
@RequestMapping("/insertset")
@ResponseBody
public void insertSet() {
SetOperations operations = redisTemplate.opsForSet();
String key = "city_" + 55;
boolean hasKey = redisTemplate.hasKey(key);
if(hasKey) {
operations.members(key);//查询
operations.getOperations().delete(key);//删除
return;
}
operations.add(key, "111");
operations.add(key, "222");
operations.add(key, "333");
}
}