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

(自用)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 敏感数据需加密存储
http://www.xdnf.cn/news/5622.html

相关文章:

  • 【A2A】根据A2A的协议标准,不同架构的2个大模型agent的交互,是否都需要实现和对接 client和server模块?
  • NuPlan v1.1 数据集校验
  • 网络原理(TCP协议性质)
  • ARM64内核内存空间布局
  • 0512 - 2094. 找出 3 位偶数
  • Spring Boot动态配置修改全攻略
  • Docker从0到1:入门指南
  • 基于卡尔曼滤波的传感器融合技术的多传感器融合技术(附战场环境模拟可视化代码及应用说明)
  • 量子加密通信:守护信息安全的未来之盾
  • 2025年第十六届蓝桥杯软件赛省赛C/C++大学A组个人解题
  • 51c大模型~合集127
  • 用C语言实现的——一个完整的AVL树的交互式系统
  • NHANES指标推荐:sNfL
  • 几何_平面方程表示_点+向量形式
  • linux内存管理
  • C盘扩容方法:如何扩展不相邻的分区?
  • 8天Python从入门到精通【itheima】-1~5
  • Baumer工业相机堡盟工业相机在使用光源时如何选择蓝光还是红光
  • 制作一款打飞机游戏43:行为编辑
  • dfs算法第二次加训之普及/提高- ,详解 上
  • GPT系列:自然语言处理的演进与多模态的探索
  • day012-软件包管理专题
  • ms-swift 代码推理数据集
  • iOS即时通信的技术要点
  • 扩展:React 项目执行 yarn eject 后的 package.json 变化详解及参数解析
  • 25、Tailwind:魔法速记术——React 19 样式新思路
  • LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
  • STC15W408AS计数器
  • 【C++11】异常
  • 配置集群-日志聚集操作