苍穹外卖05 Redis常用命令&&在Java中操作Redis_Spring Data Redis使用方式&店铺营业状态设置

2-8 Redis常用命令

02 02-Redis入门

ctrl+c :快捷结束进程

配置密码:

以后再启动客户端的时候就需要进行密码的配置了。使用-a

在图形化界面中创建链接:

启动成功了。

03 03-Redis常用数据类型

04 04-Redis常用命令_字符串操作命令

05 05-Redis常用命令_哈希操作命令

06 06-Redis常用命令_列表操作命令

07 07-Redis常用命令_集合操作命令

08 08-Redis常用命令_有序集合操作命令

09 09-Redis常用命令_通用命令

2-9 在Java中操作Redis_Spring Data Redis使用方法 操作步骤

02 11-在Java中操作Redis_Spring Data Redis使用方式_环境搭建

java 复制代码
package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfigation {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        log.info("开始创建redis模板对象...");
        RedisTemplate redisTemplate = new RedisTemplate();
        // 设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置key序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}

用这五个对象来操作redis当中的数据。

操作字符串类型的数据:

操作字符串功能测试代码如下:

java 复制代码
/**
     * 测试redis操作字符串
     */
    @Test
    public void testString() {
        //set
        redisTemplate.opsForValue().set("city", "北京");//设定key的值
        //get
        String city = (String) redisTemplate.opsForValue().get("city");//获取key的值
        System.out.println(city);
        // setx
        redisTemplate.opsForValue().set("code", "123",3, TimeUnit.MINUTES);//设定key的值,并且设定过期时间
        // setnx
        redisTemplate.opsForValue().setIfAbsent("lock","1");//设定key的值,如果key不存在则设置成功,如果key已存在则设置失败
        redisTemplate.opsForValue().setIfAbsent("lock","2");//校验组

    }

操作哈希类型的数据:

操作哈希表代码实现如下:

java 复制代码
   @Test
    public void testHash() {

        HashOperations hashOperations =redisTemplate.opsForHash();
        //hset   将哈希表key中的字段field的值设为value
        hashOperations.put("100","name","tom");
        hashOperations.put("100","age","20");
        //hget   获取存储在指定哈希表中指定字段的值
        String name = (String)hashOperations.get("100", "name");
        System.out.println(name);
        //hkey   获取指定哈希表所有字段
        Set keys = hashOperations.keys("100");
        System.out.println(keys);
        //hvals   获取指定哈希表中所有值
        List values = hashOperations.values("100");
        System.out.println(values);
        //hdel   删除指定哈希表字段
        hashOperations.delete("100","age");
    }

其他类型的操作效果大差不差,就是注意好引用参数的形参限制就好了。不过多赘述,用到了多用几次就熟练了。

06 15-店铺营业状态设置_需求分析和设计

由于下方两个接口你只需要调用一个参数status ,又没必要为了这一个参数创建一张表,所以我们可以选择将这个参数创建在redis当中。

07 16-店铺营业状态设置_代码开发

三个接口实现如下:

java 复制代码
package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@ApiOperation("店铺管理")
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置店铺的营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置店铺的营业状态")
    public Result setStatus(@PathVariable Integer status){
        log.info("设置店铺的营业状态为:{}", status==1? "营业中":"打烊中");
        redisTemplate.opsForValue().set("SHOP_STATUS", status);
        return Result.success();
    }

    /**
     *  获取营业状态
     * @return
     */
    @RequestMapping("/status")
    @ApiOperation("获取店铺的营业状态")
    public Result<Integer> getStatus(){
        Integer status = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");
        log.info("获取营业状态为:{}", status==1? "营业中":"打烊中");
        return Result.success(status);
    }
}

package com.sky.controller.user;


import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@ApiOperation("店铺相关接口")
@RestController
@RequestMapping("/user/shop")
@Slf4j
public class ShopController {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 获取店铺的营业状态
     * @return
     */
    @ApiOperation("获取店铺的营业状态")
    @RequestMapping("/status")
    public Result<Integer> getStatus(){
        Integer status = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");
        log.info("获取营业状态为:{}", status==1? "营业中":"打烊中");
        return Result.success(status);
    }
}

测试成功!

相关推荐
三8448 小时前
redis防御加固与安全基线
数据库·redis·安全·应急响应·防御
lhldsg8 小时前
全民健身解决方案小程序开发:从0到1的技术实战
java·数据库·需求分析
jason成都8 小时前
Spring WebFlux 适配达梦新方案|dm‑r2dbc:Netty 异步传输的实验性 R2DBC 驱动
java·后端·spring
SendTomo8 小时前
【技术交流】从0到1构建一个汉字学习平台:汉字吧(hanzi8.com)的技术架构猜想
redis·mysql·nginx·php·个人开发
泡海椒8 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
辰烨chenye8 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode
小羊没烦恼!9 小时前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php
小荷才露尖尖角,早有蜻蜓立上头10 小时前
过滤器的4种创建方式
java
大圣编蚕10 小时前
Java ByteArrayInputStream 详解:从入门到实战
java·开发语言·算法
程序员阿明10 小时前
idea把插件啥的不默认放在C盘
java·ide·intellij-idea