96SEO 2026-02-23 11:46 11
创建所需的entity,Controller,service,mapper,util

spring-data-redis的jar中提供在srping应用中通过简单的配置访问redis服务的功能它对reids底层开发包进行了高度封装。
针对reids的操作包中提供了RedisTemplate类和StringRedisTemplate类其中StringRedisTemplate是RedisTemplate的子类该类只支持key和value为String的操作
RedisTemplate针对不同数据类型的操作进行封装将同一类型操作封装为Operation接口
ListOperations针对list类型的数据操作获取方式
StringRedisTemplate默认采用的是String的序列化策略保存的key和value都是采用此策略序列化保存的。
RedisTemplate默认采用的是JDK的序列化策略保存的key和value都是采用此策略序列化保存的。
跟JacksonJsonRedisSerializer实际上是一样的
JdkSerializationRedisSerializer:
序列化java对象被序列化的对象必须实现Serializable接口
GenericToStringSerializer:类似StringRedisSerializer的字符串序列化
GenericJackson2JsonRedisSerializer:类似Jackson2JsonRedisSerializer但使用时构造函数不用特定的类
--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency
--dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId/dependency
#连接池最大阻塞等待时间使用负值表示没有限制max-wait:
SpringBoot自动在容器中创建了RedisTemplate对象和StringRedisTemplate对象。
但是RedisTemplate的泛型是Object,Object进行数据处理时比价麻烦我们需要自定义一个RedisTemplate对象
1.5.x版本默认的Redis客户端是Jedis实现的SpringBoot
org.springframework.beans.factory.annotation.Autowired;
org.springframework.context.annotation.Bean;
org.springframework.context.annotation.Configuration;
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
org.springframework.data.redis.core.RedisTemplate;
org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
org.springframework.data.redis.serializer.StringRedisSerializer;
org.springframework.stereotype.Component;
RedisTemplate();StringRedisSerializer
StringRedisSerializer();GenericJackson2JsonRedisSerializer
GenericJackson2JsonRedisSerializer();
//key采用String的序列化方式redisTemplate.setKeySerializer(stringRedisSerializer);//
value序列化方式采用jacksonredisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//
hash的key也采用String的序列化方式redisTemplate.setHashKeySerializer(stringRedisSerializer);//
hash的value序列化方式采用jacksonredisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setConnectionFactory(lettuceConnectionFactory);return
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactId/dependency
org.springframework.beans.factory.annotation.Autowired;
org.springframework.boot.test.context.SpringBootTest;
org.springframework.data.redis.core.*;
1000valueOperations.set(a1,1000h);valueOperations.set(a2,哈哈哈);//
valueOperations.get(a2);System.out.println(result
result);System.out.println(result2
StudentTb();studentTb1.setId(1000);studentTb1.setName(xiaoming);studentTb1.setAge(18);
StudentTb();studentTb2.setId(1001);studentTb2.setName(lisi);studentTb2.setAge(28);
studentTb1对象listOperations.leftPush(studentList,studentTb1);listOperations.leftPush(studentList,studentTb2);
listOperations.range(studentList,
-1);System.out.println(studentList
hashOperations.put(stu1,id,1000);hashOperations.put(stu1,name,xiaoming);hashOperations.put(stu1,age,18);
StudentTb();studentTb1.setId(1000);studentTb1.setName(xiaoming);studentTb1.setAge(18);
StudentTb();studentTb2.setId(1001);studentTb2.setName(lisi);studentTb2.setAge(28);
redisTemplate.opsForSet();setOperations.add(studentSet1,studentTb1,studentTb2);
setOperations.members(studentSet1);
System.out.println(studentSet1
StudentTb();studentTb1.setId(1000);studentTb1.setName(xiaoming);studentTb1.setAge(18);
StudentTb();studentTb2.setId(1001);studentTb2.setName(lisi);studentTb2.setAge(28);
redisTemplate.opsForZSet();zSetOperations.add(zset1,studentTb1,88);zSetOperations.add(zset1,studentTb2,78);
redisTemplate.delete(a2);System.out.println(result
一般在开发的时候,不会直接使用RedisTemplate操作Redis
org.springframework.beans.factory.annotation.Autowired;
org.springframework.data.redis.core.BoundSetOperations;
org.springframework.data.redis.core.HashOperations;
org.springframework.data.redis.core.RedisTemplate;
org.springframework.data.redis.core.ValueOperations;
org.springframework.stereotype.Component;
redisTemplate.delete(collection);}
redisTemplate.boundSetOps(key);IteratorT
redisUtil.getCacheObject(a1);System.out.println(a1
redisUtil.getCacheMap(stu1);System.out.println(stu1
redisUtil.getCacheList(studentList);System.out.println(studentList
redisUtil.getCacheSet(studentSet1);System.out.println(studentSet1
生成一个token当做key,用户信息当做value,并设置过期时间1小时
前端登录成功后,从返回数据中取出token,存储到Vuex和Cookie中(Vue-admin-template架子是这么做的)
有token,但是通过token从Redis中取不出数据,说明过期了,响应回前端让其重新登录
前端后端要统一使用JSON交互(即统一返回对象R)的,拦截器中如何返回R?
拦截器中返回指定异常类,然后全局异常处理类中捕获这些异常,统一返回指定的状态码即可
Vue-admin-template架子中设置了50008,50012,50014状态码
登录时生成令牌,给到前端,前端每次携带令牌,后端对请求拦截实现鉴权
message){super(message);this.code
org.springframework.web.bind.annotation.ExceptionHandler;
org.springframework.web.bind.annotation.RestControllerAdvice;/***
{ExceptionHandler(NoLoginException.class)public
ex.getMessage());NoLoginException
R.fail(noLoginException.getCode(),noLoginException.getMessage());}
redisUtil;PostMapping(/login)public
value是对象,已经配置value使用jackson2Json将对象转成JSON字符串redisUtil.setCacheObject(user:token,user,1,
TimeUnit.MINUTES);HashMapString,
本例从Authorization这个请求头中获取token值//
注意,需要将前端的请求头改变为AuthorizationOverridepublic
request.getHeader(Authorization);if
NoLoginException(50008,无效令牌,重新登录);}SysUser
NoLoginException(50014,身份信息失效,重新登录);}return
com.qf.interceptor.LoginInterceptor;
org.springframework.beans.factory.annotation.Autowired;
org.springframework.context.annotation.Bean;
org.springframework.context.annotation.Configuration;
org.springframework.context.annotation.Lazy;
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
org.springframework.data.redis.core.RedisTemplate;
org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
org.springframework.data.redis.serializer.StringRedisSerializer;
org.springframework.web.servlet.config.annotation.InterceptorRegistry;
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/***
这个注解,让springboot框架知道,以下的这个类是提供配置
loginInterceptor;Overridepublic
addInterceptors(InterceptorRegistry
{registry.addInterceptor(loginInterceptor).addPathPatterns(/**).excludePathPatterns(/sys/user/login);//
{request.getSession().invalidate();String
request.getHeader(Authorization);//
销毁redis中的tokenredisUtil.deleteObject(user:token);return
{redisUtil.deleteObject(user:token);return
后端接收请求后,调用工具类(短信工具类),生成验证码,存Redis一份(设置过期时间5分钟),短信发一份
执行查询操作发现缓存中没有数据查询数据库中老的数据将数据放入缓存
而且只要缓存没有过期只要没有其他的修改数据库的操作缓存和数据库会长时间不一致
c用户请求数据发现缓存中数据不存在查询数据库新数据将数据写入缓存这时缓存中是最新数据
实现缓存和数据库的数据短时间不一致只有b出现一次不一致的情况影响小
问题查询一个不存在的数据由于缓存中没有该数据导致每次请求都会去数据库查询数据库压力增大。
布隆过滤器在缓存之前先通过布隆过滤器判断数据是否存在如果不存在则直接返回避免访问数据库。
是否存在若存在的话则进行下一步处理。
若不存在的话直接返回这样就不会触发后续的数据库查询。
需要注意的是缓存穿透不能完全解决我们只能将其控制在一个可以容忍的范围内。
缓存空值或默认值对于不存在的数据也在缓存中保存一个空值或默认值并设置较短的过期时间减少数据库查询压力。
问题当某个热点数据在缓存中过期或者不存在时大量请求会直接访问数据库导致数据库压力骤增。
使用互斥锁如分布式锁来控制只有一个请求去数据库加载数据其他请求等待。
逻辑过期不直接设置过期时间而是用程序逻辑判断数据是否“过期”减少因过期导致缓存击穿的情况。
预先加载对于热点数据在其过期前主动进行加载避免过期时刻的并发访问。
穿过缓存层到达数据库此时就会对数据库造成很大压力数据库压力过大也会崩溃
问题当大量缓存数据同时过期或被删除时大量请求会直接访问数据库导致数据库压力骤增。
添加随机过期时间在设置缓存过期时间时添加一定的随机时间避免大量数据同时过期。
使用分布式锁在查询数据库时使用分布式锁来避免并发查询导致的数据库压力增大。
延迟双删策略在更新数据时先删除缓存中的数据然后更新数据库。
在更新数据库成功后再次删除缓存中的数据确保数据一致性。
监控和告警对Redis缓存系统进行监控和告警及时发现和解决数据一致性问题。
问题某个热点数据被大量请求访问导致该数据所在的Redis节点压力过大甚至可能引发宕机。
热点数据分散将热点数据分散到多个Redis节点中避免单一节点压力过大。
使用多级缓存除了Redis缓存外还可以引入其他缓存层如本地缓存、CDN等将热点数据缓存到离用户更近的地方减少Redis的访问压力。
热点数据预处理对于热点数据可以提前进行预处理和计算减少实时计算的压力。
监控和告警对热点数据的访问进行监控和告警及时发现并解决潜在问题。
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback