zoukankan      html  css  js  c++  java
  • redis缓存如何添加到业务逻辑代码中

    redis作为缓存的作用就是减少对数据库的访问压力,当我们访问一个数据的时候,首先我们从redis中查看是否有该数据,如果没有,则从数据库中读取,将从数据库中读取的数据存放到缓存中,下次再访问同样的数据的是,还是先判断redis中是否存在该数据,如果有,则从缓存中读取,不访问数据库了。

    举个例子:根据内容分类id访问内容:

        package com.taotao.rest.service.impl;

     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
     
    import com.taotao.commonEntity.JsonUtils;
    import com.taotao.commonEntity.TaotaoResult;
    import com.taotao.mapper.TbContentMapper;
    import com.taotao.pojo.TbContent;
    import com.taotao.pojo.TbContentExample;
    import com.taotao.pojo.TbContentExample.Criteria;
    import com.taotao.rest.dao.JedisClient;
    import com.taotao.rest.service.ContentService;
     
    import redis.clients.jedis.Jedis;
    //首页大广告位的获取服务层信息
    @Service
    public class ContentServiceImpl implements ContentService {
         
        @Value("${CONTENTCATEGORYID}")
        private String CONTENTCATEGORYID;
        @Autowired
        private TbContentMapper contentMapper;
        @Autowired
        private JedisClient jedisClient;
         
        @Override
        public List<TbContent> getContentList(Long categoryId) {
            /*一般第一次访问的时候先从数据库读取数据,然后将数据写入到缓存,再次访问同一内容的时候就从缓存中读取,如果缓存中没有则从数据库中读取
            所以我们添加缓存逻辑的时候,从数据库中将内容读取出来之后,先set入缓存,然后再从缓存中添加读取行为,如果缓存为空则从数据库中进行读取
            */
            //从缓存中获取值
            String getData = jedisClient.hget(CONTENTCATEGORYID, categoryId+"");
            if (!StringUtils.isBlank(getData)) {
                List<TbContent> resultList= JsonUtils.jsonToList(getData, TbContent.class);
                return resultList; 
            }
            TbContentExample example=new TbContentExample();
            Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
           List<TbContent> list = contentMapper.selectByExample(example);
           //向缓存中放入值
           String jsonData = JsonUtils.objectToJson(list);
           jedisClient.hset(CONTENTCATEGORYID, categoryId+"",jsonData);
            return list;
        }
     
    }
    所以这里就是写逻辑代码的时候,在业务功能处,从缓存中读取-----从db中读取----将数据写入缓存。

    3.针对上面出现的问题:

    当我们后台数据库中内容修改之后,因为缓存中的内容没有修改,我们访问的时候都是先访问缓存,所以即使数据库中的内容修改了,但是页面的显示还是不会改变的。因为缓存没有更新,所以这就涉及到缓存同步的问题:即数据库修改了内容与缓存中对应的内容同步。

    缓存同步的原理:就是将redis中的key进行删除,下次访问的时候,redis中没有改数据,则从DB进行查询,再次更新到redis中。

    我们可以写一个缓存同步的服务:

    缓存同步除了查询是没有涉及到同步问题,增加删除修改都会涉及到同步问题。

    只需要在后台进行CRUD的地方添加调用该缓存同步的服务即可:

    5.redis客户端jedis的使用:

  • 相关阅读:
    数据库MySQL调优实战经验总结
    Apache常见功能实战详解
    使用HeartBeat实现高可用HA的配置过程详解
    Nginx实现集群的负载均衡配置过程详解
    CentOS系统通过PXE实现批量无人值守安装
    CentOS 7 网卡命名修改为eth0格式
    Nagios 系统监控基本安装配置过程详解
    LAMP 系统服务搭建过程详解
    使用 python 管理 mysql 开发工具箱
    C++标准库string类型的使用和操作总结
  • 原文地址:https://www.cnblogs.com/MaxElephant/p/14535755.html
Copyright © 2011-2022 走看看