欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Redis實戰(zhàn)之商城購物車功能的實現(xiàn)代碼

 更新時間:2021年02月01日 09:57:18   作者:山前留名  
這篇文章主要介紹了Redis實戰(zhàn)之商城購物車功能的實現(xiàn)代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

目標

利用Redis實現(xiàn)商城購物車功能。

功能

根據(jù)用戶編號查詢購物車列表,且各個商品需要跟在對應的店鋪下;統(tǒng)計購物車中的商品總數(shù);新增或刪減購物車商品;增加或減少購物車中的商品數(shù)量。


分析

Hash數(shù)據(jù)類型:值為多組映射,相當于JAVA中的Map。適合存儲對象數(shù)據(jù)類型。因為用戶ID作為唯一的身份標識,所以可以把模塊名稱+用戶ID作為Redis的鍵;商品ID作為商品的唯一標識,可以把店鋪編號+商品ID作為Hash元素的鍵,商品數(shù)量為元素的值。


代碼實現(xiàn)

控制層

package com.shoppingcart.controller;
 
import com.shoppingcart.service.ShoppingCartServer;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
 
/**
 * redis實現(xiàn)購物車功能
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
 @Resource
 private ShoppingCartServer shoppingCartServer;
 
 /**
  * http://localhost:8099/shoppingCart/addCommodity?userId=001&shopId=1234560&commodityId=001&commodityNum=336
  * 添加商品
  * @return
  * @param: userId 用戶ID
  * @param: [{shopId:商鋪id,commodityId:商品id,commodityNum:商品數(shù)量},{shopId:商鋪id,commodityId:商品id,commodityNum:商品數(shù)量}]
  * 測試數(shù)據(jù):
  [
  {
  "shopId": 123,
  "commodityId": 145350,
  "commodityNum": 155.88
  },
  {
  "shopId": 123,
  "commodityId": 6754434,
  "commodityNum": 945.09
  },
  {
  "shopId": 123,
  "commodityId": 7452,
  "commodityNum": 2445.09
  },
  {
  "shopId": 3210,
  "commodityId": 98766,
  "commodityNum": 2345.09
  },
  {
  "shopId": 456,
  "commodityId": 2435640,
  "commodityNum": 11945.09
  }
  ]
  */
 @GetMapping("/addCommodity")
 public Map<String, Object> addCommodity(
   @RequestParam(value = "userId", required = true) String userId,
   @RequestBody List<Map<String, Object>> list
 ) {
  Map<String, Object> map = shoppingCartServer.addCommodity(userId, list);
  return map;
 }
 
 /**
  * 購物車列表
  * http://localhost:8099/shoppingCart/shoppingCartList?userId=001
  *
  * @param userId
  * @return 返回{店鋪ID:商品ID=商品數(shù)量}的map。
  */
 @GetMapping("/shoppingCartList")
 public Map<Object, Object> shoppingCartList(
   @RequestParam(value = "userId", required = true) String userId,
   @RequestParam(value = "pageNo", defaultValue = "0") Long pageNo,
   @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize
 ) {
  Map<Object, Object> map = shoppingCartServer.shoppingCartList(userId, pageNo, pageSize);
  return map;
 }
 
 /**
  * http://localhost:8099/shoppingCart/updateNum?userId=001&shopId=01&comId=123456&num=342
  * 修改商品數(shù)量。
  *
  * @param userId  用戶id
  * @param commodityId 商品id
  * @param commodityNum 商品數(shù)量
  * @return
  */
 @GetMapping("/updateNum")
 public Map<String, Object> updateNum(
   @RequestParam(value = "userId", required = true) String userId,
   @RequestParam(value = "shopId", required = true) Long shopId,
   @RequestParam(value = "commodityId", required = true) Long commodityId,
   @RequestParam(value = "commodityNum", required = true) Double commodityNum
 ) {
  return shoppingCartServer.updateNum(userId, shopId, commodityId, commodityNum);
 }
 
 /**
  * http://localhost:8099/shoppingCart/delCom?userId=001&shopId=01&comId=123457
  * 刪除購物車中的商品
  * @return
  * @param: userId 用戶id
  * @param: [{shopId:商鋪id,commodityId:商品id},{shopId:商鋪id,commodityId:商品id}]
  */
 @PostMapping("/delCommodity")
 public Map<String, Object> delCommodity(
   @RequestParam(value = "userId", required = true) String userId,
   @RequestBody List<Map<String, Object>> list
 ) {
  return shoppingCartServer.delCommodity(userId, list);
 }
}

