缓存数据生产服务的工作流程

网友投稿 272 2022-09-04

缓存数据生产服务的工作流程

缓存数据生产服务的工作流程分析

(1)监听多个kafka topic,每个kafka topic对应一个服务(简化一下,监听一个kafka topic)

(2)如果一个服务发生了数据变更,那么就发送一个消息到kafka topic中

(3)缓存数据生产服务监听到了消息以后,就发送请求到对应的服务中调用接口以及拉取数据,此时是从mysql中查询的

(4)缓存数据生产服务拉取到了数据之后,会将数据在本地缓存中写入一份,就是ehcache中

(5)同时会将数据在redis中写入一份

每一层缓存在高并发的场景下,都有其特殊的用途,需要综合利用多级的缓存,才能支撑住高并发场景下各种各样的特殊情况

服务本地堆缓存,作用,预防redis层的彻底崩溃,作为缓存的最后一道防线,避免数据库直接裸奔

spring boot+mybatis+redis框架整合搭建

pom文件配置:

application配置:

import java.util.HashSet;import java.util.Set;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.tomcat.jdbc.pool.DataSource;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;@EnableAutoConfiguration@SpringBootApplication@ComponentScan@MapperScan("com.roncoo.eshop.cache.mapper")public class Application { @Bean @ConfigurationProperties(prefix="spring.datasource") public DataSource dataSource() { return new org.apache.tomcat.jdbc.pool.DataSource(); } @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } @Bean public JedisCluster JedisClusterFactory() { Set jedisClusterNodes = new HashSet(); jedisClusterNodes.add(new HostAndPort("192.168.31.19", 7003)); jedisClusterNodes.add(new HostAndPort("192.168.31.19", 7004)); jedisClusterNodes.add(new HostAndPort("192.168.31.227", 7006)); JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes); return jedisCluster; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.ehcache.EhCacheCacheManager;import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;//缓存配置管理类@Configuration@EnableCachingpublic class CacheConfiguration { @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){ EhCacheManagerFactoryBean ehCacheManagerFactoryBean= new EhCacheManagerFactoryBean(); ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml")); ehCacheManagerFactoryBean.setShared(true); return ehCacheManagerFactoryBean; } @Bean public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean ehCacheManagerFactoryBean){ return new EhCacheCacheManager(ehCacheManagerFactoryBean.getObject()); }}

import com.roncoo.eshop.model.ProductInfo;//缓存service接口public interface CacheService { //将商品信息保存到本地缓存中 public ProductInfo saveLocalCache(ProductInfo productInfo); //从本地缓存中获取商品信息 public ProductInfo getLocalCache(Long id);}

import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import com.roncoo.eshop.model.ProductInfo;import com.roncoo.eshop.service.CacheService;//缓存Service实现类@Service("cacheService")public class CacheServiceImpl implements CacheService { public static final String CACHE_NAME ="local"; //将商品信息保存到本地缓存中 @CachePut(value = CACHE_NAME, key = "'key_'+#productInfo.getId()") public ProductInfo saveLocalCache(ProductInfo productInfo) { return productInfo; } // 从本地缓存中获取商品信息 @Cacheable(value = CACHE_NAME, key = "'key_'+#id") public ProductInfo getLocalCache(Long id) { return null; }}

import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.roncoo.eshop.model.ProductInfo;import com.roncoo.eshop.service.CacheService;@Controllerpublic class CacheController { @Resource private CacheService cacheService; @RequestMapping("/testPutCache") @ResponseBody public String testPutCache(ProductInfo productInfo) { cacheService.saveLocalCache(productInfo); return "success"; } @RequestMapping("/testGetCache") @ResponseBody public ProductInfo testGetCache(Long id) { return cacheService.getLocalCache(id); }}

redis是会在数据达到一定程度之后,超过了一个最大的限度之后,就会将数据进行一定的清理,从内存中清理掉一些数据

只有清理掉一些数据之后,才能将新的数据写入内存中

1、LRU算法概述

redis默认情况下就是使用LRU策略的,因为内存是有限的,但是如果你不断地往redis里面写入数据,那肯定是没法存放下所有的数据在内存的

所以redis默认情况下,当内存中写入的数据很满之后,就会使用LRU算法清理掉部分内存中的数据,腾出一些空间来,然后让新的数据写入redis缓存中

LRU:Least Recently Used,最近最少使用算法

将最近一段时间内,最少使用的一些数据,给干掉。比如说有一个key,在最近1个小时内,只被访问了一次; 还有一个key在最近1个小时内,被访问了1万次

2、缓存清理设置

redis.conf

maxmemory,设置redis用来存放数据的最大的内存大小,一旦超出这个内存大小之后,就会立即使用LRU算法清理掉部分数据

如果用LRU,那么就是将最近最少使用的数据从缓存中清除出去

对于64 bit的机器,如果maxmemory设置为0,那么就默认不限制内存的使用,直到耗尽机器中所有的内存为止; 但是对于32 bit的机器,有一个隐式的闲置就是3GB

maxmemory-policy,可以设置内存达到最大闲置后,采取什么策略来处理

(1)noeviction: 如果内存使用达到了maxmemory,client还要继续写入数据,那么就直接报错给客户端

(2)allkeys-lru: 就是我们常说的LRU算法,移除掉最近最少使用的那些keys对应的数据

(3)volatile-lru: 也是采取LRU算法,但是仅仅针对那些设置了指定存活时间(TTL)的key才会清理掉

(4)allkeys-random: 随机选择一些key来删除掉

(5)volatile-random: 随机选择一些设置了TTL的key来删除掉

(6)volatile-ttl: 移除掉部分keys,选择那些TTL时间比较短的keys

在redis里面,写入key-value对的时候,是可以设置TTL,存活时间,比如你设置了60s。那么一个key-value对,在60s之后就会自动被删除

3、缓存清理的流程

(1)客户端执行数据写入操作

(2)redis server接收到写入操作之后,检查maxmemory的限制,如果超过了限制,那么就根据对应的policy清理掉部分数据

(3)写入操作完成执行

4、redis的LRU近似算法

redis采取的是LRU近似算法,也就是对keys进行采样,然后在采样结果中进行数据清理

redis 3.0开始,在LRU近似算法中引入了pool机制,表现可以跟真正的LRU算法相当,但是还是有所差距的,不过这样可以减少内存的消耗

redis LRU算法,是采样之后再做LRU清理的,跟真正的、传统、全量的LRU算法是不太一样的

maxmemory-samples,比如5,可以设置采样的大小,如果设置为10,那么效果会更好,不过也会耗费更多的CPU资源

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python算法
下一篇:python requests 正则爬虫
相关文章

 发表评论

暂时没有评论,来抢沙发吧~