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

发布于:2025-05-25 ⋅ 阅读:(20) ⋅ 点赞:(0)

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使用方式_环境搭建

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当中的数据。

操作字符串类型的数据:

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

/**
     * 测试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");//校验组

    }

操作哈希类型的数据:

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

   @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-店铺营业状态设置_代码开发

三个接口实现如下:

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);
    }
}

测试成功!


网站公告

今日签到

点亮在社区的每一天
去签到