引入依赖
compile 'org.springframework.boot:spring-boot-starter-data-redis'
使用 Redis 有两种方法
- Jedis
- Jedis jedis = new Jedis("localhost");
- RedisTemplate
- @Autowired
- private RedisTemplate redisTemplate;
如果使用 RedisTemplate 的话, 要在 application.properties 中配置信息, 这里我使用 Jedis 比较简单
Redis 的自动配置
在 application.properties 文件下
- #Redis 的 springboot 的自动配置
- # Redis 数据库索引 (默认为 0)
- spring.Redis.database=0
- # Redis 服务器地址
- spring.Redis.host=127.0.0.1
- # Redis 服务器连接端口
- spring.Redis.port=6379
- # Redis 服务器连接密码 (默认为空)
- spring.Redis.password=
- # 连接池最大连接数 (使用负值表示没有限制)
- spring.Redis.pool.max-active=8
- # 连接池最大阻塞等待时间 (使用负值表示没有限制)
- spring.Redis.pool.max-wait=-1
- # 连接池中的最大空闲连接
- spring.Redis.pool.max-idle=8
- # 连接池中的最小空闲连接
- spring.Redis.pool.min-idle=0
- # 连接超时时间 (毫秒)
- spring.Redis.timeout=0
Jedis 使用
- package com.test.booleanjava.helloRS.util;
- import Redis.clients.jedis.Jedis;
- /**
- * @author booleanjava
- * Date: 2019/7/2 19:48
- * description:Redis 的工具类
- */
- public class RedisUtil {
- static Jedis jedis = new Jedis("localhost");
- /**
- * 插入 key, 如果存在就更新
- * @param key
- * @param value
- * @return
- */
- public static String set(String key, String value){
- return jedis.set(key, value);
- }
- /**
- * 获取 key 的值
- * @param key
- * @return
- */
- public static String get(String key) {
- return jedis.get(key);
- }
- /**
- * 删除 key
- * @param key
- * @return
- */
- public static Long del(String key){
- return jedis.del(key);
- }
- /**
- * 设置一个有过期时间的 key(秒)
- * @param key
- * @param seconds
- * @param value
- * @return
- */
- public static String setex(final String key, final int seconds, final String value){
- return jedis.setex(key, seconds, value);
- }
- /**
- * 如果不存在就执行操作, 用作简单分布式锁
- *
- * @param key
- * @param value
- * @return true 表示执行, false 表示没有执行
- */
- public static Boolean setnx(final String key, final String value){
- return jedis.setnx(key, value) == 1;
- }
- }
RedisTemplates 使用
- package com.test.booleanjava.helloRS.util;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.Redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
- /**
- * @author boolean
- * Date: 2019/7/2 19:48
- * description:
- */
- @Component
- public class Redisplus {
- @Autowired
- private RedisTemplate redisTemplate;
- public void set(String key, String value){
- redisTemplate.opsForValue().set(key, value);
- }
- }
测试
- package com.test.booleanjava.helloRS.controller;
- import com.test.booleanjava.helloRS.entity.User;
- import com.test.booleanjava.helloRS.util.Redisplus;
- import com.test.booleanjava.helloRS.service.IUserService;
- import com.test.booleanjava.helloRS.util.RedisUtil;
- import com.test.base.core.util.LogUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.Redis.core.RedisTemplate;
- import org.springframework.data.Redis.serializer.RedisSerializer;
- import org.springframework.data.Redis.serializer.StringRedisSerializer;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.Web.bind.annotation.RestController;
- import java.util.Date;
- /**
- * @author booleanjava
- * Date: 2019/7/2 19:48
- * description:
- */
- @RestController
- @RequestMapping("/helloRS/redisHello")
- public class RedisHello {
- private final static Logger logger = LoggerFactory.getLogger(RedisHello.class);
- private final static String USERKEY = "com.test.booleanjava.helloRS.controller.setex";
- private final static String LOCKKEY = "com.test.booleanjava.helloRS.controller.lock";
- @Autowired
- private IUserService iUserService;
- @Autowired
- private Redisplus redisplus;
- @Autowired
- private RedisTemplate redisTemplate;
- RedisSerializer redisSerializer =new StringRedisSerializer();
- @RequestMapping("/hello")
- public String hello(){
- LogUtil.info("redis 的展示:[{}]", redisTemplate);
- return "hello, redis";
- }
- @RequestMapping("/set")
- public String set(){
- Date date = new Date();
- redisTemplate.setKeySerializer(redisSerializer);
- redisTemplate.opsForValue().set("q", "1");
- redisTemplate.opsForValue().get("q");
- System.out.println(redisTemplate.opsForValue().get("q"));
- RedisUtil.set("a1", String.valueOf(1));
- logger.info("redis 的展示:[{}]", redisTemplate);
- return "hello, set 一下 redis";
- }
- @RequestMapping("/setex")
- public String setex( ){
- // String key = "1min";
- // int seconds = 10;
- // String value = "陈";
- // RedisUtil.setex(key, seconds, value);
- // String rs = RedisUtil.get(key);
- // logger.info("获取的值:[{}]", rs);
- String value = RedisUtil.get(USERKEY);
- if (value != null) {
- logger.info("缓存的 user 值:[{}]", value);
- return value;
- }
- User user = iUserService.query().eq("name", "chen").one();
- logger.info("user 的值:[{}]",user.toString());
- if (user != null ) {
- RedisUtil.setex(USERKEY, 60, user.toString());
- }
- return "hello,booleanjava, 设置了有时限的 key";
- }
- @RequestMapping("/del")
- public String del(String key) {
- redisTemplate.delete(key);
- return "hello, del 一下 redis";
- }
- /**
- * 做分布锁,
- * 先加锁, 写业务, 最后解锁
- * @return
- */
- @RequestMapping("/lock")
- public String lock() {
- // 加锁
- RedisUtil.setnx(LOCKKEY,LOCKKEY);
- // 写业务代码, 一人我饮酒醉
- // 解锁
- RedisUtil.del(LOCKKEY);
- return "hello, lock 一下 redis";
- }
- }
深入
背景
互联网公司大部分通常使用 myslq 作为数据库存储数据, 但是 MySQL 存数据是以影响 IO 为代价的, 所以 MySQL 是系统的常见瓶颈, 为解决这个问题, Redis 这种非关系型数据库就出现了, 存在即合理. Redis 喜欢在内存操作, 比 MySQL 在磁盘瞎忙高效多了, 因此深受人们喜爱.
数据结构
Redis 有五种数据结构
String 字符串
Hash 哈希
List 列表
Set 集合
Sorted Set
最常用的就是 String 类型, 通常使用它做缓存, 减轻直接访问数据库的压力. Hash 的话可以用来做用户 id,List 可以用来做粉丝列表, Set 的话可以做共同好友, Sorted Set 可以做排行榜.
分布式锁
Redis 处理上面列举的例子, 还有就是可以做分布式锁, 在分布式系统中, 接口面临的是多进程多线程访问, 如果依赖 java 的锁是不能解决问题的, 因为进程之间不共享内存; 利用数据库加锁又显得笨重, 因此还得用 Redis 来加锁. Redis 怎么加锁, 主要还是利用 setnx 命令, 该命令作用是如果 key 存在就不执行操作, 不存在的话就设置 value, 这种特性就是为锁打造的啊.
来源: http://www.bubuko.com/infodetail-3119644.html