Redis入门
在cmd中启动server和client
-p -h来连接指定的地址的数据库
Redis常用的数据类型
Hash可以存储对象
视频播放投票排行榜,使用有序集合
字符串操作命令
在manager的控制台中小小的尝试一下
Hash操作命令
列表操作命令(数据结构中的队列)
先进先出?QUEUE?
集合操作命令
有序集合命令(视频播放投票排行榜)
通用命令
如何在java中使用Redis(Spring Data Redis)
在server里面配置yml文件进行redis的host和port(dev是用于隐藏真实的配置值)
spring:
profiles:
active: dev
main:
allow-circular-references: true
datasource:
druid:
driver-class-name: ${sky.datasource.driver-class-name}
url: jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: ${sky.datasource.username}
password: ${sky.datasource.password}
redis:
host: ${sky.redis.host}
port: ${sky.redis.port}
database: ${sky.redis.database}
初始化RedisConfiguration (序列化器)
@Slf4j
@Configuration
public class RedisConfiguration {
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
log.info("开始创建redis模板对象。。。");
RedisTemplate redisTemplate = new RedisTemplate();
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置redis的key序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
测试Redis命令
String命令
@SpringBootTest
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(){
//set get setex setnx
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("code","1");
}
hash的测试
@Test
public void testHash(){
//hset hget hdel hkeys hvals
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("100","name","tom");
hashOperations.put("100","age","20");
String name = (String)hashOperations.get("100", "name");
System.out.println(name);
Set keys = hashOperations.keys("100");
System.out.println(keys);
List value = hashOperations.values("100");
System.out.println(value);
hashOperations.delete("100","age");
}
店铺营业状态设置变化
设置营业状态(路径参数)
管理端查询营业接口(Requestparam )
ShopController(使用Redis直接一个Controller就搞定了)
@RestController("adminShopController")
@Slf4j
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
public class ShopController {
public static final String Key = "SHOP_STATUS" ;
// @Autowired
// ShopService shopService = new ShopService();
@Autowired
private RedisTemplate redisTemplate;
@PutMapping("/{status}")
@ApiOperation("改变店铺营业状态")
public Result setStatus(@PathVariable Integer status){
log.info("设置店铺状态为:{}",status);
// shopService.setStatus(status);
redisTemplate.opsForValue().set(Key,status);
return Result.success();
}
@GetMapping("/status")
@ApiOperation("获取店铺营业状态")
public Result<Integer> getStatus(){
Integer shopStatus = (Integer)redisTemplate.opsForValue().get(Key);
return Result.success(shopStatus);
}
}
更改SWAGGER的接口区别用户和管理
@Bean
public Docket docket() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("苍穹外卖项目接口文档")
.version("2.0")
.description("苍穹外卖项目接口文档")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("管理端接口")
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin")) //指定我们扫描的包去生成接口
.paths(PathSelectors.any())
.build();
return docket;
}
.groupName(“管理端接口”)
.apis(RequestHandlerSelectors.basePackage(“com.sky.controller.admin”))