Browse Source

redis 秒杀

Qing 1 year ago
parent
commit
7dc2d3a3a0

+ 6 - 0
redis-demo/pom.xml

@@ -27,6 +27,12 @@
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>1.18.30</version>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>

+ 25 - 0
redis-demo/src/main/java/com/sf/controller/RedisController.java

@@ -0,0 +1,25 @@
+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.RestController;
+
+@RestController
+public class RedisController {
+
+    @Autowired
+    private RedisService redisService;
+
+    @GetMapping("/seckill")
+    public String seckill() {
+        return "success";
+    }
+
+    // http://localhost:8080/init
+    @GetMapping("/init")
+    public String init() {
+        redisService.init();
+        return "success";
+    }
+}

+ 23 - 0
redis-demo/src/main/java/com/sf/entity/Goods.java

@@ -0,0 +1,23 @@
+package com.sf.entity;
+
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * 秒杀的是商品
+ * <p>
+ * 秒杀商品表
+ * id 秒杀开始时间  秒杀结束时间  秒杀价格  库存数量
+ * <p>
+ * 订单表
+ * id 商品id  用户id 下单时间
+ */
+@Data
+public class Goods {
+    private Long id;
+    private Date startTime;
+    private Date endTime;
+    private Double price;
+    private Integer stockNum;
+}

+ 17 - 0
redis-demo/src/main/java/com/sf/entity/Order.java

@@ -0,0 +1,17 @@
+package com.sf.entity;
+
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * 订单表
+ * id 商品id  用户id 下单时间
+ */
+@Data
+public class Order {
+    private Long id;
+    private Long goodsId;
+    private Long userId;
+    private Date createTime;
+}

+ 52 - 0
redis-demo/src/main/java/com/sf/leetcode/LRUCache.java

@@ -0,0 +1,52 @@
+package com.sf.leetcode;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * LinkedHashMap是hashmap+双向链表的实现结构
+ * 1)新数据如何加入,使用map,有空间的时候直接存入
+ * 2)查找数据时,如何查找,通过key找到value,如果不存在key,返回-1
+ * 3)链表满的时候,如何淘汰数据,查找最近最少被使用的key
+ *
+ * <1,1><2,2>
+ * get(1)
+ * <3,3>
+ * <1,1><3,3>
+ */
+public class LRUCache extends LinkedHashMap<Integer, Integer> {
+
+    private int capacity;
+    public LRUCache(int capacity) {
+        // 使用父类的构造器  设置容量 负载因子  排序方式(按照读取顺序排序)
+        super(capacity, 0.75f, true);
+        this.capacity = capacity;
+    }
+
+    public int get(int key){
+        // 使用get时 如果找不到数据  可以设置默认返回值  这里是-1
+        return super.getOrDefault(key,-1);
+    }
+
+    public void put(int key,int value){
+        super.put(key,value);
+    }
+
+    // 自定义删除元素的方法
+    @Override
+    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
+        // 如果超出了容量大小 开始删除元素
+        return size() > capacity;
+    }
+
+    public static void main(String[] args) {
+        LRUCache map = new LRUCache(2);
+        map.put(1, 1);
+        map.put(2, 2);
+        System.out.println(map.get(1));
+        map.put(3, 3);
+        System.out.println(map.get(2));
+        map.put(4, 4);
+        System.out.println(map);
+    }
+}

+ 67 - 0
redis-demo/src/main/java/com/sf/leetcode/LRUCache1.java

@@ -0,0 +1,67 @@
+package com.sf.leetcode;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Queue;
+
+/**
+ * tangzhenliang
+ */
+public class LRUCache1 {
+
+    private Queue<Integer> queue;  //  利用队列 先进先出 特点  符合LRU  最长时间未使用 要求
+    private Map<Integer, Integer> map;
+    private int capacity;
+
+    public LRUCache1(int capacity) {
+        this.capacity = capacity;
+        queue = new LinkedList<>();
+        map = new HashMap<>();
+    }
+
+    public int get(int key) {
+        if (map.containsKey(key)) {
+            queue.remove(key);    //  把之前的移除
+            queue.offer(key);     //  重新放入队列   在队列尾部
+            return map.get(key);
+        } else {
+            return -1;
+        }
+    }
+
+    public void put(int key, int value) {
+        if (map.containsKey(key)) {
+            queue.remove(key);     //  如果存在  需要更改  则移除   重新放入队列尾部
+        }
+        else if (map.size() >= capacity) {
+            int key1 = queue.poll();    // 超出容量   则队列弹出  队列最前面的  则是长期未使用的
+            map.remove(key1);
+        }
+        map.put(key, value);
+        queue.offer(key);
+    }
+
+    @Override
+    public String toString() {
+        return "LRUCache1{" +
+                "queue=" + queue +
+                ", map=" + map +
+                ", capacity=" + capacity +
+                '}';
+    }
+
+    public static void main(String[] args) {
+        LRUCache1 map = new LRUCache1(2);
+        map.put(1, 1);
+        map.put(2, 2);
+        System.out.println(map);
+        System.out.println(map.get(1));
+        System.out.println(map);
+        map.put(3, 3);
+        System.out.println(map);
+        System.out.println(map.get(2));
+        map.put(4, 4);
+        System.out.println(map);
+    }
+}

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

@@ -0,0 +1,7 @@
+package com.sf.service;
+
+public interface RedisService {
+
+    void seckill();
+    void init();
+}

+ 31 - 0
redis-demo/src/main/java/com/sf/service/RedisServiceImpl.java

@@ -0,0 +1,31 @@
+package com.sf.service;
+
+import com.sf.utils.RedisUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class RedisServiceImpl implements RedisService{
+
+    @Autowired
+    private RedisUtils redisUtils;
+
+    @Override
+    public void seckill() {
+
+    }
+
+
+    public void init(){
+        // 初始化秒杀商品的库存数量
+        // stockNum
+        // <1,10>    <1,9>
+        // <2,10>
+        // 尝试用hash来存储库存数据
+        //
+        // 1_stock_num  10
+        // 2_stock_num  10
+        redisUtils.set("1_stock_num","10");
+        redisUtils.set("2_stock_num","10");
+    }
+}

+ 20 - 0
redis-demo/src/main/java/com/sf/utils/RedisUtils.java

@@ -0,0 +1,20 @@
+package com.sf.utils;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+@Component
+public class RedisUtils {
+
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    public Object get(String key) {
+        return redisTemplate.opsForValue().get(key);
+    }
+
+    public void set(String key, String value) {
+        redisTemplate.opsForValue().set(key, value);
+    }
+}

+ 4 - 2
redis-demo/src/main/resources/application.yml

@@ -1,5 +1,7 @@
 spring:
   data:
     redis:
-      host: 127.0.0.1
-      port: 6379
+#      host: 127.0.0.1
+      host: 192.168.31.245
+      port: 6379
+#      password: 123456

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

@@ -35,9 +35,10 @@ public class RedisTests {
     @Test
     public void testSet() {
         SetOperations setOperations = redisTemplate.opsForSet();
-        setOperations.add("myset11", "v1", "v2", "v3");
+//        setOperations.add("myset11", "v1", "v2", "v3");
         setOperations.members("myset11").forEach(System.out::println);
     }
 
 
+
 }