(自用)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.1
        port: 6379
        password: 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 {
      @Override
      public 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/*.xml
      type-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 敏感数据需加密存储
相关推荐
明月看潮生20 分钟前
青少年编程与数学 02-019 Rust 编程基础 08课题、字面量、运算符和表达式
开发语言·青少年编程·rust·编程与数学
什码情况1 小时前
星际篮球争霸赛/MVP争夺战 - 华为OD机试真题(A卷、Java题解)
java·数据结构·算法·华为od·面试·机试
天天打码1 小时前
Rspack:字节跳动自研 Web 构建工具-基于 Rust打造高性能前端工具链
开发语言·前端·javascript·rust·开源
Petrichorzncu1 小时前
Lua再学习
开发语言·学习·lua
AA-代码批发V哥1 小时前
正则表达式: 从基础到进阶的语法指南
java·开发语言·javascript·python·正则表达式
大锤资源1 小时前
用NVivo革新企业创新:洞悉市场情绪,引领金融未来
人工智能·经验分享·学习·金融
字节高级特工1 小时前
【C++】”如虎添翼“:模板初阶
java·c语言·前端·javascript·c++·学习·算法
晴天下小雨o1 小时前
排序算法总结
java·算法·排序算法
曼岛_1 小时前
[Java实战]Spring Boot 整合 Redis(十八)
java·spring boot·redis
向哆哆1 小时前
Netty在Java网络编程中的应用:实现高性能的异步通信
java·网络·php