📘博客主頁:程序員葵安
🫶感謝大家點贊👍🏻收藏?評論?🏻
一、Redis入門
Redis簡介
Redis是一個基于內存的 key-value 結構數據庫
- 基于內存存儲,讀寫性能高
- 適合存儲熱點數據(熱點商品、咨詢、新聞)
- 企業應用廣泛
二、Redis數據類型
5種常用數據類型
Redis存儲的是key-value結構的數據,其中key是字符串類型,value有5種常用的數據類型:
- 字符串 String :普通字符串,Redis中最簡單的數據類型
- 哈希 hash :也叫散列,類似于Java中的HashMap結構
- 列表 list :按照插入順序排序,可以有重復元素,類似于Java中的LinkedList
- 集合 set :無序集合,沒有重復元素,類似于Java中的HashSet
- 有序集合 sorted set / zset :集合中每個元素關聯一個double類型的分數score,根據分數升序排序,沒有重復元素
三、Redis常用命令
字符串操作命令
- SET key value? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 設置指定key的值
- GET key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?獲取指定key的值
- SETEX key seconds value? ? ? ? ? ??設置指定key的值,并將key的過期時間設為seconds秒
- SETNX key value? ? ? ? ? ? ? ? ? ? ? ? ? 只有在key不存在時設置key的值
哈希操作命令
Redis hash 是一個string類型的field和value的映射表,hash特別適合用于存儲對象,常用命令:
- HSET key field value? ? ? ? ? ? ? ? ? ? 將哈希表key中的字段filed的值設為value
- HGET key field? ? ? ? ? ? ? ? ? ? ? ? ? ? ?獲取存儲在哈希表中指定字段的值
- HDEL key field? ? ? ? ? ? ? ? ? ? ? ? ? ? ?刪除存儲在哈希表中的指定字段
- HKEYS key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取哈希表中所有字段
- HVALS key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取哈希表中所有值
列表操作命令
- LPUSH key value1 [value2]? ? ? ? ? 將一個或多個值插入到列表頭部
- LRANGE key start stop? ? ? ? ? ? ? ? 獲取列表指定范圍內的元素
- RPOP key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 移除并獲取列表最后一個元素
- LLEN key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?獲取列表長度
集合操作命令
- SADD key member1 [member2]? ? ? ? 向集合添加一個或多個成員
- SMEMBERS key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 返回集合中的所有成員
- SCARD key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取集合的成員數? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
- SINTER key1 [key2]? ? ? ? ? ? ? ? ? ? ? ? ? ?返回給定所有集合的交集
- SUNION key1 [key2]? ? ? ? ? ? ? ? ? ? ? ? ? 返回所有給定集合的并集
- SREM key member1 [member2]? ? ? ? 刪除集合中一個或多個成員
有序集合操作命令
- ZADD key score1 member1 [score2 member2]? ? ? ? 向有序集合添加一個或多個成員
- ZRANGE key start stop [WITHSCORES]? ? ? ? ? ? ? ? ?通過索引區間返回有序集合中指定區間內的成員
- ZINCRBY key increment member? ? ? ? ? ? ? ? ? ? ? ? ? ? 有序集合中對指定成員分數加上增量increment
- ZREM key member [member ...]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 移除有序集合中的一個或多個成員
通用命令
- KEYS pattern? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 查找所有符合給定模式(pattern)的key
- EXISTS key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 檢查給定key是否存在
- TYPE key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?返回key所儲存的值的類型
- DEL key? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? 該命令用于在key存在時刪除key
四、Java中操作Redis
Redis的Java客戶端
Redis的Java客戶端很多,常用的幾種:
- Jedis
- Lettuce
- Spring Data Redis
Spring Data Redis 是 Springs 的一部分,對 Redis 底層開發包進行了高度封裝。
在 Spring 項目中,可以使用Spring Data Redis簡化操作
Spring Data Redis使用方式
操作步驟:
- 導入Spring Data Redis的 maven坐標
- 配置Redis數據源
- 編寫配置類,創建RedisTemplate對象
@Configuration @Slf4j 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;} }
- 通過RedisTemplate對象操作Redis
@SpringBootTest public class SpringDataRedisTest {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testRedisTemplate(){System.out.println(redisTemplate);//string數據操作ValueOperations valueOperations = redisTemplate.opsForValue();//hash類型的數據操作HashOperations hashOperations = redisTemplate.opsForHash();//list類型的數據操作ListOperations listOperations = redisTemplate.opsForList();//set類型數據操作SetOperations setOperations = redisTemplate.opsForSet();//zset類型數據操作ZSetOperations zSetOperations = redisTemplate.opsForZSet();} }
五、店鋪營業狀態設置
需求分析接口設計
接口設計:
-
設置營業狀態
-
管理端查詢營業狀態
-
用戶端查詢營業狀態
注:從技術層面分析,其實管理端和用戶端查詢營業狀態時,可通過一個接口去實現即可。因為營業狀態是一致的。但是,本項目約定:
-
管理端發出的請求,統一使用/admin作為前綴。
-
用戶端發出的請求,統一使用/user作為前綴。
因為訪問路徑不一致,故分為兩個接口實現。
代碼開發
根據接口定義創建ShopController的setStatus設置營業狀態方法:
package com.sky.controller.admin;import com.sky.result.Result;
import io.swagger.annotations.Api;
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;@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店鋪相關接口")
@Slf4j
public class ShopController {public static final String KEY = "SHOP_STATUS";@Autowiredprivate 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();}
}
根據接口定義創建ShopController的getStatus查詢營業狀態方法:
/*** 獲取店鋪的營業狀態* @return*/@GetMapping("/status")@ApiOperation("獲取店鋪的營業狀態")public Result<Integer> getStatus(){Integer status = (Integer) redisTemplate.opsForValue().get(KEY);log.info("獲取到店鋪的營業狀態為:{}",status == 1 ? "營業中" : "打烊中");return Result.success(status);}
根據接口定義創建ShopController的getStatus查詢營業狀態方法:
package com.sky.controller.user;import com.sky.result.Result;
import io.swagger.annotations.Api;
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.*;@RestController("userShopController")
@RequestMapping("/user/shop")
@Api(tags = "店鋪相關接口")
@Slf4j
public class ShopController {public static final String KEY = "SHOP_STATUS";@Autowiredprivate RedisTemplate redisTemplate;/*** 獲取店鋪的營業狀態* @return*/@GetMapping("/status")@ApiOperation("獲取店鋪的營業狀態")public Result<Integer> getStatus(){Integer status = (Integer) redisTemplate.opsForValue().get(KEY);log.info("獲取到店鋪的營業狀態為:{}",status == 1 ? "營業中" : "打烊中");return Result.success(status);}
}