Spring Boot整合Reids
0 条评论终于还是用了Spring Boot,主要跑定时任务。上一次写的那个定时任务就是要用的。这次主要是在定时任务中更新缓存(Redis)数据。
添加依赖
当前版本是SpringBoot 1.5.4.RELEASE
1 | <!-- Redis --> |
这里只需要添加这一个依赖就好了,点进去可以看到相关依赖的信息:1
2
3
4
5
6
7
8
9
10
11
12
13
14<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>
调整配置
application.properties增加参数
1 | # REDIS (RedisProperties) |
使用
1 | import org.springframework.beans.factory.annotation.Autowired; |
然后通过这个类操作Redis,通过阅读源码我们发现,该类是RedisTemplate的子类,具有以下数据操作对象:1
2
3
4
5
6
7 // cache singleton objects (where possible)
private ValueOperations<K, V> valueOps;//操作String类型的键值
private ListOperations<K, V> listOps;//操作List集合类型的键值
private SetOperations<K, V> setOps;//操作Set集合类型的键值
private ZSetOperations<K, V> zSetOps;//操作有序Set集合类型的键值
private GeoOperations<K, V> geoOps;//操作GEO类型的键值
private HyperLogLogOperations<K, V> hllOps;//这个没有关注
比如
操作String类型的键值可以:stringRedisTemplate.opsForValue().set(key,value);
操作Zset集合类型的键值可以:stringRedisTemplate.opsForZSet().add(key,value,double);
这里需要注意,不要使用import org.springframework.data.redis.core.RedisTemplate;这个类,虽然上面的StringRedisTemplate是其子类,但是它操作的redis数据会产生一些额外的二进制数值,如果其他项目读取该数据会略有不便。
简单记录,有待深入挖坑!欢迎留言交流!
- 本文链接:http://www.icnws.com/2017/146-spring-boot-with-redis/
- 版权声明:本文由 简成 创作,采用 CC BY 3.0 CN协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。交流沟通请加QQ:1503904712 或 邮件:dujc1018@gmail.com