秒杀商品查询接口性能优化(redis中取值)

2023-12-16 12:30:52
package com.java1234.miaosha.controller;

import com.java1234.miaosha.constant.Constant;
import com.java1234.miaosha.entity.MiaoShaGoods;
import com.java1234.miaosha.entity.R;
import com.java1234.miaosha.service.IMiaoShaGoodsService;
import com.java1234.miaosha.util.RedisUtil;
import com.java1234.miaosha.vo.MiaoShaGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 秒杀商品控制器
 */
@RestController
@RequestMapping("/miaoShaGoods")
public class MiaoShaGoodsController {
   

    @Autowired
    private IMiaoShaGoodsService miaoShaGoodsService;


    @Autowired
    private RedisUtil redisUtil;
    /**
     * 查询所有秒杀商品
     * @return
     */
    @RequestMapping("/findAll")
    public R findAll(){
   
        List<MiaoShaGoodsVo> miaoShaGoodsList=null;
        Object o=redisUtil.get(Constant.REDIS_MIAOSHA_GOODS);
        if(o==null){
   
            System.out.println("从数据库里面查询");
            miaoShaGoodsList = miaoShaGoodsService.listAll();
            redisUtil.set(Constant.REDIS_MIAOSHA_GOODS,miaoShaGoodsList,Constant.REDIS_MIAOSHA_GOODS_EXPIRE);

        }else{
   
            System.out.println("从redis中取值");
            miaoShaGoodsList= (List<MiaoShaGoodsVo>) o;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data",miaoShaGoodsList);
        return R.ok(map);
    }
}

package com.java1234.miaosha.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 * @author Administrator
 *
 */
@Component
public class RedisUtil {
   

	@Resource(name="redisTemplate")
	private RedisTemplate<String, Object> redisTemplate;

	@Resource(name="redisTemplate2")
	private RedisTemplate<String, Object> redisTemplate2;


	//=============================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;
		}
	}

	/**
	 * 指定缓存失效时间
	 * @param prefix 前缀
	 * @param key 键
	 * @param time 时间()
	 * @return
	 */
	public boolean expire(String prefix,String key,long time){
   
		try {
   
			if(time>0){
   
				redisTemplate.expire(prefix+key, time, TimeUnit.SECONDS);
			}
			return true;
		} catch (Exception e) {
   
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 根据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);
		

文章来源:https://blog.csdn.net/m0_68935893/article/details/135030895
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。