• 使用redis做缓存


    redis常本用来作为缓存服务器。缓存的好处是减少服务器的压力,数据查询速度快。解决数据响应慢的问题。

    添加缓存:只用redis的Hash数据类型添加缓存。

    例如:需要在查询的业务功能中,添加缓存

    1.首先需要在执行正常的业务逻辑之前(查询数据库之前),查询缓存,如果缓存中没有需要的数据,查询数据库

    为了防止添加缓存出错,影响正常业务代码的执行,将添加缓存的代码放置到try-catch代码快中,让程序自动捕获。

    2.完成数据库的查询操作,查询完成之后需要将查询的数据添加到缓存中。

    代码:

        @Override
        public List<TbContent> findContentByCategoryId(Long categoryId) {
            // 查询出的内容列表可以添加到缓存中,便于展示,为了保证添加缓存出现错误不影响程序的正常业务功能,可以使用try catch的方式加缓存
            try {
                String json = jedisClient.hget(CONTENT_LIST, categoryId + "");
                if (json != null) {
                    List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class);
                    return list;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            TbContentExample example = new TbContentExample();
            Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            // 使用selectByExampleWithBLOBs方法会将content属性框中的内容也查询出来
            List<TbContent> list = contentMapper.selectByExampleWithBLOBs(example);
    
            // 操作完成后需要将查询的内容添加到缓存中,因为添加缓存的过程可能出错,所以使用try catch将异常抛出即可
            // categoryId+""将Long类型的数据转换成String类型的
            try {
                jedisClient.hset(CONTENT_LIST, categoryId + "", JsonUtils.objectToJson(list));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list;
        }

    Json转换的工具类:

    package nyist.e3.utils;
    
    import java.util.List;
    
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JavaType;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    /**
     * 淘淘商城自定义响应结构
     */
    public class JsonUtils {
    
        // 定义jackson对象
        private static final ObjectMapper MAPPER = new ObjectMapper();
    
        /**
         * 将对象转换成json字符串。
         * <p>Title: pojoToJson</p>
         * <p>Description: </p>
         * @param data
         * @return
         */
        public static String objectToJson(Object data) {
            try {
                String string = MAPPER.writeValueAsString(data);
                return string;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return null;
        }
        
        /**
         * 将json结果集转化为对象
         * 
         * @param jsonData json数据
         * @param clazz 对象中的object类型
         * @return
         */
        public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
            try {
                T t = MAPPER.readValue(jsonData, beanType);
                return t;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        
        /**
         * 将json数据转换成pojo对象list
         * <p>Title: jsonToList</p>
         * <p>Description: </p>
         * @param jsonData
         * @param beanType
         * @return
         */
        public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
            JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
            try {
                List<T> list = MAPPER.readValue(jsonData, javaType);
                return list;
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return null;
        }
        
    }
    View Code

    如何实现缓存同步?

    所谓缓存同步,也就是在数据库的增删改操作完成之后,清除对应的缓存即可,下一次执行查询操作时,重新添加新的缓存,这样就很好的实现了缓存同步的问题。

  • 相关阅读:
    mysql5.7 编码统一utf-8
    Spring+Swagger文档无法排序问题解决
    git的常用命令
    maven常用命令
    在centos6.5中安装zookeeper集群
    在centos6.5中安装github的客户端git
    rpm安装和卸载软件
    在centos6.5中安装scp和lrzsz
    用cxf开发restful风格的WebService
    cxf的soap风格+spirng4+maven 客户端
  • 原文地址:https://www.cnblogs.com/shuai-server/p/8922778.html
Copyright © 2020-2023  润新知