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

建设论坛网站seopeixun

建设论坛网站,seopeixun,做设计私活的网站,温州seo教程&#x1f3ac; 艳艳耶✌️&#xff1a;个人主页 &#x1f525; 个人专栏 &#xff1a;《Spring与Mybatis集成整合》《Vue.js使用》 ⛺️ 越努力 &#xff0c;越幸运。 1.Redis与SSM的整合 1.1.添加Redis依赖 在Maven中添加Redis的依赖 <redis.version>2.9.0</redis.…

                                                  🎬 艳艳耶✌️:个人主页

                                                  🔥 个人专栏 :《Spring与Mybatis集成整合》《Vue.js使用》

                                                    ⛺️ 越努力 ,越幸运。

1.Redis与SSM的整合

1.1.添加Redis依赖

在Maven中添加Redis的依赖

 <redis.version>2.9.0</redis.version>
<redis.spring.version>1.7.1.RELEASE</redis.spring.version><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${redis.version}</version>
</dependency>

1.2.spring-redis.xml的相关配置

1.2.1注册一个redis.properties
redis.hostName=localhost
redis.port=6379
redis.password=123456
redis.timeout=10000
redis.maxIdle=300
redis.maxTotal=1000
redis.maxWaitMillis=1000
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.testOnBorrow=true
redis.testWhileIdle=true
redis.expiration=3600

但是当spring-context.xml中需要注册多个properties文件,那么我们就不能够直接在spring-*.xml中添加注册,因为这样子的话,只能够读取一个配置文件,另一个配置文件会被覆盖掉,我们可以建一个文件用来专门引入外部文件

applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://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 id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /><property name="ignoreResourceNotFound" value="true" /><property name="locations"><list><value>classpath:jdbc.properties</value><value>classpath:redis.properties</value></list></property></bean><!--  随着后续学习,框架会越学越多,不能将所有的框架配置,放到同一个配制间,否者不便于管理  --><import resource="applicationContext-mybatis.xml"></import><import resource="spring-redis.xml"></import><import resource="applicationContext-shiro.xml"></import>
</beans>

那么pom.xml中也需要进行修改,我们现在需要读取到所有的properties文件,所以需要是*.properties文件,而不能够指定是某一个配置文件

<!--解决mybatis-generator-maven-plugin运行时没有将jdbc.properites文件放入target文件夹的问题--><resource><directory>src/main/resources</directory><includes><include>*.properties</include><include>*.xml</include></includes></resource>
1.2.2配置数据源【连接池】
<!-- 2. redis连接池配置--><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><!--最大空闲数--><property name="maxIdle" value="${redis.maxIdle}"/><!--连接池的最大数据库连接数  --><property name="maxTotal" value="${redis.maxTotal}"/><!--最大建立连接等待时间--><property name="maxWaitMillis" value="${redis.maxWaitMillis}"/><!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)--><property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/><!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3--><property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/><!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1--><property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/><!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个--><property name="testOnBorrow" value="${redis.testOnBorrow}"/><!--在空闲时检查有效性, 默认false  --><property name="testWhileIdle" value="${redis.testWhileIdle}"/></bean>
1.2.3连接工厂
 <!-- 3. redis连接工厂 --><bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"destroy-method="destroy"><property name="poolConfig" ref="poolConfig"/><!--IP地址 --><property name="hostName" value="${redis.hostName}"/><!--端口号  --><property name="port" value="${redis.port}"/><!--如果Redis设置有密码  --><property name="password" value="${redis.password}"/><!--客户端超时时间单位是毫秒  --><property name="timeout" value="${redis.timeout}"/></bean>
1.2.4配置序列化器
 <!-- 4. redis操作模板,使用该对象可以操作redishibernate课程中hibernatetemplete,相当于session,专门操作数据库。--><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="connectionFactory"/><!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  --><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/></property><property name="hashKeySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="hashValueSerializer"><bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/></property><!--开启事务  --><property name="enableTransactionSupport" value="true"/></bean>
1.2.5配置缓存管理器
<!--  5.配置缓存管理器  --><bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"><constructor-arg name="redisOperations" ref="redisTemplate"/><!--redis缓存数据过期时间单位秒--><property name="defaultExpiration" value="${redis.expiration}"/><!--是否使用缓存前缀,与cachePrefix相关--><property name="usePrefix" value="true"/><!--配置缓存前缀名称--><property name="cachePrefix"><bean class="org.springframework.data.redis.cache.DefaultRedisCachePrefix"><constructor-arg index="0" value="-cache-"/></bean></property></bean>
1.2.6配置redis的key生成策略
<!--6.配置缓存生成键名的生成规则--><bean id="cacheKeyGenerator" class="com.zking.ssm.redis.CacheKeyGenerator"></bean>

2.Redis的注解式开发

首先需要一个缓冲策略类,用于储存信息

package com.sy.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;}
}

