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

青岛公司建设网站跨境电商哪个平台比较好

青岛公司建设网站,跨境电商哪个平台比较好,网站建设代码实例,互联网直播营销大赛主题(2) 缓存更新注解一、场景需求 在高并发系统中,缓存是提升性能的关键组件。而Cache-Aside模式作为最常用的缓存策略之一,要求开发者手动管理缓存与数据库的交互。本文将结合自定义注解与Redisson客户端,实现声明式的缓存管理方案。 二、方案亮…

(2) 缓存更新注解一、场景需求
在高并发系统中,缓存是提升性能的关键组件。而Cache-Aside模式作为最常用的缓存策略之一,要求开发者手动管理缓存与数据库的交互。本文将结合自定义注解与Redisson客户端,实现声明式的缓存管理方案。


二、方案亮点
🚀 零侵入性:通过注解实现缓存逻辑
🔒 完整防护:解决缓存穿透/击穿/雪崩问题
⚡ 双删策略:保障数据库与缓存一致性
🛠️ 逻辑删除:支持数据恢复与审计需求

三、核心实现
1. 环境准备
 Maven依赖

<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.23.2</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2. 定义注解
(1) 缓存查询注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CacheableData {String key();              // 缓存键(支持SpEL)int expire() default 3600; // 过期时间(秒)int nullExpire() default 300; // 空值缓存时间
}

(2) 缓存更新注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CacheUpdateData {String key();              // 需删除的缓存键boolean logicalDelete() default false; 
}

(3) 分布式锁注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CacheLock {String lockKey();         // 锁的键(SpEL)int waitTime() default 3; // 获取锁等待时间(秒)int leaseTime() default 10; // 锁持有时间
}

3. AOP切面实现

@Aspect
@Component
public class CacheAspect {@Autowiredprivate RedissonClient redisson;private static final String NULL_PLACEHOLDER = "##NULL##";// 读操作切面@Around("@annotation(cacheable)")public Object aroundCacheable(ProceedingJoinPoint joinPoint, CacheableData cacheable) throws Throwable {// 解析SpEL生成缓存键String key = parseKey(cacheable.key(), joinPoint);// 1. 检查缓存RBucket<Object> bucket = redisson.getBucket(key);Object cachedValue = bucket.get();if (cachedValue != null) {if (NULL_PLACEHOLDER.equals(cachedValue)) return null;if (isLogicalDeleted(cachedValue)) return null;return cachedValue;}// 2. 执行原方法(查询数据库)Object dbResult = joinPoint.proceed();// 3. 回写缓存if (dbResult == null) {bucket.set(NULL_PLACEHOLDER, cacheable.nullExpire(), TimeUnit.SECONDS);} else {bucket.set(dbResult, cacheable.expire(), TimeUnit.SECONDS);}return dbResult;}// 更新操作切面(带双删)@Around("@annotation(cacheUpdate)")public Object aroundUpdate(ProceedingJoinPoint joinPoint, CacheUpdateData cacheUpdate) throws Throwable {String key = parseKey(cacheUpdate.key(), joinPoint);// 第一次删除redisson.getBucket(key).delete();// 执行数据库操作Object result = joinPoint.proceed();// 延迟双删(1秒后二次删除)redisson.getDelayedQueue(redisson.getQueue("cache:delete:queue")).offer(key, 1, TimeUnit.SECONDS);// 处理逻辑删除if (cacheUpdate.logicalDelete()) {markLogicalDelete(key);}return result;}// 其他辅助方法省略,完整代码见文末Github链接
}

4. 业务层使用示例

@Service
public class UserService {// 带防击穿的查询方法@CacheableData(key = "user:#userId", expire = 7200)@CacheLock(lockKey = "user_lock:#userId")public User getUserById(Long userId) {return userDao.findById(userId);}// 更新用户信息@CacheUpdateData(key = "user:#user.id")public void updateUser(User user) {userDao.update(user);}// 逻辑删除用户@CacheUpdateData(key = "user:#id", logicalDelete = true)public void deleteUser(Long id) {userDao.logicalDelete(id);}
}

四、方案总结

七:踩坑指南

  1. 序列化问题:推荐使用JSON序列化,避免Java序列化的版本兼容问题
  2. 锁超时设置:分布式锁的leaseTime应大于业务执行时间
  3. 内存泄漏:逻辑删除数据必须设置TTL
  4. SpEL解析:复杂表达式建议使用Spring的ExpressionParse

http://www.dtcms.com/wzjs/519142.html

相关文章:

  • wordpress内容关键字seo专业技术培训
  • 网站的现状辽源seo
  • 企业信息门户网站建设方案海外广告联盟平台推广
  • 服装 营销型网站案例seo优化包括什么
  • 焦作网站建设活动软文模板
  • 有什么网站专门做美食的吗个人网页制作成品欣赏
  • 网站开源系统bt kitty磁力猫
  • 文字logo免费设计在线生成优化关键词步骤
  • 江苏专业做网站的公司免费推广的预期效果
  • 中国最厉害的室内设计师seo做什么网站赚钱
  • 做常识的网站动态网站的制作与设计
  • 怎样自己做卖商品的网站自助建站网
  • 一流的网站建设搜索引擎外部优化有哪些渠道
  • 深圳企业网站建设制作网络公司外贸网站设计
  • 叮当快药网站谁做的网站建设推广服务
  • 在深圳做网站平台需要什么备案怎么查询最新网站
  • 万能进销存软件免费版seo网站怎么优化
  • 网店代运营收费标准佛山优化网站关键词
  • 2015年做那个网站能致富台州关键词优化平台
  • 饭店营销方案怎样快速引客南宁百度关键词优化
  • 官方网站建设银行泰安做网站公司
  • 做网站卖酒广州seo和网络推广
  • 临沂企业自助建站系统可以免费发广告的网站有哪些
  • 佛山 做网站公司有哪些网络营销成功案例介绍
  • 织梦网站模板还原的文件在哪里百度收录怎么查询
  • 织梦新闻网站模板搜索引擎排名影响因素有哪些
  • 浙江省网站建设与管理试卷公司网络推广服务
  • 自己做网站难吗成都关键词快速排名
  • 河南省教育类网站前置审批国内比百度好的搜索引擎
  • 建筑网站登陆页面东莞企业网站排名