最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

目標(biāo)

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

功能

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


分析

Hash數(shù)據(jù)類型:值為多組映射,相當(dāng)于JAVA中的Map。適合存儲(chǔ)對(duì)象數(shù)據(jù)類型。因?yàn)橛脩鬒D作為唯一的身份標(biāo)識(shí),所以可以把模塊名稱+用戶ID作為Redis的鍵;商品ID作為商品的唯一標(biāo)識(shí),可以把店鋪編號(hào)+商品ID作為Hash元素的鍵,商品數(shù)量為元素的值。


代碼實(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實(shí)現(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ù)量}]
  * 測(cè)試數(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è)務(wù)層

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)存在,重復(fù)數(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============================
 
 /**
  * 指定緩存失效時(shí)間
  *
  * @param key 鍵
  * @param time 時(shí)間(秒)
  * @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 獲取過期時(shí)間
  *
  * @param key 鍵 不能為null
  * @return 時(shí)間(秒) 返回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 可以傳一個(gè)值 或多個(gè)
  */
 @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 可以傳一個(gè)值 或多個(gè)
  */
 @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;
  }
 }
 
 /**
  * 普通緩存放入并設(shè)置時(shí)間
  *
  * @param key 鍵
  * @param value 值
  * @param time 時(shí)間(秒) time要大于0 如果time小于等于0 將設(shè)置無限期
  * @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 項(xiàng) 不能為null
  * @return 值
  */
 public Object hget(String key, String item) {
  return redisTemplate.opsForHash().get(key, item);
 }
 
 /**
  * 獲取hashKey對(duì)應(yīng)的所有鍵值
  *
  * @param key 鍵
  * @return 對(duì)應(yīng)的多個(gè)鍵值
  */
 public Map<Object, Object> hmget(String key) {
  Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
  return entries;
 }
 
 /**
  * HashSet
  *
  * @param key 鍵
  * @param map 對(duì)應(yīng)多個(gè)鍵值
  * @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 并設(shè)置時(shí)間
  *
  * @param key 鍵
  * @param map 對(duì)應(yīng)多個(gè)鍵值
  * @param time 時(shí)間(秒)
  * @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 項(xiàng)
  * @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 項(xiàng)
  * @param value 值
  * @param time 時(shí)間(秒) 注意:如果已存在的hash表有時(shí)間,這里將會(huì)替換原有的時(shí)間
  * @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 項(xiàng) 可以使多個(gè) 不能為null
  *    返回被刪除的數(shù)量
  */
 public Long hdel(String key, Object... item) {
  return redisTemplate.opsForHash().delete(key, item);
 }
 
 /**
  * 刪除hash表中的值
  *
  * @param key 鍵 不能為null
  * @param items 項(xiàng) 可以使多個(gè) 不能為null
  */
 public void hdel(String key, Collection items) {
  redisTemplate.opsForHash().delete(key, items.toArray());
 }
 
 /**
  * 判斷hash表中是否有該項(xiàng)的值
  *
  * @param key 鍵 不能為null
  * @param item 項(xiàng) 不能為null
  * @return true 存在 false不存在
  */
 public boolean hHasKey(String key, String item) {
  return redisTemplate.opsForHash().hasKey(key, item);
 }
 
 /**
  * hash數(shù)據(jù)類型:給元素一個(gè)增量 如果不存在,就會(huì)創(chuàng)建一個(gè) 并把新增后的值返回
  *
  * @param key 鍵
  * @param item 項(xiàng)
  * @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從一個(gè)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 值 可以是多個(gè)
  * @return 成功個(gè)數(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 值 可以是多個(gè)
  * @return 成功個(gè)數(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 時(shí)間(秒)
  * @param values 值 可以是多個(gè)
  * @return 成功個(gè)數(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 值 可以是多個(gè)
  * @return 移除的個(gè)數(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時(shí), 0 表頭,1 第二個(gè)元素,依次類推;index<0時(shí),-1,表尾,-2倒數(shù)第二個(gè)元素,依次類推
  * @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 時(shí)間(秒)
  * @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 時(shí)間(秒)
  * @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個(gè)值為value
  *
  * @param key 鍵
  * @param count 移除多少個(gè)
  * @param value 值
  * @return 移除的個(gè)數(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分?jǐn)?shù),沒有則會(huì)創(chuàng)建。
  *
  * @param key 鍵
  * @param value 值
  * @param score 分?jǐn)?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元素的分?jǐn)?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]如果是負(fù)數(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],如果是負(fù)數(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;
 }
 
 /**
  * 刪除指定鍵的指定下標(biāo)范圍數(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;
 }
}

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

相關(guān)文章

  • Redis MGET命令深度解析

    Redis MGET命令深度解析

    Redis的MGET命令是一種高效的批量讀取操作,可以顯著提高讀取性能,減少網(wǎng)絡(luò)往返的次數(shù),本文從MGET命令的機(jī)制實(shí)現(xiàn)、底層原理、應(yīng)用場(chǎng)景及性能優(yōu)化等多個(gè)維度,深入解析Redis中的MGET命令的工作方式,并對(duì)它與其他批量操作命令的對(duì)比進(jìn)行了詳細(xì)介紹
    2024-09-09
  • Redis實(shí)現(xiàn)每日簽到功能(大數(shù)據(jù)量)

    Redis實(shí)現(xiàn)每日簽到功能(大數(shù)據(jù)量)

    在面對(duì)百萬級(jí)用戶簽到情況下,傳統(tǒng)數(shù)據(jù)庫存儲(chǔ)和判斷會(huì)遇到瓶頸,使用Redis的二進(jìn)制數(shù)據(jù)類型可實(shí)現(xiàn)高效的簽到功能,示例代碼展示了如何調(diào)用這些功能,包括當(dāng)天簽到、補(bǔ)簽以及查詢簽到記錄,PHP結(jié)合Redis二進(jìn)制數(shù)據(jù)類型可有效處理大數(shù)據(jù)量下的簽到問題
    2024-10-10
  • Redis數(shù)據(jù)庫中實(shí)現(xiàn)分布式鎖的方法

    Redis數(shù)據(jù)庫中實(shí)現(xiàn)分布式鎖的方法

    這篇文章主要介紹了Redis數(shù)據(jù)庫中實(shí)現(xiàn)分布式鎖的方法,Redis是一個(gè)高性能的主存式數(shù)據(jù)庫,需要的朋友可以參考下
    2015-06-06
  • reids自定義RedisTemplate以及亂碼問題解決

    reids自定義RedisTemplate以及亂碼問題解決

    本文主要介紹了reids自定義RedisTemplate以及亂碼問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • redis?setIfAbsent返回null的問題及解決

    redis?setIfAbsent返回null的問題及解決

    這篇文章主要介紹了redis?setIfAbsent返回null的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Redis的LRU機(jī)制介紹

    Redis的LRU機(jī)制介紹

    這篇文章主要介紹了Redis的LRU機(jī)制介紹,Redis會(huì)按LRU算法刪除設(shè)置了過期時(shí)間但還沒有過期的key,而對(duì)于沒有設(shè)置過期時(shí)間的key,Redis是永遠(yuǎn)保留的,需要的朋友可以參考下
    2015-06-06
  • Redis集群模式和常用數(shù)據(jù)結(jié)構(gòu)詳解

    Redis集群模式和常用數(shù)據(jù)結(jié)構(gòu)詳解

    Redis集群模式下的運(yùn)維指令主要用于集群的搭建、管理、監(jiān)控和維護(hù),講解了一些常用的Redis集群運(yùn)維指令,本文重點(diǎn)介紹了Redis集群模式和常用數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下
    2024-03-03
  • Redis中鍵值過期操作示例詳解

    Redis中鍵值過期操作示例詳解

    這篇文章主要給大家介紹了關(guān)于Redis中鍵值過期操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • redis與mongodb的區(qū)別總結(jié)

    redis與mongodb的區(qū)別總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于redis與mongodb的區(qū)別的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。
    2019-06-06
  • 解決redis-cli報(bào)錯(cuò)Could not connect to Redis at 127.0.0.1:6379: Connection refused

    解決redis-cli報(bào)錯(cuò)Could not connect to Redis&

    這篇文章主要介紹了解決redis-cli報(bào)錯(cuò)Could not connect to Redis at 127.0.0.1:6379: Connection refused,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04

最新評(píng)論

两当县| 靖远县| 洛宁县| 凭祥市| 阳原县| 和龙市| 泸西县| 盈江县| 文昌市| 视频| 平湖市| 汉中市| 确山县| 如东县| 清涧县| 石柱| 潮州市| 友谊县| 太仆寺旗| 肥城市| 公主岭市| 石狮市| 永城市| 霍山县| 资溪县| 陆川县| 康乐县| 托里县| 岗巴县| 彭山县| 巫溪县| 达州市| 金沙县| 仁寿县| 丹江口市| 白沙| 元谋县| 仁化县| 彭泽县| 天祝| 锡林郭勒盟|