创新的专业网站建设,wordpress搬家dz论坛,大厂县住房和城乡建设局网站,泰安网站建设找工作什么叫缓存穿透?
模拟一个场景:
前端用户发送请求获取数据,后端首先会在缓存Redis中查询,如果能查到数据,则直接返回.如果缓存中查不到数据,则要去数据库查询,如果数据库有,将数据保存到Redis缓存中并且返回用户数据.如果数据库没有则返回null;
这个缓存穿透的问题就是这个…什么叫缓存穿透?
模拟一个场景:
前端用户发送请求获取数据,后端首先会在缓存Redis中查询,如果能查到数据,则直接返回.如果缓存中查不到数据,则要去数据库查询,如果数据库有,将数据保存到Redis缓存中并且返回用户数据.如果数据库没有则返回null;
这个缓存穿透的问题就是这个返回的null上面,如果客户端恶意频繁的发起Redis不存在的Key,且数据库中也不存在的数据,返回永远是null.当洪流式的请求过来,给数据库造成极大压力,甚至压垮数据库.它永远越过Redis缓存而直接访问数据库,这个过程就是缓存穿透.
其实是个设计上的缺陷. 缓存穿透解决方案
业界比较成熟的一种解决方案:当越过缓存,且数据库没有该数据返回客户端null并且存到Redis,数据是null,给这个Key设置过期时间.这种方案一定程度上减少数据库频繁查询的压力. 实战过程 CREATE TABLE item ( id int(11) NOT NULL AUTO_INCREMENT, code varchar(255) DEFAULT NULL COMMENT 商品编号, name varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT 商品名称, create_time datetime DEFAULT NULL, PRIMARY KEY (id) ) ENGINEInnoDB AUTO_INCREMENT2 DEFAULT CHARSETutf8 COMMENT商品信息表; INSERT INTO item VALUES (1, book_10010, Redis缓存穿透实战, 2019-03-17 17:21:16); 项目整体结构 依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.2/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIdredis1/artifactIdversion0.0.1-SNAPSHOT/versionnameredis1/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.3.0/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter-test/artifactIdversion3.0.3/versionscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project启动类
SpringBootApplication
MapperScan({com.example.redis1.mapper})
public class Redis1Application {public static void main(String[] args) {SpringApplication.run(Redis1Application.class, args);}}
application.yml
server:port: 80
spring:application:name: redis-testredis:##redis 单机环境配置##将docker脚本部署的redis服务映射为宿主机ip##生产环境推荐使用阿里云高可用redis服务并设置密码host: 127.0.0.1port: 6379password:database: 0ssl: false##redis 集群环境配置#cluster:# nodes: 127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003# commandTimeout: 5000datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://1111111:3306/redis-test?useSSLfalseuseUnicodetruecharacterEncodingUTF-8autoReconnecttrueserverTimezoneGMT%2B8useCursorFetchtrueusername: xxxxpassword: xxxxxxxx
mybatis:mapper-locations: classpath:mappers/*Mapper.xml # 指定mapper文件位置type-aliases-package: com.example.redis1.pojoconfiguration:map-underscore-to-camel-case: true
logging:level:com.example.redis1.mapper: debug
数据库映射xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.redis1.mapper.ItemMapper resultMap idBaseResultMap typecom.example.redis1.pojo.Item id columnid propertyid jdbcTypeINTEGER /result columncode propertycode jdbcTypeVARCHAR /result columnname propertyname jdbcTypeVARCHAR /result columncreate_time propertycreateTime jdbcTypeTIMESTAMP //resultMapsql idBase_Column_List id, code, name, create_time/sqlselect idselectByPrimaryKey resultMapBaseResultMap parameterTypejava.lang.Integer selectinclude refidBase_Column_List /from itemwhere id #{id,jdbcTypeINTEGER}/selectdelete iddeleteByPrimaryKey parameterTypejava.lang.Integer delete from itemwhere id #{id,jdbcTypeINTEGER}/deleteinsert idinsert parameterTypeitem insert into item (id, code, name,create_time)values (#{id,jdbcTypeINTEGER}, #{code,jdbcTypeVARCHAR}, #{name,jdbcTypeVARCHAR},#{createTime,jdbcTypeTIMESTAMP})/insertinsert idinsertSelective parameterTypeitem insert into itemtrim prefix( suffix) suffixOverrides, if testid ! null id,/ifif testcode ! null code,/ifif testname ! null name,/ifif testcreateTime ! null create_time,/if/trimtrim prefixvalues ( suffix) suffixOverrides, if testid ! null #{id,jdbcTypeINTEGER},/ifif testcode ! null #{code,jdbcTypeVARCHAR},/ifif testname ! null #{name,jdbcTypeVARCHAR},/ifif testcreateTime ! null #{createTime,jdbcTypeTIMESTAMP},/if/trim/insertupdate idupdateByPrimaryKeySelective parameterTypeitem update itemset if testcode ! null code #{code,jdbcTypeVARCHAR},/ifif testname ! null name #{name,jdbcTypeVARCHAR},/ifif testcreateTime ! null create_time #{createTime,jdbcTypeTIMESTAMP},/if/setwhere id #{id,jdbcTypeINTEGER}/updateupdate idupdateByPrimaryKey parameterTypeitem update itemset code #{code,jdbcTypeVARCHAR},name #{name,jdbcTypeVARCHAR},create_time #{createTime,jdbcTypeTIMESTAMP}where id #{id,jdbcTypeINTEGER}/update!--根据商品编码查询--select idselectByCode resultTypeitemselectinclude refidBase_Column_List /from itemwhere code #{code}/select/mapper
pojo
package com.example.redis1.pojo;import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;import java.util.Date;Data
public class Item {private Integer id;private String code;private String name;JsonFormat(pattern yyyy-MM-dd HH:mm:ss,timezone GMT8)private Date createTime;}mapper
package com.example.redis1.mapper;import com.example.redis1.pojo.Item;
import org.apache.ibatis.annotations.Param;public interface ItemMapper {int deleteByPrimaryKey(Integer id);int insert(Item record);int insertSelective(Item record);Item selectByPrimaryKey(Integer id);int updateByPrimaryKeySelective(Item record);int updateByPrimaryKey(Item record);Item selectByCode(Param(code) String code);
}service
package com.example.redis1.service;import com.example.redis1.mapper.ItemMapper;
import com.example.redis1.pojo.Item;
import com.fasterxml.jackson.databind.ObjectMapper;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;/*** 缓存穿透service* Created by Administrator on 2019/3/17.*/
Service
public class CachePassService {private static final Logger log LoggerFactory.getLogger(CachePassService.class);Autowiredprivate ItemMapper itemMapper;Autowiredprivate RedisTemplate redisTemplate;Autowiredprivate ObjectMapper objectMapper;private static final String keyPrefixitem:;/*** 获取商品详情-如果缓存有则从缓存中获取如果没有则从数据库查询并将查询结果塞入缓存中* param itemCode* return* throws Exception*/public Item getItemInfo(String itemCode) throws Exception{Item itemnull;final String keykeyPrefixitemCode;ValueOperations valueOperationsredisTemplate.opsForValue();if (redisTemplate.hasKey(key)){log.info(---获取商品详情-缓存中存在该商品---商品编号为{} ,itemCode);//从缓存中查询该商品详情Object resvalueOperations.get(key);if (res!null!(res.equals())){itemobjectMapper.readValue(res.toString(),Item.class);}}else{log.info(---获取商品详情-缓存中不存在该商品-从数据库中查询---商品编号为{} ,itemCode);//从数据库中获取该商品详情itemitemMapper.selectByCode(itemCode);if (item!null){valueOperations.set(key,objectMapper.writeValueAsString(item));}else{//过期失效时间TTL设置为30分钟-当然实际情况要根据实际业务决定valueOperations.set(key,,30L, TimeUnit.MINUTES);}}return item;}
}controller
package com.example.redis1.controller;import com.example.redis1.service.CachePassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** 缓存穿透实战* Author:debug (SteadyJack)* Date: 2019/3/17 18:33**/
RestController
public class CachePassController {private static final Logger log LoggerFactory.getLogger(CachePassController.class);private static final String prefixcache/pass;Autowiredprivate CachePassService cachePassService;/*** 获取热销商品信息* param itemCode* return*/RequestMapping(value prefix/item/info,method RequestMethod.GET)public MapString,Object getItem(RequestParam String itemCode){MapString,Object resMapnew HashMap();resMap.put(code,0);resMap.put(msg,成功);try {resMap.put(data,cachePassService.getItemInfo(itemCode));}catch (Exception e){resMap.put(code,-1);resMap.put(msg,失败e.getMessage());}return resMap;}
} 第一次访问
localhost/cache/pass/item/info?itemCodebook_10010 查看日志输出 用个数据库不存在的
localhost/cache/pass/item/info?itemCodebook_10012 后端的处理是将不存在的key存到redis并指定过期时间 其他典型问题介绍
缓存雪崩:指的的某个时间点,缓存中的Key集体发生过期失效,导致大量查询的请求落到数据库上,导致数据库负载过高,压力暴增的现象
解决方案:设置错开不同的过期时间 缓存击穿:指缓存中某个频繁被访问的Key(热点Key),突然过期时间到了失效了,持续的高并发访问瞬间就像击破缓存一样瞬间到达数据库。
解决办法:设置热点Key永不过期