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

iis的默认网站没有自动启动电话营销外包公司

iis的默认网站没有自动启动,电话营销外包公司,沈阳优化网站关键词,东软集团Spring Boot集成Redis:从配置到实战的完整指南Redis作为高性能的内存数据库,在现代应用开发中扮演着至关重要的角色。本文将详细讲解如何在Spring Boot项目中集成Redis,并提供多种使用场景的实战代码。一、为什么需要集成Redis? R…

Spring Boot集成Redis:从配置到实战的完整指南

Redis作为高性能的内存数据库,在现代应用开发中扮演着至关重要的角色。本文将详细讲解如何在Spring
Boot项目中集成Redis,并提供多种使用场景的实战代码。

一、为什么需要集成Redis?

Redis在Spring Boot应用中的核心价值:

  • 性能加速:将热点数据存入内存,访问速度比数据库快100倍

  • 解耦应用:作为独立的缓存层,减轻数据库压力

  • 丰富数据结构:支持字符串、哈希、列表、集合等高级数据结构

  • 分布式支持:实现分布式锁、会话共享等功能

  • 消息系统:提供发布/订阅模式实现异步通信

二、环境准备

  • JDK 17+

  • Spring Boot 3.2.0+

  • Redis 7.0+

三、快速集成Redis

1. 添加依赖

在pom.xml中添加Spring Data Redis和连接池依赖:

<dependencies><!-- Spring Data Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- 连接池 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><!-- JSON序列化 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
</dependencies>

2. 配置Redis连接

在application.yml中添加配置:

spring:redis:host: 127.0.0.1port: 6379password: yourpassword # 无密码可省略database: 0 # 默认DBlettuce:pool:max-active: 20   # 最大连接数max-idle: 10     # 最大空闲连接min-idle: 5      # 最小空闲连接max-wait: 2000ms # 获取连接最大等待时间

3. 配置RedisTemplate

创建配置类解决键值序列化问题:

@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);// 使用Jackson序列化valueJackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper mapper = new ObjectMapper();mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);serializer.setObjectMapper(mapper);// 设置序列化template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(serializer);template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(serializer);template.afterPropertiesSet();return template;}
}

四、Redis基础操作实战

1. 使用RedisTemplate操作

@Service
public class RedisService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;// 字符串操作public void setString(String key, String value) {redisTemplate.opsForValue().set(key, value);}public String getString(String key) {return (String) redisTemplate.opsForValue().get(key);}// 哈希操作public void setHash(String key, String field, Object value) {redisTemplate.opsForHash().put(key, field, value);}public Object getHash(String key, String field) {return redisTemplate.opsForHash().get(key, field);}// 设置过期时间public void setWithExpire(String key, Object value, long seconds) {redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);}// 删除键public Boolean delete(String key) {return redisTemplate.delete(key);}
}

2. 测试Controller

@RestController
@RequestMapping("/redis")
public class RedisController {@Autowiredprivate RedisService redisService;@PostMapping("/set")public String setValue(@RequestParam String key, @RequestParam String value) {redisService.setString(key, value);return "设置成功: " + key + "=" + value;}@GetMapping("/get/{key}")public String getValue(@PathVariable String key) {return "获取结果: " + redisService.getString(key);}@PostMapping("/setUser")public String setUser(@RequestParam String userId) {User user = new User(userId, "用户" + userId, "user" + userId + "@example.com");redisService.setHash("user:" + userId, "profile", user);return "用户信息已缓存";}@GetMapping("/getUser/{userId}")public User getUser(@PathVariable String userId) {return (User) redisService.getHash("user:" + userId, "profile");}// 用户实体类@Data@AllArgsConstructor@NoArgsConstructorstatic class User implements Serializable {private String id;private String name;private String email;}
}

五、Spring Cache集成Redis

1. 启用缓存支持

在启动类添加注解:

@SpringBootApplication
@EnableCaching // 启用缓存功能
public class RedisApplication {public static void main(String[] args) {SpringApplication.run(RedisApplication.class, args);}
}

2. 配置缓存管理器

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {@Autowiredprivate RedisConnectionFactory connectionFactory;@Beanpublic CacheManager cacheManager() {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(30)) // 默认过期时间.disableCachingNullValues() // 不缓存null.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).transactionAware().build();}
}

3. 使用缓存注解

@Service
public class ProductService {@Cacheable(value = "products", key = "#id")public Product getProductById(String id) {// 模拟数据库查询System.out.println("查询数据库: " + id);return new Product(id, "商品" + id, 99.99);}@CachePut(value = "products", key = "#product.id")public Product updateProduct(Product product) {// 模拟数据库更新System.out.println("更新数据库: " + product.getId());return product;}@CacheEvict(value = "products", key = "#id")public void deleteProduct(String id) {System.out.println("删除数据库记录: " + id);}// 条件缓存:只缓存价格大于100的商品@Cacheable(value = "expensiveProducts", key = "#id", condition = "#result.price > 100")public Product getProductWithCondition(String id) {return new Product(id, "高级商品", 199.99);}
}
http://www.dtcms.com/wzjs/509904.html

相关文章:

  • 木鱼网站建设游戏推广文案
  • 策划品牌全案衡阳seo快速排名
  • 郑州做网站建设的公司宣传网站有哪些
  • 国外网页设计评论网站谷歌官网
  • 人力资源外包灵活用工惠州seo收费
  • 网站推广 html关键词代码解说百度关键词排名代做
  • 什么是网站分析深圳网络推广哪家比较好
  • 企业做网站系统网站搜索引擎优化的步骤
  • 怎么选择丹徒网站建设如何做好网站推广优化
  • 湖南做网站 地址磐石网络制作一个网站的费用是多少
  • 网上商城网站建设解决方案电商平台链接怎么弄
  • 常州网站建设公司信息广州市口碑seo推广
  • 汇款账号 网站建设seo点击排名器
  • wordpress实现ajax沈阳seo排名公司
  • 成都网站建设技巧seo外链论坛
  • 文化网站建设需要的功能写文的免费软件
  • 网络工程师中级网站快速排名优化
  • 广州外贸网站建设公司快速排名seo
  • wordpress固定链接设置静态链接廊坊自动seo
  • 微信网站开发新开页面企业文化案例
  • 个人做企业网站seo推广工具
  • 浏阳市商务局网站溪江农贸市场建设在线crm网站建站
  • 网站托管服务使用于那种类型的网站网站制作过程
  • app制作开发公司怎么收费重庆专业seo
  • 广州设计公司排名榜网站优化包括哪些内容
  • 怎么用word做一个网站seo做的比较好的公司
  • 动漫一级a做爰片免费网站爱站网排名
  • 运城做网站公司51网站统计
  • 委托网站建设注意什么优化大师怎么卸载
  • 企业网站内容东莞网站关键词优化公司