Qing 1 rok pred
rodič
commit
67db12828a

+ 5 - 1
redis-demo/src/main/java/com/sf/controller/RedisController.java

@@ -3,6 +3,7 @@ package com.sf.controller;
 import com.sf.service.RedisService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
@@ -11,8 +12,11 @@ public class RedisController {
     @Autowired
     private RedisService redisService;
 
+    // http://localhost:8080/seckill?userId=111&goodsId=1
     @GetMapping("/seckill")
-    public String seckill() {
+    public String seckill(@RequestParam("userId") String userId,
+                          @RequestParam("goodsId") String goodsId) {
+        redisService.seckill(userId, goodsId);
         return "success";
     }
 

+ 1 - 1
redis-demo/src/main/java/com/sf/service/RedisService.java

@@ -2,6 +2,6 @@ package com.sf.service;
 
 public interface RedisService {
 
-    void seckill();
+    void seckill(String userId,String goodsId);
     void init();
 }

+ 21 - 6
redis-demo/src/main/java/com/sf/service/RedisServiceImpl.java

@@ -5,18 +5,26 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 @Service
-public class RedisServiceImpl implements RedisService{
+public class RedisServiceImpl implements RedisService {
 
     @Autowired
     private RedisUtils redisUtils;
 
     @Override
-    public void seckill() {
-
+    public void seckill(String userId, String goodsId) {
+        // goodsId = 1
+        // 1_stock_num
+        String key = goodsId + "_stock_num";
+        Integer num = (Integer) redisUtils.get(key);
+        if (num > 0) {
+            Long decr = redisUtils.decr(key);
+            System.out.println(decr);
+        }
     }
 
 
-    public void init(){
+    @Override
+    public void init() {
         // 初始化秒杀商品的库存数量
         // stockNum
         // <1,10>    <1,9>
@@ -25,7 +33,14 @@ public class RedisServiceImpl implements RedisService{
         //
         // 1_stock_num  10
         // 2_stock_num  10
-        redisUtils.set("1_stock_num","10");
-        redisUtils.set("2_stock_num","10");
+//        redisUtils.set("1_stock_num", "10");
+        redisUtils.set("1_stock_num", 10L);
+        redisUtils.set("2_stock_num", 10L);
+
+//        Map<String, Object> map = new HashMap<>();
+//        map.put("1", 10L);
+//        map.put("2", 10L);
+//        redisUtils.setAllMap("stock_num", map);
+
     }
 }

+ 30 - 0
redis-demo/src/main/java/com/sf/utils/HttpUtils.java

@@ -0,0 +1,30 @@
+package com.sf.utils;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.time.Duration;
+
+public class HttpUtils {
+
+    public static void main(String[] args) throws Exception {
+//        String url = "http://localhost:8080/init";
+        String url = "http://localhost:8080/seckill?userId=111&goodsId=1";
+        // Get请求
+        // 可以设置http的版本  也可以设置请求的超时时间
+        //  http客户端 类似浏览器一样
+        HttpClient httpClient = HttpClient.newBuilder()
+                .version(HttpClient.Version.HTTP_1_1)
+                .connectTimeout(Duration.ofSeconds(30))
+                .build();
+        // 创建请求参数  url -> uri
+        URI uri = URI.create(url);
+        HttpRequest httpRequest = HttpRequest.newBuilder().uri(uri).build();
+        // 发送请求 接收响应
+        HttpResponse<String> httpResponse = httpClient.send(
+                httpRequest, HttpResponse.BodyHandlers.ofString());
+        String body = httpResponse.body();
+        System.out.println(body);
+    }
+}

+ 19 - 1
redis-demo/src/main/java/com/sf/utils/RedisUtils.java

@@ -4,6 +4,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
+import java.util.Map;
+
 @Component
 public class RedisUtils {
 
@@ -14,7 +16,23 @@ public class RedisUtils {
         return redisTemplate.opsForValue().get(key);
     }
 
-    public void set(String key, String value) {
+    public void set(String key, Object value) {
         redisTemplate.opsForValue().set(key, value);
     }
+
+    public Object getMap(String key, String subKey) {
+        return redisTemplate.opsForHash().get(key, subKey);
+    }
+
+    public Long decr(String key) {
+        return redisTemplate.opsForValue().decrement(key);
+    }
+
+    public void setMap(String key, String subKey, Object subValue) {
+        redisTemplate.opsForHash().put(key, subKey, subValue);
+    }
+
+    public void setAllMap(String key, Map<String, Object> map) {
+        redisTemplate.opsForHash().putAll(key, map);
+    }
 }

+ 13 - 1
redis-demo/src/test/java/com/sf/RedisTests.java

@@ -15,6 +15,19 @@ public class RedisTests {
     @Autowired
     private RedisTemplate redisTemplate;
 
+
+    @Test
+    public void testString() {
+        redisTemplate.opsForValue().set("key11", 10L);
+        redisTemplate.opsForValue().increment("key11");
+        System.out.println(redisTemplate.opsForValue().get("key11"));
+
+        redisTemplate.opsForValue().append("key14", "10");
+        redisTemplate.opsForValue().append("key14", "20");
+        System.out.println(redisTemplate.opsForValue().get("key14"));
+
+    }
+
     @Test
     public void test() {
 //        redisTemplate.opsForValue().set("kk1","vv1");
@@ -40,5 +53,4 @@ public class RedisTests {
     }
 
 
-
 }