当前位置: 首页 > news >正文

做医疗护具网站微信网站建设app公司

做医疗护具网站,微信网站建设app公司,娄底网站建设公司有哪些,wordpress 微博 同步目录#xff1a; 目录 一#xff0c;SSM整合redis 二#xff0c;redis注解式缓存 三#xff0c;Redis中的缓存穿透、雪崩、击穿的原因以及解决方案#xff08;附图#xff09; 一#xff0c;SSM整合redis 1.原因#xff1a; 整合SSM和Redis可以提升系统的性能、可… 目录 目录 一SSM整合redis 二redis注解式缓存 三Redis中的缓存穿透、雪崩、击穿的原因以及解决方案附图 一SSM整合redis 1.原因 整合SSM和Redis可以提升系统的性能、可伸缩性和可靠性在分布式环境下更好地支持会话管理、消息队列和分布式锁等功能。 2.步骤可以参考SSM整合mysql 导入pom依赖在Maven中添加Redis的依赖 redis.version2.9.0/redis.version redis.spring.version1.7.1.RELEASE/redis.spring.versiondependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion${redis.version}/version /dependency dependencygroupIdorg.springframework.data/groupIdartifactIdspring-data-redis/artifactIdversion${redis.spring.version}/version /dependency 2.2.spring-redis.xml的相关配置 redis.properties redis.hostName127.0.0.1 redis.port6379 redis.password123456 redis.timeout10000 redis.maxIdle300 redis.maxTotal1000 redis.maxWaitMillis1000 redis.minEvictableIdleTimeMillis300000 redis.numTestsPerEvictionRun1024 redis.timeBetweenEvictionRunsMillis30000 redis.testOnBorrowtrue redis.testWhileIdletrue redis.expiration3600 spring-redis.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:cachehttp://www.springframework.org/schema/cachexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd!-- 1. 引入properties配置文件 --!--context:property-placeholder locationclasspath:redis.properties /--!-- 2. redis连接池配置--bean idpoolConfig classredis.clients.jedis.JedisPoolConfig!--最大空闲数--property namemaxIdle value${redis.maxIdle}/!--连接池的最大数据库连接数 --property namemaxTotal value${redis.maxTotal}/!--最大建立连接等待时间--property namemaxWaitMillis value${redis.maxWaitMillis}/!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)--property nameminEvictableIdleTimeMillis value${redis.minEvictableIdleTimeMillis}/!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3--property namenumTestsPerEvictionRun value${redis.numTestsPerEvictionRun}/!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1--property nametimeBetweenEvictionRunsMillis value${redis.timeBetweenEvictionRunsMillis}/!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个--property nametestOnBorrow value${redis.testOnBorrow}/!--在空闲时检查有效性, 默认false --property nametestWhileIdle value${redis.testWhileIdle}//bean!-- 3. redis连接工厂 --bean idconnectionFactory classorg.springframework.data.redis.connection.jedis.JedisConnectionFactorydestroy-methoddestroyproperty namepoolConfig refpoolConfig/!--IP地址 --property namehostName value${redis.hostName}/!--端口号 --property nameport value${redis.port}/!--如果Redis设置有密码 --property namepassword value${redis.password}/!--客户端超时时间单位是毫秒 --property nametimeout value${redis.timeout}//bean!-- 4. redis操作模板,使用该对象可以操作redishibernate课程中hibernatetemplete相当于session专门操作数据库。--bean idredisTemplate classorg.springframework.data.redis.core.RedisTemplateproperty nameconnectionFactory refconnectionFactory/!--如果不配置Serializer那么存储的时候缺省使用String如果用User类型存储那么会提示错误User cant cast to String --property namekeySerializerbean classorg.springframework.data.redis.serializer.StringRedisSerializer//propertyproperty namevalueSerializerbean classorg.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer//propertyproperty namehashKeySerializerbean classorg.springframework.data.redis.serializer.StringRedisSerializer//propertyproperty namehashValueSerializerbean classorg.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer//property!--开启事务 --property nameenableTransactionSupport valuetrue//bean!-- 5.配置缓存管理器 --bean idredisCacheManager classorg.springframework.data.redis.cache.RedisCacheManagerconstructor-arg nameredisOperations refredisTemplate/!--redis缓存数据过期时间单位秒--property namedefaultExpiration value${redis.expiration}/!--是否使用缓存前缀与cachePrefix相关--property nameusePrefix valuetrue/!--配置缓存前缀名称--property namecachePrefixbean classorg.springframework.data.redis.cache.DefaultRedisCachePrefixconstructor-arg index0 value-cache-//bean/property/bean!--6.配置缓存生成键名的生成规则--bean idcacheKeyGenerator classcom.zking.ssm.redis.CacheKeyGenerator/bean!--7.启用缓存注解功能--cache:annotation-driven cache-managerredisCacheManager key-generatorcacheKeyGenerator/ /beans 2.3.修改applicationContext.xml 如果spring配置文件中需要配置两个及以上的properties文件则需要在applicationContext.xml中进行配置处理否则会出现覆盖的情况。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/context xmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd!--1. 引入外部多文件方式 --bean idpropertyConfigurerclassorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namesystemPropertiesModeName valueSYSTEM_PROPERTIES_MODE_OVERRIDE /property nameignoreResourceNotFound valuetrue /property namelocationslistvalueclasspath:jdbc.properties/valuevalueclasspath:redis.properties/value/list/property/bean!-- 随着后续学习框架会越学越多不能将所有的框架配置放到同一个配制间否者不便于管理 --import resourceapplicationContext-mybatis.xml/importimport resourcespring-redis.xml/importimport resourceapplicationContext-shiro.xml/import /beans 2.4.配置redis的key生成策略 CacheKeyGenerator.java package com.zking.ssm.redis;import lombok.extern.slf4j.Slf4j; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.util.ClassUtils;import java.lang.reflect.Array; import java.lang.reflect.Method;Slf4j public class CacheKeyGenerator implements KeyGenerator {// custom cache keypublic static final int NO_PARAM_KEY 0;public static final int NULL_PARAM_KEY 53;Overridepublic Object generate(Object target, Method method, Object... params) {StringBuilder key new StringBuilder();key.append(target.getClass().getSimpleName()).append(.).append(method.getName()).append(:);if (params.length 0) {key.append(NO_PARAM_KEY);} else {int count 0;for (Object param : params) {if (0 ! count) {//参数之间用,进行分隔key.append(,);}if (param null) {key.append(NULL_PARAM_KEY);} else if (ClassUtils.isPrimitiveArray(param.getClass())) {int length Array.getLength(param);for (int i 0; i length; i) {key.append(Array.get(param, i));key.append(,);}} else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) {key.append(param);} else {//Java一定要重写hashCode和eqaulskey.append(param.hashCode());}count;}}String finalKey key.toString(); // IEDA要安装lombok插件log.debug(using cache key{}, finalKey);return finalKey;} } 二redis注解式缓存 关于什么是Redis注解式 注解式缓存是一种通过在方法上添加特定的注解来实现自动缓存的机制。在Java开发中我们可以使用Spring框架提供的Cacheable、CachePut和CacheEvict等注解来实现对Redis的缓存操作。 Cacheable注解 作用标记方法的返回值可以被缓存并且在下次调用该方法时会直接从缓存中获取结果而不会执行方法体内的逻辑。使用该注解会将查询结果放入Redis中下一次同样的查询就不会走数据库而是走Redis.示例代码Cacheable(value myCache, key #id) public User getUserById(String id) {// 从数据库或其他数据源获取用户信息的逻辑return user; }解释以上代码表示将方法的返回值以键值对的形式缓存在减少了数据访问对MySQL数据库的压力 CachePut注解 作用标记方法的返回值需要更新缓存即每次调用方法都会执行方法体内的逻辑并将返回值存入缓存。示例代码 CachePut(value myCache, key #user.id) public User updateUser(User user) {// 更新用户信息的逻辑return user; }解释以上代码表示每次调用该方法都会执行方法体内的逻辑该注解会将查询结果放入Redis中类似于更新操作即每次不管缓存中有没有结果都从数据库查找结果并将结果更新到缓存并返回结果 CacheEvict注解 作用标记方法会清除缓存中的指定数据。示例代码CacheEvict(value myCache, key #id) public void deleteUser(String id) {// 删除用户信息的逻辑 }解释以上代码表示调用该方法时会从名为myCache的缓存中移除key为参数id对应的缓存数据。 CachePut和Cacheable的区别 Cacheable和CachePut都是Spring框架中用于缓存的注解它们的主要区别是 Cacheable用于获取缓存中的数据如果缓存不存在就会执行方法并将返回值放入缓存中而CachePut用于更新缓存中的数据它每次都会执行方法并将返回值放入缓存中。 Cacheable和CachePut的key生成方式不同Cacheable默认使用方法的参数作为key可以通过key属性指定key的生成方式或使用SpEL表达式自定义key的生成方式而CachePut默认使用Cacheable的默认key生成方式也可以通过key属性指定key的生成方式。 Cacheable和CachePut的Sync属性也不同Cacheable默认为false即异步处理而CachePut默认为true即同步处理。   通过使用这些注解我们可以更加方便地实现对Redis的缓存操作减少了手动管理缓存的复杂性。但需要注意的是使用注解式缓存时需要确保被缓存的方法的输入参数和返回值类型是可序列化的以便在Redis中进行存储和读取。 此外还可以通过配置Spring框架的缓存管理器、缓存策略等来进一步优化和配置缓存行为。具体的配置和使用细节可以参考Spring框架的文档和教程。 三Redis中的缓存穿透、雪崩、击穿的原因以及解决方案附图 缓存穿透Cache Penetration 原因当请求查询一个不存在于缓存和数据库中的数据时每次查询都会直接访问数据库导致对数据库的频繁访问增加数据库负载。情景客户端发送大量的不可响应的请求比如发送一个id为-999的用户解决方案 布隆过滤器Bloom Filter用于判断请求的数据是否存在于缓存或数据库中。使用布隆过滤器可以快速过滤掉一部分不存在的数据避免对数据库的无效查询。空值缓存Null Object Caching将不存在的数据也缓存起来但设置一个较短的过期时间以便下次查询时可以从缓存中获取。 注意事项 使用空值作为缓存的时候key设置的过期时间不能太长防止占用太多redis资源 对空值缓存是一种被动的防御方式当遇到黑客暴力请求很多不存在的数据就需要写入大量的null值到Redis中可能导致Redis内存占用不足的情况 使用布隆过滤器可以在用户访问的时候判断该资源是否存在不存在则直接拒绝访问 布隆过滤器是有一定的误差所以一般需要配合一些接口流量的限制规定用户在一段时间内访问的频率、权限校验、黑名单等来解决缓存穿透的问题 缓存雪崩Cache Avalanche 原因当缓存中的大量数据同时过期或失效时所有请求都会直接访问数据库导致数据库压力骤增甚至导致数据库宕机。解决方案 随机过期时间Randomized Expiration为缓存中的数据设置随机的过期时间以避免大量数据同时过期。并发控制Concurrency Control使用分布式锁或互斥机制确保只有一个线程可以重新加载缓存数据避免重复查询。 缓存击穿Cache Miss 原因当某个热点数据过期或失效时大量请求同时涌入导致并发查询数据库增加数据库压力。解决方案 互斥锁Mutex Lock在缓存失效的情况下使用互斥锁来确保只有一个请求可以查询数据库并将查询结果进行缓存其他请求等待缓存更新后再获取数据。提前加载Preloading提前加载热点数据到缓存中并设置较长的过期时间以避免热点数据过期后被击穿。
http://www.w-s-a.com/news/577436/

