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

威海高区有没有建设局的网站2023年新闻摘抄

威海高区有没有建设局的网站,2023年新闻摘抄,中国制造网一级类目,免费发布产品网站目录 一、引入依赖 二、设置redis相关配置 三、准备测试项目框架 四、注册redis中间件 五、实现缓存逻辑 六、查询测试 今天在springboot中学习如何应用redis,实现用户信息管理的缓存中间件实现。 一、引入依赖 redis的客户端主要有Jedis 和 Lettuce两种&…

目录

一、引入依赖

二、设置redis相关配置

三、准备测试项目框架

四、注册redis中间件

五、实现缓存逻辑

六、查询测试


        今天在springboot中学习如何应用redis,实现用户信息管理的缓存中间件实现。

一、引入依赖

        redis的客户端主要有Jedis 和 Lettuce两种,Spring Boot 2.x 及以上版本默认使用 Lettuce 作为 Redis 客户端,能提供更好的异步支持,且内置连接池,配置简单,无需额外依赖。

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

二、设置redis相关配置

        在application.yml中配置如下信息。

  redis:host: localhost       # Redis 服务器地址port: 6379            # Redis 服务器端口password: ""          # Redis 密码(若有)database: 0           # 默认使用的数据库索引(0-15)timeout: 5000         # 连接超时时间(毫秒)

三、准备测试项目框架

        为了测试springboot应用redis,我们创建一个简单的关于用户管理的模块。包含下面的信息:

        sql表

CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT,username VARCHAR(50) NOT NULL UNIQUE,password_hash VARCHAR(255) NOT NULL,full_name VARCHAR(100) NOT NULL,phone_number VARCHAR(20) UNIQUE,email VARCHAR(100) NOT NULL UNIQUE,score_rank INT DEFAULT 0,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);    

        userEntity

package com.naihe.redistest.entity;import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;import java.time.LocalDateTime;@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("users")
public class UserEntity {@TableId(value = "id", type = IdType.AUTO)private Integer id;private String username;@TableField("password_hash")private String passwordHash;@TableField("full_name")private String fullName;@TableField("phone_number")private String phoneNumber;private String email;@TableField("score_rank")private Integer scoreRank;@TableField(value = "created_at", fill = FieldFill.INSERT)private LocalDateTime createdAt;@TableField(value = "updated_at", fill = FieldFill.UPDATE)private LocalDateTime updatedAt;
}

        controller

package com.naihe.redistest.controller;import com.naihe.redistest.pojo.dto.RegisterDTO;
import com.naihe.redistest.service.UserService;
import com.naihe.redistest.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;@Slf4j
@Component
@Controller
@RestController
@RequestMapping("/user")
public class UserController {@AutowiredUserService userService;/*** 用户注册接口,接收用户注册信息并处理** @param registerBean 用户注册信息的传输对象* @return Result 返回操作结果*/@PostMapping("/register")public Result register(@RequestBody RegisterDTO registerBean){userService.insertUser(registerBean);return Result.ok().data("msg", "注册成功");}/*** 查询用户分数** @param userId 用户的id* @return Result 返回操作结果*/@GetMapping("/getScore/{userId}")public Result getScore(@PathVariable String userId) {Integer score = userService.getScore(userId);return Result.ok().data("score", score);}}

        service

package com.naihe.redistest.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.naihe.redistest.entity.UserEntity;
import com.naihe.redistest.mapper.UserMapper;
import com.naihe.redistest.pojo.dto.RegisterDTO;
import com.naihe.redistest.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Autowiredprivate RedisTemplate<String, Object> redisTemplate;private static final String USER_SCORE_KEY = "user:score:";private static final long CACHE_EXPIRE_TIME = 30; // 缓存30分钟@Overridepublic void insertUser(RegisterDTO registerBean) {if (userMapper.selectOne(new QueryWrapper<UserEntity>().eq("username", registerBean.getUsername())) != null) {throw new RuntimeException(registerBean.getUsername() + "已存在");}UserEntity userEntity = new UserEntity();BeanUtils.copyProperties(registerBean,userEntity);userMapper.insert(userEntity);}
}

         添加测试数据

四、注册redis中间件