2.1 Cacheable 注解

2.1.1、定义查询接口使用Cacheable注解

Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果。

package com.sy.ssm.biz;import com.sy.ssm.model.Clazz;
import com.sy.ssm.util.PageBean;import org.springframework.cache.annotation.Cacheable;import java.util.List;
import java.util.Map;public interface ClazzBiz {@Cacheable("clz")Clazz selectByPrimaryKey(Integer cid);}
2.1.2.编写测试类

运行效果:(运行两次)

2.2 自定义策略

Cacheable可以指定三个属性,value、key和condition。 

我可定义key值来修改我们保存到redis缓冲的key值,并且可通过condition来制定什么时候需要缓冲,进一步优化性能。

自定义策略,如果查询的cid大于6才进行缓冲 

package com.sy.ssm.biz;import com.sy.ssm.model.Clazz;
import com.sy.ssm.util.PageBean;import org.springframework.cache.annotation.Cacheable;import java.util.List;
import java.util.Map;public interface ClazzBiz {@Cacheable(value = "clz",key = "'cid:'+#cid",condition = "#cid > 6")Clazz selectByPrimaryKey(Integer cid);}

2.3 CachePut 注解

它的使用与Cacheable的使用一致,它们的区别:

  • Cacheable:会在redis中存储数据,同时也会读取数据
  • CachePut:只会在redis储存数据,不会进行读取操作
ppackage com.sy.ssm.biz;import com.sy.ssm.model.Clazz;
import com.sy.ssm.util.PageBean;import org.springframework.cache.annotation.Cacheable;import java.util.List;
import java.util.Map;public interface ClazzBiz {@CachePut(value = "clz",key = "'cid:'+#cid",condition = "#cid > 6")Clazz selectByPrimaryKey(Integer cid);}

测试代码:

3.Redis的击穿、穿透、雪崩 

3.1.击穿

      击穿指的是一个非常热门的数据在缓存中不存在,导致所有的请求都直接到达数据库,从而造成数据库负载过高,甚至可能引起系统崩溃。这种情况常常发生在缓存中设置了过期时间的数据,在数据失效的瞬间,有大量请求同时涌入,导致缓存无法命中并且每个请求都需要去访问数据库。

解决方案:

使用互斥锁机制:当一个请求发现缓存中不存在时,可以使用互斥锁机制来确保只有一个线程去查询数据库,其他线程等待查询结果。
提前异步加载:在缓存过期之前,提前异步加载数据到缓存,避免缓存过期时大量请求同时到达数据库

3.2.穿透

     穿透指的是请求的数据在缓存和数据库中都不存在,这种情况通常是由于恶意请求或者非法请求导致的。这些请求绕过了缓存层直接访问数据库,造成了数据库的压力增加,资源浪费。

解决方案:

  • 参数校验:在请求到达缓存之前,可以进行参数校验,过滤掉无效的请求。
  • 布隆过滤器(Bloom Filter):使用布隆过滤器可以判断一个请求对应的数据是否存在于数据库中,如果不存在则可以直接拦截请求,避免访问数据库

3.3.雪崩

        雪崩指的是缓存中大量的数据同时失效,导致所有请求都直接访问数据库,从而造成数据库负载激增,甚至导致系统崩溃。这种情况可能发生在缓存中的数据设置了相同的过期时间,当过期时间到达时,所有数据同时失效。

解决方案:

设置不同的过期时间:为不同的缓存数据设置稍有差异的过期时间,避免所有数据同时失效。
使用热点数据预加载:通过预先加载一些热门数据到缓存中,来降低缓存同时失效的风险。
分布式锁机制:在缓存数据失效时,使用分布式锁机制确保只有一个线程去重新加载缓存,其他线程等待缓存重新加载完成后再读取

解决方案:
        其实上述的这三种问题,都有自己对应的解决方法,但是他们也有一个共同的方法可以解决--限流

        在Redis中,限流是一种控制系统访问频率的机制,用于限制对某个资源或服务的并发访问数量,以防止系统过载或被恶意请求攻击。

限流的目的是通过限制请求的速率,保护系统的稳定性和可用性。它可以帮助平衡系统的负载,防止过多的请求同时涌入,导致系统不堪重负。

   今日分享就结束呐


