1 在 Java 中操作 Redis
1.1 Redis 的 Java 客户端
- Jedis(官方推荐,且命令语句同 redis 命令)
- Lettuce(底层基于 Netty 多线程框架实现,性能高效)
- Spring Data Redis(对 Jedis 和 Lettuce 进行了高度封装,使用更加简单)
【注】Spring Data Redis 是 Spring 的一部分,对 Redis 底层开发包进行了高度封装,在 Spring 项目中,可以使用 Spring Data Redis 进行简化操作,本项目使用此
1.2 Spring Data Redis 的使用方式
操作步骤:
① 导入 Spring Data Redis 的 maven 坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
② 配置 Redis 数据源
spring:
redis:
host: localhost
port: 6379
password: 123456
database: 0
③ 编写配置类,创建 RedisTemplate 对象
@Configuration
@Slf4j
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
log.info("开始创建redis模板对象...");
RedisTemplate redisTemplate = new RedisTemplate();
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置redis (键)的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
④ 通过 RedisTemplate 对象操作 Redis
public class SpringDataRedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedisTemplate() {
System.out.println(redisTemplate);
ValueOperations valueOperations = redisTemplate.opsForValue();
HashOperations hashOperations = redisTemplate.opsForHash();
ListOperations listOperations = redisTemplate.opsForList();
SetOperations setoperations = redisTemplate.opsForSet();
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
}
/**
*操作字符串类型的数据
*/
@Test
public void testString() {
// redis 里的 string 操作命令:set/ get/ setex:设置有效期/ setnx:如果存在key,插入失败
redisTemplate.opsForValue().set("city","北京");
String city = (String) redisTemplate.opsForValue().get("city");
System.out.println(city);
redisTemplate.opsForValue().set("code",1234,3, TimeUnit.MINUTES);
redisTemplate.opsForValue().setIfAbsent("lock","1");
redisTemplate.opsForValue().setIfAbsent("lock","2");
}
/**
* 操作哈希类型数据
*/
@Test
public void teshHash() {
//hset hget hdel hkeys hvals
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("101","name","tom");
hashOperations.put("101","age","20");
String name = (String) hashOperations.get("101","name");
System.out.println(name);
Set keys = hashOperations.keys("101");
System.out.println(keys);
List values = hashOperations.values("101");
System.out.println(values);
hashOperations.delete("101","age");
}
/**
* 操作列表类型的数据
*/
@Test
public void testList() {
//lpush lrange rpop llen
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPushAll("mylist","a","b","c");
listOperations.leftPush("mylist","d");
List mylist = listOperations.range("mylist",0,-1);
System.out.println(mylist);
listOperations.rightPop("mylist");
Long size = listOperations.size("mylist");
System.out.println(size);
}
/**
* 操作集合类型的数据
*/
@Test
public void testSet() {
//sadd smembers scard sinter sunion srem
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1","a","b","c","d");
setOperations.add("set2","a","b","x","y");
Set members = setOperations.members("set1");
System.out.println(members);
Long size = setOperations.size("set1");
System.out.println(size);
Set intersect = setOperations.intersect("set1","set2");
System.out.println(intersect);
Set union = setOperations.union("set1","set2");
System.out.println(union);
setOperations.remove("set1","a","b");
}
/**
* 操作有序集合类型的数据
*/
@Test
public void testZset() {
//zadd zrange zincrby zrem
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset1","a",10);
zSetOperations.add("zset2", "b", 12);
zSetOperations.add("zset2", "c", 9);
Set zset1 = zSetOperations.range("zset1" ,0 , -1);
System.out.println(zset1);
zSetOperations.incrementScore("zset1","c",10);
zSetOperations.remove("zset1","a","b");
}
/**
* 通用命令操作
*/
@Test
public void testCommon() {
//keys exists type del
Set keys = redisTemplate.keys("*");
System.out.println(keys);
Boolean name = redisTemplate.hasKey("name");
Boolean set1 = redisTemplate.hasKey("set1");
for (Object key :keys) {
DataType type = redisTemplate.type(key);
System.out.println(type.name());
}
redisTemplate.delete("mylist");
}
}
2 店铺营业状态设置
2.1 需求分析
产品原型
接口设计
- 设置营业状态
- 管理端查询营业状态
- 用户端查询营业状态
【项目约定】
管理端发出的请求,统一使用 /admin 作为前缀;
用户端发出的请求,统一使用 /user 作为前缀
营业状态数据存储方式:
基于 Redis 的字符串来进行存储 ,key:SHOP_STATUS ;value:1/0
【项目约定】 1表示营业,0表示打烊
2.2 代码分析
admin.ShopController.java
@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {
public static final String KEY = "SHOP_STATUS";
@Autowired
private RedisTemplate redisTemplate;
/**
* 设置店铺的营业状态
* @param status
* @return
*/
@PutMapping("/{status}")
@ApiOperation("设置店铺的营业状态")
public Result setStatus(@PathVariable Integer status) {
log.info("设置店铺的营业状态为:{}",status == 1 ? "营业中" : "打烊中");
redisTemplate.opsForValue().set(KEY, status);
return Result.success();
}
/**
* 获取店铺的营业状态
* @return
*/
@GetMapping("/status")
@ApiOperation("获取店铺的营业状态")
public Result<Integer> getStatus() {
Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
log.info("获取到店铺的营业状态为:{}",status == 1 ? "营业中" : "打烊中");
return null;
}
}
user.ShopController.java
@RestController("userShopController")
@RequestMapping("/user/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {
public static final String KEY = "SHOP_STATUS";
@Autowired
private RedisTemplate redisTemplate;
/**
* 获取店铺的营业状态
* @return
*/
@GetMapping("/status")
@ApiOperation("获取店铺的营业状态")
public Result<Integer> getStatus() {
Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
log.info("获取到店铺的营业状态为:{}",status == 1 ? "营业中" : "打烊中");
return null;
}
}