相关文章:

  • 医院网站如何备案东莞优化公司收费
  • 罗村网站开发适合ps做图的素材网站有哪些
  • 网站建设中 油财宝企业网址怎么整
  • asp.net空网站php网站开发要学什么
  • 做可视化的网站微信网站模版下载
  • 包头移动的网站建设茂名建站价格
  • 网站文章内容一键排版功能铜山网站建设
  • cdr可不可做网站对网站建设起到计划和指导的作用
  • 合肥最好的网站建设网页设计心得体会2000字
  • 西安网站品牌建设门户网站类型
  • 网上做调查问卷的网站请人做网站域名和主机
  • 个人网站模板html5找公司网站建设
  • 找最新游戏做视频网站一个做网站的团队需要哪些人员
  • 威海市做网站的做网站很难吗
  • 广州房地产网站建设方案怎么免费申请网站
  • 免费生成网站软件下载影视公司名字取名
  • 网站公司提供程序免费的网页入口
  • jsp网站开发实例教学房产网站怎么做400电话
  • 网络营销方式及流程广州seo工作
  • 专业商城网站制作免费网页设计成品
  • 韩国优秀设计网站找做网站找那个平台做
  • 贵州省清镇市建设学校网站国家企业信用信息公示系统官网河北
  • 游戏界面设计网站网站建设问一问公司
  • 织梦网站模板如何安装教程视频国外哪些网站可以注册域名
  • 用群晖做网站网站中文名称注册
  • 做一个企业网站需要哪些技术app开发公司名字
  • 网站建设有技术的公司图片在线设计平台
  • 建公司网站的详细步骤关于进一步加强网站建设
  • 丰宁县有做网站的吗?维护一个网站一年多少钱
  • 杭州网站设计渠道wordpress购物主题