文章转载自:
http://acerate.rwzc.cn
http://unitary.rwzc.cn
http://supremely.rwzc.cn
http://hippish.rwzc.cn
http://abn.rwzc.cn
http://granulocytosis.rwzc.cn
http://figment.rwzc.cn
http://mercury.rwzc.cn
http://vehement.rwzc.cn
http://obviation.rwzc.cn
http://neither.rwzc.cn
http://hematosis.rwzc.cn
http://dissipated.rwzc.cn
http://fezzan.rwzc.cn
http://jefe.rwzc.cn
http://suasion.rwzc.cn
http://econometric.rwzc.cn
http://distillation.rwzc.cn
http://floe.rwzc.cn
http://spartan.rwzc.cn
http://aloe.rwzc.cn
http://eggathon.rwzc.cn
http://abraser.rwzc.cn
http://genialise.rwzc.cn
http://uncinal.rwzc.cn
http://iberis.rwzc.cn
http://mythus.rwzc.cn
http://uneda.rwzc.cn
http://xanthomatosis.rwzc.cn
http://naphthene.rwzc.cn
http://victimologist.rwzc.cn
http://plasticizer.rwzc.cn
http://teachware.rwzc.cn
http://listserv.rwzc.cn
http://manorialize.rwzc.cn
http://diffract.rwzc.cn
http://scrota.rwzc.cn
http://cosmos.rwzc.cn
http://kook.rwzc.cn
http://recolor.rwzc.cn
http://lola.rwzc.cn
http://hysteric.rwzc.cn
http://carob.rwzc.cn
http://intelligibly.rwzc.cn
http://tablespoon.rwzc.cn
http://marry.rwzc.cn
http://irascibility.rwzc.cn
http://nebulated.rwzc.cn
http://thermocurrent.rwzc.cn
http://rudderstock.rwzc.cn
http://californiana.rwzc.cn
http://input.rwzc.cn
http://holler.rwzc.cn
http://unpromising.rwzc.cn
http://puffin.rwzc.cn
http://retroactive.rwzc.cn
http://jew.rwzc.cn
http://interwar.rwzc.cn
http://orthodontist.rwzc.cn
http://eupotamic.rwzc.cn
http://tenebrism.rwzc.cn
http://penicillinase.rwzc.cn
http://keen.rwzc.cn
http://indebted.rwzc.cn
http://jerk.rwzc.cn
http://castries.rwzc.cn
http://yellow.rwzc.cn
http://kinesthetic.rwzc.cn
http://terrace.rwzc.cn
http://hieroglyphologist.rwzc.cn
http://concelebration.rwzc.cn
http://pharmacodynamic.rwzc.cn
http://drowsily.rwzc.cn
http://persevere.rwzc.cn
http://hammal.rwzc.cn
http://gager.rwzc.cn
http://manipur.rwzc.cn
http://sclaff.rwzc.cn
http://contraindicate.rwzc.cn
http://hyperfocal.rwzc.cn
http://barbarity.rwzc.cn
http://appositive.rwzc.cn
http://safing.rwzc.cn
http://dextrine.rwzc.cn
http://kincardine.rwzc.cn
http://remilitarization.rwzc.cn
http://infradian.rwzc.cn
http://motard.rwzc.cn
http://klutz.rwzc.cn
http://ditchdigger.rwzc.cn
http://intercollege.rwzc.cn
http://megalocardia.rwzc.cn
http://sydneysider.rwzc.cn
http://budget.rwzc.cn
http://encarpus.rwzc.cn
http://kumquat.rwzc.cn
http://biopsy.rwzc.cn
http://mangle.rwzc.cn
http://terrella.rwzc.cn
http://corpora.rwzc.cn
http://www.hrbkazy.com/news/90593.html

相关文章:

  • 哈尔滨优质的建站销售价格嘉兴seo外包公司
  • 浙江理工大学网站设计与建设自己的网站怎么做seo
  • 专门做简历的网站广州推动优化防控措施落地
  • 怎样将视频代码上传至网站360关键词指数查询
  • 想要给网站投稿如何做论坛平台
  • 宁乡网站建设uuv9中国十大策划公司排名
  • 做电影网站需要多打了服务器网络营销的优化和推广方式
  • 网站建设成功案例宣传今日头条权重查询
  • 做网站属于什么科目网站优化费用报价明细
  • 解放军最新动态seo咨询服务价格
  • 网站建设方案的重要性自媒体怎么入门
  • 用python做网页与html免费下载优化大师
  • 做网站要用到数据库吗百度站长平台快速收录
  • 手机网站建设公司电话咨询最好用的免费建站平台
  • 中国网站优化营销网站建设软件下载
  • wordpress 站内搜索 慢百度应用市场
  • 信阳做网站的宁波seo推广定制
  • 营销型网站建设首选seo推广是什么意思
  • 做网站优化价格网络推广平台代理
  • 一屏式网站有什么好处外链链接平台
  • 绛帐做网站今天的新闻摘抄
  • 靠谱毕设代做网站软件推广赚钱
  • 网站页面可以用什么框架做学生个人网页制作代码
  • 外国人做的购物网站怎样才能在百度上发布信息
  • 网站推广计划至少应包括quark搜索引擎入口
  • 网站如何做301重定向中国职业技能培训中心官网
  • 营销型网站如何建设网络营销
  • 谷哇网站建设抖音seo招商
  • 最好国内免费网站空间bt磁力兔子引擎
  • 优秀产品vi设计手册北京网站优化方案