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

哪里能做网站免费发布信息网网站

哪里能做网站,免费发布信息网网站,怎样创建个人网站,市政府网站建设文章目录 1. 引言2. 添加依赖3. 配置 Redis 连接信息4. 创建 Redis 操作服务类5. 使用 RedisTemplate 或 ReactiveRedisTemplate6. 测试 Redis 功能7. 注意事项8. 总结 Redis 是一个高性能的键值存储系统,常用于缓存、消息队列等多种场景。将 Redis 与 Spring Boo…
文章目录
  • 1. 引言
  • 2. 添加依赖
  • 3. 配置 Redis 连接信息
  • 4. 创建 Redis 操作服务类
  • 5. 使用 RedisTemplate 或 ReactiveRedisTemplate
  • 6. 测试 Redis 功能
  • 7. 注意事项
  • 8. 总结

Redis 是一个高性能的键值存储系统,常用于缓存、消息队列等多种场景。将 Redis 与 Spring Boot 结合使用可以极大提升应用的性能和响应速度。本文将详细介绍如何在 Spring Boot 应用中整合 Redis,并通过示例代码展示具体实现步骤。

1. 引言

随着互联网应用对快速读写数据的需求日益增长,传统的数据库已经难以满足某些特定场景下的性能要求。Redis 凭借其内存级的数据访问速度、丰富的数据结构支持以及简单易用的 API,成为了许多开发者的首选。接下来,我们将一步步介绍如何在 Spring Boot 中集成 Redis。

2. 添加依赖

首先,在 pom.xml 文件中添加 Spring Data Redis 和 Jedis(或 Lettuce)客户端的 Maven 依赖:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Spring Boot Starter for Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version></dependency><!-- Jedis or Lettuce client --><!-- Choose one of the following two dependencies --><!-- For Jedis --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency><!-- Or for Lettuce --><dependency><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></dependency>
</dependencies>

3. 配置 Redis 连接信息

接下来,在 application.properties 或 application.yml 文件中配置 Redis 的连接参数。这里以 .yml 文件为例:

server:port: 8082
spring:data:redis:host: localhostport: 6379password: 123456

4. 创建 Redis 操作服务类

Java实体

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @Description* @Author HaleyHu* @Date 2024/12/5 23:41*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private Long id;private String username;private int age;
}

用户接口类

import org.hbin.redis.entity.User;/*** @Description* @Author HaleyHu* @Date 2024/12/5 23:44*/
public interface UserService {User query(Long id);Boolean expired(Long id);
}

接口实现类

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.hbin.redis.entity.User;
import org.hbin.redis.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;/*** @Description* @Author HaleyHu* @Date 2024/12/5 23:46*/
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {private final RedisTemplate<String, String> redisTemplate;private final String REDIS_PREFIX_KEY = "User::";@Overridepublic User query(Long id) {Object obj = redisTemplate.opsForValue().get("User::" + id);if(obj != null) {return JSON.parseObject(obj.toString(), User.class);}// 模拟从DB查询User user = new User(id, "user" + id, 20);redisTemplate.opsForValue().set(REDIS_PREFIX_KEY + id, JSON.toJSONString(user));return user;}@Overridepublic Boolean expired(Long id) {return redisTemplate.delete(REDIS_PREFIX_KEY + id);}
}

Controller代码

import lombok.RequiredArgsConstructor;
import org.hbin.redis.entity.User;
import org.hbin.redis.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @Description* @Author HaleyHu* @Date 2024/12/5 23:46*/
@RequiredArgsConstructor
@RestController
public class UserController {private final UserService userService;@GetMapping("/query")public User query(@RequestParam Long id) {return userService.query(id);}@GetMapping("/expired")public String expired(@RequestParam Long id) {return userService.expired(id).toString();}
}

如果你想处理更复杂的数据类型(如对象),则需要使用 RedisTemplate 并配置序列化器。例如,使用 Jackson JSON 序列化器:

import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
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);Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper objectMapper = new ObjectMapper();serializer.setObjectMapper(objectMapper);template.setValueSerializer(serializer);template.setKeySerializer(new StringRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(serializer);template.afterPropertiesSet();return template;}
}

5. 使用 RedisTemplate 或 ReactiveRedisTemplate

Spring Data Redis 提供了两种主要的方式来与 Redis 进行交互:同步方式 (RedisTemplate) 和响应式编程方式 (ReactiveRedisTemplate)。根据你的需求选择合适的方式。

同步方式 (RedisTemplate)
这是最常见的方式,适用于大多数应用场景。

响应式编程方式 (ReactiveRedisTemplate)
如果你的应用采用了响应式编程模型(如 WebFlux),那么 ReactiveRedisTemplate 可能更适合你。它允许你以非阻塞的方式与 Redis 进行通信。

6. 测试 Redis 功能

最后,我们可以通过编写单元测试来验证 Redis 的基本功能是否正常工作。也可以部署运行上述程序来验证。访问路径:
http://localhost:8082/query?id=1
http://localhost:8082/expired?id=1
在这里插入图片描述
在这里插入图片描述

7. 注意事项

  • 生产环境配置:在生产环境中部署时,请确保正确配置 Redis 的安全设置(如密码保护、网络限制等),并考虑启用持久化选项以防止数据丢失。
  • 性能优化:合理调整连接池参数,避免过多的连接消耗资源;同时也可以根据业务特点选用合适的序列化器来提高性能。
  • 监控和维护:定期检查 Redis 的运行状态,及时清理过期数据,保持系统的稳定性和高效性。

8. 总结

通过上述步骤,我们成功地在 Spring Boot 应用中集成了 Redis,并实现了基本的数据缓存功能。这不仅提高了应用的性能,还为开发者提供了更多灵活的数据管理手段。

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

相关文章:

  • 企业门户网站建设内容做网站怎么做
  • 建设电子商务网站的目的滁州网站seo
  • 网站建设与制作总结市场调研报告范文模板word
  • 广告网站制作多少钱搜全网的浏览器
  • 关于网站建设的请示互联网营销的方法有哪些
  • 做网站优化如何遍文章网络营销品牌策划
  • 深圳网站开发ucreator优化关键词的方法
  • 网站建设方案书 内容管理制度外贸网站建设推广公司
  • 沈阳建站价格乐天seo视频教程
  • 红酒商城网站建设方案做竞价推广大概多少钱
  • 建设个人博客网站小程序怎么引流推广
  • 网站备案时间太长深圳谷歌优化seo
  • 搭建网站的免费程序新东方线下培训机构官网
  • 上海cms模板建站seo链接优化
  • 深圳手机网站建设公司成都网站建设方案推广
  • 哪个网站可以付费做淘宝推广招商外包公司
  • 企业建设网站需注意哪些事项宁波seo快速优化公司
  • 学校怎么创建网站凌哥seo
  • 龙泉网站建设优化设计官网
  • 网站相似度互联网营销的特点
  • 国内做网站最大的公司有哪些搜索引擎优化的具体措施
  • 济南网站建设套餐百度搜索官方网站
  • wordpress主机有什么优电商网站seo优化
  • 东营网站备案代理公司外贸网站建设
  • 自己电脑上做网站网络营销的主要方式
  • 阿克苏网站建设咨询如何制作链接推广
  • 如何做优化网站排名seo外链在线工具
  • 中山网站建设模板招商百度品牌推广
  • 商务网站建设的必备功能最知名的网站推广公司
  • 兰州网站推广建设微信营销推广方案