SpringBoot实现页面数据缓存

过期时间类

public class MememCacheEntity {

    //过期时间
    private  Date _addDate = null;
    public Date getAddDate()
    {
        return  this._addDate;
    }

    public void setAddDate(Date newDate)
    {
       this._addDate = newDate;
    }

    private List<MyT> _data = null;

    public List<MyT> getData()
    {
        return  this._data;
    }

    public void setData(List<MyT> newData)
    {
        this._data = newData;
    }

    private  String _key = null;
    public String getKey()
    {
        return  this._key;
    }

    public void setKey(String newKey)
    {
        this._key = newKey;
    }


}

对缓存数据进行操作

public class MemCahceHelper {

    private HashMap<String,MememCacheEntity> cacheData = null;

    //单例访问处理
    private static  MemCahceHelper _Instance = null;
    public  static  MemCahceHelper Instance() {
        if(_Instance == null)
        {
            _Instance = new MemCahceHelper();
        }
        return  _Instance;
    }

    private  MemCahceHelper() {
        cacheData = new HashMap<String,MememCacheEntity>();
    }


    //添加到缓存数据
    public void Add(String key, List<MyT> data,int expireDate)
    {
        if(key != "" && key != null)
        {
            if(cacheData.containsKey(key)) {
                cacheData.remove(key);
            }
            MememCacheEntity newData = new MememCacheEntity();
            newData.setData(data);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newData.setAddDate(new Date(Long.parseLong(String.valueOf(new Date().getTime()+expireDate))));
            newData.setKey(key);
            cacheData.put(key,newData);
        }
    }

    //删除缓存数据
    public  void  Remove(String key)
    {
        if(key !="" && key !=null)
        {
            if(cacheData.containsKey(key)) {
                cacheData.remove(key);
            }
        }
    }

    //获取缓存数据
    public  List<MyT> GetCache(String key)
    {
        List<MyT> tempData = null;
        if(key !="" && key !=null)
        {
            if(cacheData.containsKey(key)) {
                System.out.println("进入缓存--------" + key);
                MememCacheEntity data = cacheData.get(key);
                if(data.getAddDate().getTime() >  new Date().getTime()) {
                    tempData = data.getData();
                }else{
                    System.out.println("删除缓存数据--重新读取数据库数据");
                    MemCahceHelper.Instance().Remove(key);
                }

            }
        }
        return tempData;
    }

}

Mapper接口

public List<MyT> CacheaiteshopList();

Mapper.xml

<select id="CacheaiteshopList" resultType="com.demo.entity.AiteshopCarClass">
         SELECT * FROM aiteshop_car_class
</select>

Service

public List<MyT> CacheaiteshopList();

ServiceImpl

@Override
    public List<MyT> CacheaiteshopList(){
        List<MyT> aiteshopCarClassList = null;
        if(MemCahceHelper.Instance().GetCache(currencyKey)!=null){
            aiteshopCarClassList = MemCahceHelper.Instance().GetCache(currencyKey);
        }
        if(aiteshopCarClassList == null)
        {
            //从数据库取数据
            List<MyT> myTS = this.baseMapper.CacheaiteshopList();
            aiteshopCarClassList = myTS;
            List<MyT> parentCurrrencyList = new ArrayList<MyT>(aiteshopCarClassList);
            //设定过期时间,多少时间之后会自动删除,然后从数据库拿数据
            MemCahceHelper.Instance().Add(currencyKey,parentCurrrencyList,10000);//毫秒
        }
        return aiteshopCarClassList;
}

Controller

    @RequestMapping(value = "/index")
    public String AiteshopCarClassIndex(HttpServletRequest request){
      List<MyT> aiteshopCarClassList = aiteshopCarClassService.CacheaiteshopList();
      request.setAttribute("aiteshopCarClassList", aiteshopCarClassList);
      return "index";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值