查询优化技术方案

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: 查询优化技术方案

查询优化技术之多级缓存  配置Jedis不需要序列化存入json

   image.pngimage.png



 一级缓存             二级缓存                              三级缓存                            四级缓存

redis缓存  --> 热点内存本地缓存JVM  --> nginx proxy chache 缓存 --> nginx lua缓存


   redis缓存 :

      单机版 : 存在单点故障,存在容量上限

         sentinal 哨兵模式 :  解决单机版问题,解决redis哪台坏了,那个redis备份是最新的。单点机器。

                                           master机器通过心跳机制和sentinal建立链接,当master出现异常,sentinal则将                                                master机器改为slave,slave变成master进行切换。

         集群cluster模式:不需要知道redis集群的状态


        jedis已经集成三种模式支持,只需配制Redis 集群模式cluster配置及搭建

         Springboot整合luttuce 当集群主节点宕机之后需要重新拓扑刷新

      查询资料 https://blog.csdn.net/qq_30062125/article/details/101689894

                        https://www.twblogs.net/a/5c9edae4bd9eee75238884b8/zh-cn


package cn.duckerkj.springbootguide.config.redis;

import java.time.Duration;

import org.joda.time.DateTime;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import  org.springframework.boot.autoconfigure.data.redis.RedisProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import  org.springframework.data.redis.connection.RedisClusterConfiguration;

import  org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;

import  org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import  org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import  org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.module.SimpleModule;

import io.lettuce.core.ReadFrom;

import io.lettuce.core.cluster.ClusterClientOptions;

import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;

/**

* @author pgig

* @date 2019/3/21

*/

@Configuration

public class RedisConfiguration {

   @Autowired

   private RedisProperties redisProperties;

   @Value("${redis.maxRedirects:3}")

   private int maxRedirects;

   @Value("${redis.refreshTime:5}")

   private int refreshTime;

   @Bean

   public LettuceConnectionFactory redisConnectionFactory() {

       RedisClusterConfiguration redisClusterConfiguration =  new  RedisClusterConfiguration(redisProperties.getCluster().getNodes());

       redisClusterConfiguration.setMaxRedirects(maxRedirects);

       //支持自适应集群拓扑刷新和静态刷新源

       ClusterTopologyRefreshOptions  clusterTopologyRefreshOptions =   ClusterTopologyRefreshOptions.builder()

               .enablePeriodicRefresh()

               .enableAllAdaptiveRefreshTriggers()

               .refreshPeriod(Duration.ofSeconds(refreshTime))

               .build();

       ClusterClientOptions clusterClientOptions =  ClusterClientOptions.builder()

                .topologyRefreshOptions(clusterTopologyRefreshOptions).build();

       //从优先,读写分离,读从可能存在不一致,最终一致性CP

       LettuceClientConfiguration lettuceClientConfiguration =  LettuceClientConfiguration.builder()

               .readFrom(ReadFrom.SLAVE_PREFERRED)

               .clientOptions(clusterClientOptions).build();

       return new  LettuceConnectionFactory(redisClusterConfiguration,  lettuceClientConfiguration);

   }

   @Bean

   public RedisTemplate<Object, Object>  redisTemplate(LettuceConnectionFactory redisConnectionFactory) {

       RedisTemplate<Object, Object> redisTemplate = new  RedisTemplate<>();

        redisTemplate.setConnectionFactory(redisConnectionFactory);

       Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new  Jackson2JsonRedisSerializer(Object.class);      

       ObjectMapper om = new ObjectMapper();

       

       SimpleModule simpleModule = new SimpleModule();

       simpleModule.addSerializer(DateTime.class,new  JodaDateTimeJsonSerializer());

       simpleModule.addDeserializer(DateTime.class,new  JodaDateTimeJsonDeserializer());

       om.registerModule(simpleModule);

        om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);

        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

       jackson2JsonRedisSerializer.setObjectMapper(om);

       

        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

       redisTemplate.setKeySerializer(new  StringRedisSerializer());

       redisTemplate.afterPropertiesSet();

       return redisTemplate;

   }

}

     



本地数据热点缓存 (二级)

      不叫本地缓存是因为: 1、热点数据  2、脏读非常不敏感 3、内存可控 。使用的是JVM缓存。本地缓存生命周期比较短


        方式一 : Guava cache   Guava深入学习了解

                   可以控制的大小和超时时间

                   可配置的lru策略

                   线程安全

           

       <dependency>

           <groupId>com.google.guava</groupId>

           <artifactId>guava</artifactId>

           <version>23.0</version>

       </dependency>


         问题 : 存在数据更新,本地缓存没有更新的问题???




nginx proxy cache 缓存 (三级) 推荐nginx lua


      1、nginx反向代理 前置条件

        2、依靠文件系统存索引级的文件

        3、依靠内存缓存文件地址 就是key

       

      配置 :

    proxy_cache_path  

   key内存100m 保存时间7天 存在目录一共10G

   

   location 配置

image.png





      问题 : 不应该访问本地文件磁盘,降低读本地文件性能降低 应该读取远程NAS,文件应该无线大。





  16、nginx lua 推荐代替 nginx proxy cache


          1、lua 协程机制 。https://coding.imooc.com/lesson/338.html#mid=24955

               依托于线程 无需考虑异步机制 。如果阻塞 会想epoll放弃自己的调用权限

               image.png

         2、nginx 协程机制 https://coding.imooc.com/lesson/338.html#mid=24956

               image.png

               image.png

image.png

               

         3、nginx lua 插槽点

            image.png


image.png

         实战 https://coding.imooc.com/lesson/338.html#mid=24958

           主要常用在content_by_lua

               image.png

             好处: 使用lua脚本直接处理业务逻辑不用访问后端

               

           4、openresty

           image.png

               

               

           image.png


               4.1 openresty hellow world  注意路径地址

               4.2 share dic

                   4.2.1 配置share dic https://coding.imooc.com/lesson/338.html#mid=24961

image.png


           image.png

   


            4.4、openresty redis 可以代替share dichttps://coding.imooc.com/lesson/338.html#mid=24962


image.png

           

目录
相关文章
|
5天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1138 2
|
4天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
692 11
|
14天前
|
人工智能 运维 安全
|
3天前
|
机器学习/深度学习 物联网
Wan2.2再次开源数字人:Animate-14B!一键实现电影角色替换和动作驱动
今天,通义万相的视频生成模型又又又开源了!Wan2.2系列模型家族新增数字人成员Wan2.2-Animate-14B。
323 10
|
5天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
319 0
|
12天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
5天前
|
缓存 供应链 监控
VVIC seller_search 排行榜搜索接口深度分析及 Python 实现
VVIC搜款网seller_search接口提供服装批发市场的商品及商家排行榜数据,涵盖热销榜、销量排名、类目趋势等,支持多维度筛选与数据分析,助力选品决策、竞品分析与市场预测,为服装供应链提供有力数据支撑。