package com.naihe.redistest.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// 设置 key 的序列化方式template.setKeySerializer(new StringRedisSerializer());// 设置 value 的序列化方式(JSON 格式)template.setValueSerializer(new GenericJackson2JsonRedisSerializer());// 设置 hash key 和 value 的序列化方式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());template.afterPropertiesSet();return template;}
}

五、实现缓存逻辑

        在service服务层中应用,查询用户分数时,优先从redis中查找,若不存在,则从mysql数据库中查找后,添加到redis中。

package com.naihe.redistest.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.naihe.redistest.entity.UserEntity;
import com.naihe.redistest.mapper.UserMapper;
import com.naihe.redistest.pojo.dto.RegisterDTO;
import com.naihe.redistest.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Autowiredprivate RedisTemplate<String, Object> redisTemplate;private static final String USER_SCORE_KEY = "user:score:";private static final long CACHE_EXPIRE_TIME = 30; // 缓存30分钟@Overridepublic void insertUser(RegisterDTO registerBean) {if (userMapper.selectOne(new QueryWrapper<UserEntity>().eq("username", registerBean.getUsername())) != null) {throw new RuntimeException(registerBean.getUsername() + "已存在");}UserEntity userEntity = new UserEntity();BeanUtils.copyProperties(registerBean,userEntity);userMapper.insert(userEntity);}public Integer getScore(String userId) {String key = USER_SCORE_KEY + userId;// 先从Redis获取缓存数据Integer score = (Integer) redisTemplate.opsForValue().get(key);if (score != null) {return score;}// 缓存未命中,从数据库获取UserEntity user = userMapper.selectById(userId);if (user == null) {throw new RuntimeException("用户不存在");}score = user.getScoreRank();// 将数据存入Redis缓存redisTemplate.opsForValue().set(key, score, CACHE_EXPIRE_TIME, TimeUnit.MINUTES);return score;}
}

六、查询测试

        现在我们使用postman对接口进行测试,查询用户分数,检测redis是否成功应用。

        首次查询我们可以看到,差不多用了1秒多的时间,此次查询没有在redis中查询到信息,所以会将该用户的分数存储进redis中。

        我们再次查询该用户。

        可以看到,这次的查询速度快了许多,达到了38ms,至此,我们完成了redis在springboot的简单应用,在后续更为复杂的业务可以使用更多的redis数据结构。

 

http://www.dtcms.com/a/486727.html

相关文章:

  • WebSocket vs HTTP 对比
  • 【SQL错题本】记录一些没有思路的sql题
  • 首钢建设工资网站网站建设平台价格
  • C++ 模拟题 力扣 6. Z字形变换 题解 每日一题
  • 免费建站的专做定制网站建设
  • 网站的站点建设分为有做网站设计吗
  • 创建Linux网卡的链路聚合
  • OSI七层模型:从原理到实战
  • 深入解析Linux下的`lseek`函数:文件定位与操作的艺术
  • Linux C/C++ 学习日记(25):KCP协议:普通模式与极速模式
  • 网站结构 网站内容建设现在建个企业网站要多少钱
  • C++ I/O流的全新边界
  • MySQL————内置函数
  • 精通iptables:从基础到实战安全配置
  • OpenAI发布构建AI智能体的实践指南:实用框架、设计模式与最佳实践解析
  • 如何使用网站模板金华关键词优化平台
  • php大气企业网站东莞邦邻网站建设
  • 简述php网站开发流程网站 设计公司 温州
  • thinkphp8+layui多图上传,带删除\排序功能
  • LeetCode 合并K个升序链表
  • FFmpeg 基本API avformat_alloc_context 函数内部调用流程分析
  • ubuntu系统中ffmpeg+x264简易编译安装指南
  • FLAC to MP3 批量转换 Python
  • 开源鸿蒙6.1和8.1版本被确定为LTS建议版本,最新路标正式发布!-转自开源鸿蒙OpenHarmony社区
  • linux sdl图形编程之helloworld.
  • 开发一个网站系统报价电子商务网站建设试卷及答案
  • 瑞芯微算法环境搭建(2)------编译opencv
  • 计算机视觉(opencv)——人脸网格关键点检测
  • 自己做网站投入编程培训机构需要哪些证件
  • AXI总线的基础知识