業(yè)務層

package com.shoppingcart.service;
 
import java.util.List;
import java.util.Map;
 
public interface ShoppingCartServer {
 //購物車列表
 public Map<Object, Object> shoppingCartList(String userId,Long pageNo,Long pageSize);
 Map<String,Object> updateNum(String userId,Long shopId, Long commodityId, Double commodityNum);
 Map<String, Object> delCommodity(String userId, List<Map<String , Object>> list);
 Map<String, Object> addCommodity(String userId, List<Map<String , Object>> list);
}
package com.shoppingcart.service.impl;
 
import com.shoppingcart.service.ShoppingCartServer;
import com.shoppingcart.utils.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class ShoppingCartServerImpl implements ShoppingCartServer {
 @Autowired
 private RedisService redisService;
 //購物車鍵名前綴
 public static final String SHOPPING_CART = "shoppingCart:";
 //購物車的元素鍵名前綴
 public static final String SHOP_ID = "shopId";
 
 //添加商品
 @Override
 public Map<String, Object> addCommodity(String userId, List<Map<String, Object>> list) {
  //記錄:list中有哪些商品在購物車中已經(jīng)存在。
  List<String> existCommoditys = new ArrayList<>();
  //todo 購物車key
  String key = SHOPPING_CART + userId;
  for (int i = 0; i < list.size(); i++) {
   String commodityKey = SHOP_ID + list.get(i).get("shopId") + ":" + list.get(i).get("commodityId");
   //todo 添加商品
   boolean boo = redisService.hsetnx(key, commodityKey, Double.parseDouble(list.get(i).get("commodityNum") + ""));
   if (!boo) {
    existCommoditys.add(commodityKey);
   }
  }
  Map<String, Object> m = new HashMap<String, Object>() {
   {
    put("existCommoditys", existCommoditys);
    put("existCommoditysMsg", "這些商品在購物車中已經(jīng)存在,重復數(shù)量:"+existCommoditys.size()+"。");
   }
  };
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("data", m);
  map.put("code", 0);
  return map;
 }
 
 //購物車列表
 @Override
 public Map<Object, Object> shoppingCartList(String userId, Long pageNo, Long pageSize) {
  //返回{店鋪ID:商品ID=商品數(shù)量}的map。
  Map<Object, Object> map = redisService.hmget(SHOPPING_CART + userId);
  return map;
 }
 
 //修改商品數(shù)量
 @Override
 public Map<String, Object> updateNum(String userId, Long shopId, Long commodityId, Double commodityNum) {
  Map<String, Object> map = new HashMap<String, Object>();
  //todo 購物車key
  String key = SHOPPING_CART + userId;
  //todo 商品key
  String commodityKey = SHOP_ID + shopId + ":" + commodityId;
  //修改購物車的數(shù)量
  boolean boo = redisService.hset(key, commodityKey, commodityNum);
  Map<String, Object> m = new HashMap<String, Object>() {
   {
    put("key", key);
    put("commodityKey", commodityKey);
    put("commodityNum", commodityNum);
   }
  };
  map.put("data", m);
  map.put("msg", boo == true ? "修改購物車商品數(shù)量成功。" : "修改購物車商品數(shù)量失敗。");
  map.put("code", boo == true ? 0 : 1);
  return map;
 }
 
 //刪除商品
 @Override
 public Map<String, Object> delCommodity(String userId, List<Map<String, Object>> list) {
  Map<String, Object> map = new HashMap<String, Object>();
  String[] commodityIds = new String[list.size()];
  for (int i = 0; i < list.size(); i++) {
   //todo 商品key
   commodityIds[i] = SHOP_ID + list.get(i).get("shopId") + ":" + list.get(i).get("commodityId");
  }
  //todo 購物車key
  String key = SHOPPING_CART + userId;
  //刪除商品的數(shù)量
  Long num = redisService.hdel(key, commodityIds);
  map.put("msg", "刪除購物車的商品數(shù)量:" + num);
  map.put("code", 0);
  return map;
 }
}

 

工具類

package com.shoppingcart.utils;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class DateUtils {
 // 日期轉(zhuǎn)字符串,返回指定的格式
 public static String dateToString(Date date, String dateFormat) {
  SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
  return sdf.format(date);
 }
}
package com.shoppingcart.utils;
 
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.w3c.dom.ranges.Range;
 
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@Service
public class RedisService {
 
