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

(自用)Java学习-5.12(Redis,B2C电商)

一、Redis 核心知识
  1. 缓存作用

    • 提升性能:内存读写速度(读 10w/s,写 8w/s)远超 MySQL(读 3w/s,写 2w/s)
    • 减少数据库压力:通过内存缓存热点数据,避免频繁 SQL 查询
    • 分类:本地缓存(单机内存) vs 分布式缓存(Redis 集群)
  2. Redis 安装配置

    # Windows安装步骤
    redis-server --service-install redis.windows.conf  # 注册服务
    config set requirepass [密码]                      # 设置密码
    
     
    • 客户端工具配置:连接地址127.0.0.1:6379,验证密码
    • 环境变量:配置 Redis 解压目录到系统 Path
  3. Spring Boot 整合 Redis
    依赖配置

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
     

    YML 配置

    spring:redis:host: 127.0.0.1port: 6379password: root
    
     

    核心操作

    // 注入RedisTemplate操作不同数据结构
    @Resource
    private RedisTemplate<String, String> redisTemplate;// Value操作示例
    ValueOperations<String, String> vo = redisTemplate.opsForValue();
    vo.set("key", "value", 10, TimeUnit.SECONDS);  // 带过期时间// Hash操作示例
    HashOperations<String, Object, Object> ho = redisTemplate.opsForHash();
    ho.put("user", "username", "admin");
    


二、B2C 电商项目架构
  1. 项目结构

    • 父工程zxstshoop:依赖版本管理(MyBatis/Druid/Fastjson)
    • 子模块:
      • shoop_commons:通用工具类(AOP 性能监控、统一 JSON 响应)
      • shoop_customer:业务模块(用户 / 商品 / 订单服务)
  2. 关键技术实现
    AOP 性能监控

    @Aspect
    @Component
    public class TimerUtilAspect {@Around("execution(* com.zxst.shoop.service.impl.*.*(..))")public Object logTime(ProceedingJoinPoint pjp) throws Throwable {long start = System.currentTimeMillis();Object result = pjp.proceed();System.out.println("耗时:" + (System.currentTimeMillis()-start) + "ms");return result;}
    }
    
     

    登录拦截器

    public class LoginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {Object uid = request.getSession().getAttribute("uid");if (uid == null) {response.sendRedirect("/web/login.html");  // 未登录跳转return false;}return true;}
    }
    
  3. 数据库与缓存整合

    • 数据源配置(Druid 连接池):
     
    @Configuration
    public class MyDataSource {@Bean@ConfigurationProperties("spring.datasource")public DataSource dataSource() { return new DruidDataSource(); }
    }
    
     
    • MyBatis 配置:
     
    mybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.zxst.shoop.entity
    


三、最佳实践

  1. 异常处理

    • 自定义异常体系:ServiceException为根基类,派生SaveInfoExceptionDeleteInfoException
    • 全局异常处理:
     
    @ExceptionHandler(ServiceException.class)
    public JsonResult handleException(Throwable e) {JsonResult result = new JsonResult(e);if (e instanceof SaveInfoException) result.setCode(40001);return result;
    }
    
  2. 性能优化

    • Redis 缓存策略:高频查询数据(如商品分类)优先缓存
    • 连接池配置:Druid 监控 SQL 执行效率,优化慢查询
  3. 安全规范

    • Session 管理:通过拦截器验证用户登录状态
    • 密码存储:Redis 敏感数据需加密存储

相关文章:

  • OpenCV图像金字塔详解:原理、实现与应用
  • 谷歌Gemini生图升级:与GPT-4o的对决,谁更胜一筹?
  • LGDRL:基于大型语言模型的深度强化学习在自动驾驶决策中的应用
  • 视觉-语言-动作模型:概念、进展、应用与挑战(下)
  • 互联网大厂Java求职面试:优惠券服务架构设计与AI增强实践-1
  • 【面试真题】王者荣耀亿级排行榜,如何设计?
  • 重学安卓14/15自由窗口freeform企业实战bug-学员作业
  • 关于groom毛发attributes
  • 2094. 找出 3 位偶数
  • Docker编排工具详解:Docker Compose与Docker Swarm
  • 前端面试高频50个问题,解答
  • tinyint(3)数据类型讲解
  • HP303-IIC驱动,大气压力温度传感器笔记
  • Tomcat服务部署
  • 服务器多JAR程序运行与管理指南
  • 深度拆解!MES如何重构生产计划与排产调度全流程?
  • 第二十二天打卡
  • Spring Boot 注解详细解析:解锁高效开发的密钥
  • jwt学习
  • OJ判题系统第4期之判题机模块架构——设计思路、实现步骤、代码实现(工厂模式、代理模式的实践)
  • 中国创面修复学科发起者之一陆树良教授病逝,享年64岁
  • AI观察|从万元到百万元,DeepSeek一体机江湖混战
  • 国产水陆两栖大飞机AG600批产首架机完成总装下线
  • 新华时评:中美经贸会谈为全球经济纾压增信
  • 茅台1935今年动销达到预期,暂无赴港上市计划!茅台业绩会回应多个热点
  • 专访|日本驻华大使金杉宪治:对美、对华外交必须在保持平衡的基础上稳步推进