 @Autowired
 private RedisTemplate<String, Object> redisTemplate;
 
 // =============================common============================
 
 /**
  * 指定緩存失效時間
  *
  * @param key 鍵
  * @param time 時間(秒)
  * @return
  */
 public boolean expire(String key, long time) {
  try {
   if (time > 0) {
    redisTemplate.expire(key, time, TimeUnit.SECONDS);
   }
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 根據(jù)key 獲取過期時間
  *
  * @param key 鍵 不能為null
  * @return 時間(秒) 返回0代表為永久有效
  */
 public long getExpire(String key) {
  return redisTemplate.getExpire(key, TimeUnit.SECONDS);
 }
 
 /**
  * 判斷key是否存在
  *
  * @param key 鍵
  * @return true 存在 false不存在
  */
 public boolean hasKey(String key) {
  try {
   return redisTemplate.hasKey(key);
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 刪除緩存
  *
  * @param key 可以傳一個值 或多個
  */
 @SuppressWarnings("unchecked")
 public void del(String... key) {
  if (key != null && key.length > 0) {
   if (key.length == 1) {
    redisTemplate.delete(key[0]);
   } else {
    List<String> list = new ArrayList<>(Arrays.asList(key));
    redisTemplate.delete(list);
   }
  }
 }
 
 /**
  * 刪除緩存
  *
  * @param keys 可以傳一個值 或多個
  */
 @SuppressWarnings("unchecked")
 public void del(Collection keys) {
  if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(keys)) {
   redisTemplate.delete(keys);
  }
 }
 
 // ============================String=============================
 
 /**
  * 普通緩存獲取
  *
  * @param key 鍵
  * @return 值
  */
 public Object get(String key) {
  return key == null ? null : redisTemplate.opsForValue().get(key);
 }
 
 /**
  * 普通緩存放入
  *
  * @param key 鍵
  * @param value 值
  * @return true成功 false失敗
  */
 public boolean set(String key, Object value) {
  try {
   redisTemplate.opsForValue().set(key, value);
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 普通緩存放入并設置時間
  *
  * @param key 鍵
  * @param value 值
  * @param time 時間(秒) time要大于0 如果time小于等于0 將設置無限期
  * @return true成功 false 失敗
  */
 public boolean set(String key, Object value, long time) {
  try {
   if (time > 0) {
    redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
   } else {
    set(key, value);
   }
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 遞增
  *
  * @param key 鍵
  * @param delta 要增加幾(大于0)
  * @return
  */
 public long incr(String key, long delta) {
  if (delta < 0) {
   throw new RuntimeException("遞增因子必須大于0");
  }
  return redisTemplate.opsForValue().increment(key, delta);
 }
 
 /**
  * 遞減
  *
  * @param key 鍵
  * @param delta 要減少幾(小于0)
  * @return
  */
 public long decr(String key, long delta) {
  if (delta < 0) {
   throw new RuntimeException("遞減因子必須大于0");
  }
  return redisTemplate.opsForValue().increment(key, -delta);
 }
 
 // ================================Hash=================================
 
 /**
  * HashGet
  *
  * @param key 鍵 不能為null
  * @param item 項 不能為null
  * @return 值
  */
 public Object hget(String key, String item) {
  return redisTemplate.opsForHash().get(key, item);
 }
 
 /**
  * 獲取hashKey對應的所有鍵值
  *
  * @param key 鍵
  * @return 對應的多個鍵值
  */
 public Map<Object, Object> hmget(String key) {
  Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
  return entries;
 }
 
 /**
  * HashSet
  *
  * @param key 鍵
  * @param map 對應多個鍵值
  * @return true 成功 false 失敗
  */
 public boolean hmset(String key, Map<String, Object> map) {
  try {
   redisTemplate.opsForHash().putAll(key, map);
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * HashSet 并設置時間
  *
  * @param key 鍵
  * @param map 對應多個鍵值
  * @param time 時間(秒)
  * @return true成功 false失敗
  */
 public boolean hmset(String key, Map<String, Object> map, long time) {
  try {
   redisTemplate.opsForHash().putAll(key, map);
   if (time > 0) {
    expire(key, time);
   }
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 
 /**
  * 向一張hash表中放入數(shù)據(jù),如果存在就覆蓋原來的值。
  *
  * @param key 鍵
  * @param item 項
  * @param value 值
  * @return true 成功 false失敗
  */
 public boolean hset(String key, String item, Object value) {
  try {
   redisTemplate.opsForHash().put(key, item, value);
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 向一張hash表中放入數(shù)據(jù),如果存在就覆蓋原來的值。
  *
  * @param key 鍵
  * @param item 項
  * @param value 值
  * @param time 時間(秒) 注意:如果已存在的hash表有時間,這里將會替換原有的時間
  * @return true 成功 false失敗
  */
 public boolean hset(String key, String item, Object value, long time) {
  try {
   redisTemplate.opsForHash().put(key, item, value);
   if (time > 0) {
    expire(key, time);
   }
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 刪除hash表中的值
  *
  * @param key 鍵 不能為null
  * @param item 項 可以使多個 不能為null
  *    返回被刪除的數(shù)量
  */
 public Long hdel(String key, Object... item) {
  return redisTemplate.opsForHash().delete(key, item);
 }
 
 /**
  * 刪除hash表中的值
  *
  * @param key 鍵 不能為null
  * @param items 項 可以使多個 不能為null
  */
 public void hdel(String key, Collection items) {
  redisTemplate.opsForHash().delete(key, items.toArray());
 }
 
 /**
  * 判斷hash表中是否有該項的值
  *
  * @param key 鍵 不能為null
  * @param item 項 不能為null
  * @return true 存在 false不存在
  */
 public boolean hHasKey(String key, String item) {
  return redisTemplate.opsForHash().hasKey(key, item);
 }
 
 /**
  * hash數(shù)據(jù)類型:給元素一個增量 如果不存在,就會創(chuàng)建一個 并把新增后的值返回
  *
  * @param key 鍵
  * @param item 項
  * @param delta 要增加幾(大于0)
  * @return
  */
 public double hincr(String key, String item, double delta) {
  return redisTemplate.opsForHash().increment(key, item, delta);
 }
 // ============================set=============================
 
 /**
  * 根據(jù)key獲取Set中的所有值
  *
  * @param key 鍵
  * @return
  */
 public Set<Object> sGet(String key) {
  try {
   return redisTemplate.opsForSet().members(key);
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
 }
 
 /**
  * 根據(jù)value從一個set中查詢,是否存在
  *
  * @param key 鍵
  * @param value 值
  * @return true 存在 false不存在
  */
 public boolean sHasKey(String key, Object value) {
  try {
   return redisTemplate.opsForSet().isMember(key, value);
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 將數(shù)據(jù)放入set緩存
  *
  * @param key 鍵
  * @param values 值 可以是多個
  * @return 成功個數(shù)
  */
 public long sSet(String key, Object... values) {
  try {
   return redisTemplate.opsForSet().add(key, values);
  } catch (Exception e) {
   e.printStackTrace();
   return 0;
  }
 }
 
 /**
  * 將數(shù)據(jù)放入set緩存
  *
  * @param key 鍵
  * @param values 值 可以是多個
  * @return 成功個數(shù)
  */
 public long sSet(String key, Collection values) {
  try {
   return redisTemplate.opsForSet().add(key, values.toArray());
  } catch (Exception e) {
   e.printStackTrace();
   return 0;
  }
 }
 
 /**
  * 將set數(shù)據(jù)放入緩存
  *
  * @param key 鍵
  * @param time 時間(秒)
  * @param values 值 可以是多個
  * @return 成功個數(shù)
  */
 public long sSetAndTime(String key, long time, Object... values) {
  try {
   Long count = redisTemplate.opsForSet().add(key, values);
   if (time > 0)
    expire(key, time);
   return count;
  } catch (Exception e) {
   e.printStackTrace();
   return 0;
  }
 }
 
 /**
  * 獲取set緩存的長度
  *
  * @param key 鍵
  * @return
  */
 public long sGetSetSize(String key) {
  try {
   return redisTemplate.opsForSet().size(key);
  } catch (Exception e) {
   e.printStackTrace();
   return 0;
  }
 }
 
 /**
  * 移除值為value的
  *
  * @param key 鍵
  * @param values 值 可以是多個
  * @return 移除的個數(shù)
  */
 public long setRemove(String key, Object... values) {
  try {
   Long count = redisTemplate.opsForSet().remove(key, values);
   return count;
  } catch (Exception e) {
   e.printStackTrace();
   return 0;
  }
 }
 
 // ===============================list=================================
 
 /**
  * 獲取list緩存的內(nèi)容
  *
  * @param key 鍵
  * @param start 開始
  * @param end 結(jié)束 0 到 -1代表所有值
  * @return
  */
 public List<Object> lGet(String key, long start, long end) {
  try {
   return redisTemplate.opsForList().range(key, start, end);
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
 }
 
 /**
  * 獲取list緩存的長度
  *
  * @param key 鍵
  * @return
  */
 public long lGetListSize(String key) {
  try {
   return redisTemplate.opsForList().size(key);
  } catch (Exception e) {
   e.printStackTrace();
   return 0;
  }
 }
 
 /**
  * 通過索引 獲取list中的值
  *
  * @param key 鍵
  * @param index 索引 index>=0時, 0 表頭,1 第二個元素,依次類推;index<0時,-1,表尾,-2倒數(shù)第二個元素,依次類推
  * @return
  */
 public Object lGetIndex(String key, long index) {
  try {
   return redisTemplate.opsForList().index(key, index);
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
 }
 
 /**
  * 將list放入緩存
  *
  * @param key 鍵
  * @param value 值
  * @return
  */
 public boolean lSet(String key, Object value) {
  try {
   redisTemplate.opsForList().rightPush(key, value);
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 將list放入緩存
  *
  * @param key 鍵
  * @param value 值
  * @param time 時間(秒)
  * @return
  */
 public boolean lSet(String key, Object value, long time) {
  try {
   redisTemplate.opsForList().rightPush(key, value);
   if (time > 0)
    expire(key, time);
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 將list放入緩存
  *
  * @param key 鍵
  * @param value 值
  * @return
  */
 public boolean lSet(String key, List<Object> value) {
  try {
   redisTemplate.opsForList().rightPushAll(key, value);
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 將list放入緩存
  *
  * @param key 鍵
  * @param value 值
  * @param time 時間(秒)
  * @return
  */
 public boolean lSet(String key, List<Object> value, long time) {
  try {
   redisTemplate.opsForList().rightPushAll(key, value);
   if (time > 0)
    expire(key, time);
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 根據(jù)索引修改list中的某條數(shù)據(jù)
  *
  * @param key 鍵
  * @param index 索引
  * @param value 值
  * @return
  */
 public boolean lUpdateIndex(String key, long index, Object value) {
  try {
   redisTemplate.opsForList().set(key, index, value);
   return true;
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }
 }
 
 /**
  * 移除N個值為value
  *
  * @param key 鍵
  * @param count 移除多少個
  * @param value 值
  * @return 移除的個數(shù)
  */
 public long lRemove(String key, long count, Object value) {
  try {
   Long remove = redisTemplate.opsForList().remove(key, count, value);
   return remove;
  } catch (Exception e) {
   e.printStackTrace();
   return 0;
  }
 }
 // ===============================Zset=================================
 
 /**
  * 給key鍵的value增加value分數(shù),沒有則會創(chuàng)建。
  *
  * @param key 鍵
  * @param value 值
  * @param score 分數(shù)
  */
 public Double incrementScore(String key, String value, double score) {
  //Boolean add = redisTemplate.boundZSetOps(key).add(value, score);
  Double add = redisTemplate.boundZSetOps(key).incrementScore(value, score);
  return add;
 }
 
 /**
  * 獲得指定Zset元素的分數(shù)
  *
  * @param key
  * @param value
  * @return
  */
 public Double score(String key, String value) {
  Double score = redisTemplate.boundZSetOps(key).score(value);
  return score;
 }
 
 /**
  * 升序查詢key集合內(nèi)[endTop,startTop]如果是負數(shù)表示倒數(shù)
  * endTop=-1,startTop=0表示獲取所有數(shù)據(jù)。
  *
  * @param key
  * @param startPage
  * @param endPage
  */
 public Set<ZSetOperations.TypedTuple<Object>> rangeWithScores(String key, int startPage, int endPage) {
  Set<ZSetOperations.TypedTuple<Object>> set = redisTemplate.boundZSetOps(key).rangeWithScores(startPage, endPage);
  return set;
 }
 
 /**
  * 降序查詢key集合內(nèi)[endTop,startTop],如果是負數(shù)表示倒數(shù)
  * endTop=-1,startTop=0表示獲取所有數(shù)據(jù)。
  *
  * @param key
  * @param startPage
  * @param endPage
  */
 public Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String key, int startPage, int endPage) {
  Set<ZSetOperations.TypedTuple<Object>> set = redisTemplate.boundZSetOps(key).reverseRangeWithScores(startPage, endPage);
  return set;
 }
 
 /**
  * 批量新增數(shù)據(jù)
  *
  * @param key
  * @param set
  * @return
  */
 public Long zsetAdd(String key, Set set) {
  Long add = redisTemplate.boundZSetOps(key).add(set);
  return add;
 }
 
 /**
  * 刪除指定鍵的指定下標范圍數(shù)據(jù)
  *
  * @param key
  * @param startPage
  * @param endPage
  */
 public Long zsetRemoveRange(String key, int startPage, int endPage) {
  Long l = redisTemplate.boundZSetOps(key).removeRange(startPage, endPage);
  return l;
 }
 /**
  * 刪除指定鍵的指定值
  *
  * @param key
  * @param value
  */
 public Long zsetRemove(String key, String value) {
  Long remove = redisTemplate.boundZSetOps(key).remove(value);
  return remove;
 }
}

到此這篇關于Redis實戰(zhàn)之商城購物車功能的實現(xiàn)代碼的文章就介紹到這了,更多相關Redis商城購物車內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • redis單節(jié)點安裝與配置方式

    redis單節(jié)點安裝與配置方式

    這篇文章主要介紹了redis單節(jié)點安裝與配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 將音頻文件轉(zhuǎn)二進制分包存儲到Redis的實現(xiàn)方法(奇淫技巧操作)

    將音頻文件轉(zhuǎn)二進制分包存儲到Redis的實現(xiàn)方法(奇淫技巧操作)

    這篇文章主要介紹了將音頻文件轉(zhuǎn)二進制分包存儲到Redis的實現(xiàn)方法(奇淫技巧操作),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Redis的數(shù)據(jù)存儲及String類型的實現(xiàn)

    Redis的數(shù)據(jù)存儲及String類型的實現(xiàn)

    這篇文章主要介紹了Redis的數(shù)據(jù)存儲及String類型的實現(xiàn),redis作為k-v數(shù)據(jù)存儲,因查找和操作的時間復雜度都是O(1)和豐富的數(shù)據(jù)類型及數(shù)據(jù)結(jié)構(gòu)的優(yōu)化,了解了這些數(shù)據(jù)類型和結(jié)構(gòu)更有利于我們平時對于redis的使用,需要的朋友可以參考下
    2022-10-10
  • Redis應用之簽到的使用

    Redis應用之簽到的使用

    在很多時候,我們遇到用戶簽到的場景,本文主要介紹了Redis應用之簽到的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-05-05
  • 一起raid數(shù)據(jù)恢復及回遷成功的案例

    一起raid數(shù)據(jù)恢復及回遷成功的案例

    這篇文章主要介紹了一起raid數(shù)據(jù)恢復及回遷成功的案例,需要的朋友可以參考下
    2017-04-04
  • Redis內(nèi)存回收策略

    Redis內(nèi)存回收策略

    這篇文章主要介紹了Redis內(nèi)存回收策略,需要的朋友可以參考下
    2007-02-02
  • Redisson延時隊列RedissonDelayed的具體使用

    Redisson延時隊列RedissonDelayed的具體使用

    定時調(diào)度基本是每個項目都會遇到的業(yè)務場景,一般地,都會通過任務調(diào)度工具執(zhí)行定時任務完成,但是會有一定的缺點,本文主要介紹了Redisson延時隊列RedissonDelayed的具體使用,感興趣的可以了解一下
    2024-02-02
  • Redis做數(shù)據(jù)持久化的解決方案及底層原理

    Redis做數(shù)據(jù)持久化的解決方案及底層原理

    Redis有兩種方式來實現(xiàn)數(shù)據(jù)的持久化,分別是RDB(Redis Database)和AOF(Append Only File),今天通過本文給大家聊一聊Redis做數(shù)據(jù)持久化的解決方案及底層原理,感興趣的朋友一起看看吧
    2021-07-07
  • 使用Redis解決高并發(fā)方案及思路解讀

    使用Redis解決高并發(fā)方案及思路解讀

    這篇文章主要介紹了使用Redis解決高并發(fā)方案及思路,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • redis啟動報錯Can‘t?open?the?log?file:?No?such?file?or?directory

    redis啟動報錯Can‘t?open?the?log?file:?No?such?file?or?d

    這篇文章主要介紹了redis啟動報錯Can‘t?open?the?log?file:?No?such?file?or?directory問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評論