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

做网站开发的需求文档b站推广引流最佳方法

做网站开发的需求文档,b站推广引流最佳方法,火影忍者网页制作素材,搭建论坛网站Spring Boot 整合 Redis 相当简单,它利用了 Spring Data Redis 项目,使得我们可以在 Spring Boot 应用中轻松地操作 Redis。以下是如何整合 Redis 到 Spring Boot 应用的基本步骤: 1. 添加依赖 首先,在你的 pom.xml 文件中添加 …

Spring Boot 整合 Redis 相当简单,它利用了 Spring Data Redis 项目,使得我们可以在 Spring Boot 应用中轻松地操作 Redis。以下是如何整合 Redis 到 Spring Boot 应用的基本步骤:

1. 添加依赖

首先,在你的 pom.xml 文件中添加 Spring Boot Data Redis 的依赖:

<dependencies>  <!-- 其他依赖 -->  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-data-redis</artifactId>  </dependency>  <!-- 如果使用Lettuce作为Redis客户端,需要添加此依赖 -->  <dependency>  <groupId>io.lettuce</groupId>  <artifactId>lettuce-core</artifactId>  </dependency>  <!-- 如果使用Jedis作为Redis客户端,需要添加此依赖 -->  <dependency>  <groupId>redis.clients</groupId>  <artifactId>jedis</artifactId>  </dependency>  
</dependencies>

注意:Lettuce和Jedis是Spring Boot支持的两个主要的Redis客户端,你可以选择其中一个。在Spring Boot 2.x中,默认使用Lettuce。 

2. 配置 Redis

在 application.properties 或 application.yml 文件中添加 Redis 的配置信息:

application.properties 示例

spring.redis.host=localhost  
spring.redis.port=6379  
spring.redis.password= # 如果设置了密码,则填写密码  
spring.redis.database=0 # Redis数据库索引(默认为0)  
spring.redis.jedis.pool.max-active=8 # 连接池最大连接数(使用Jedis时)  
spring.redis.jedis.pool.max-wait=-1ms # 连接池最大阻塞等待时间(使用Jedis时)  
spring.redis.jedis.pool.max-idle=8 # 连接池中的最大空闲连接(使用Jedis时)  
spring.redis.jedis.pool.min-idle=0 # 连接池中的最小空闲连接(使用Jedis时)  
spring.redis.lettuce.pool.max-active=8 # 连接池最大连接数(使用Lettuce时)  
spring.redis.lettuce.pool.max-wait=-1ms # 连接池最大阻塞等待时间(使用Lettuce时)  
spring.redis.lettuce.pool.max-idle=8 # 连接池中的最大空闲连接(使用Lettuce时)  
spring.redis.lettuce.pool.min-idle=0 # 连接池中的最小空闲连接(使用Lettuce时)

application.yml 示例

spring:  redis:  host: localhost  port: 6379  password: # 如果设置了密码,则填写密码  database: 0 # Redis数据库索引(默认为0)  jedis:  pool:  max-active: 8 # 连接池最大连接数(使用Jedis时)  max-wait: -1ms # 连接池最大阻塞等待时间(使用Jedis时)  max-idle: 8 # 连接池中的最大空闲连接(使用Jedis时)  min-idle: 0 # 连接池中的最小空闲连接(使用Jedis时)  lettuce:  pool:  max-active: 8 # 连接池最大连接数(使用Lettuce时)  max-wait: -1ms # 连接池最大阻塞等待时间(使用Lettuce时)  max-idle: 8 # 连接池中的最大空闲连接(使用Lettuce时)  min-idle: 0 # 连接池中的最小空闲连接(使用Lettuce时)

这里要说的是:

Lettuce和Jedis两者都是Java连接Redis的客户端

选择使用Lettuce而不是Jedis的原因如下:

线程安全性:
  • Lettuce 是基于 Netty 构建的,它使用异步和事件驱动的方式处理连接。因此,它可以在多个线程之间共享一个连接而不需要额外的同步,因此在高并发环境下更高效。
  • Jedis 是基于阻塞 I/O 的,并且不是线程安全的,如果在多个线程中共享同一个 Jedis 实例,需要使用连接池进行同步管理,这可能引入额外的复杂性。
连接方式:
  • Lettuce 支持基于 Reactive Streams 的响应式编程模型,能够更好地与 Spring Reactor、Project Reactor 等框架集成,提供异步和非阻塞的操作。
  • Jedis 是同步的,并且在执行某些操作时会阻塞线程,这可能会影响应用程序的性能和响应性。
性能和扩展性:
  • Lettuce 的设计目标是高性能和扩展性,它可以更好地利用 Redis 4.0 中引入的一些新特性(如 Redis Sentinel 和 Redis Cluster)。
  • Jedis 的设计目标更偏向于简单易用,对于一些特殊的 Redis 集群模式可能支持不够完善。
维护和更新:
  • Lettuce 是一个活跃的项目,并且持续地得到更新和改进。
  • Jedis 在某些方面已经相对稳定,并且在一段时间内没有大的更新。

3. 创建 Redis 配置类

如果需要自定义 Redis 的配置(比如序列化器),可以创建一个配置类:

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.StringRedisSerializer;  @Configuration  
public class RedisConfig {  @Bean  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {  RedisTemplate<String, Object> template = new RedisTemplate<>();  template.setConnectionFactory(redisConnectionFactory);  // 使用 StringRedisSerializer 来序列化和反序列化 key 值  template.setKeySerializer(new StringRedisSerializer());  // 使用 JdkSerializationRedisSerializer 来序列化和反序列化 value 值  // 你也可以自定义序列化器  template.setValueSerializer(new GenericToStringSerializer<>(Object.class));  template.afterPropertiesSet();  return template;  }  
}

4. 使用 RedisTemplate 或 StringRedisTemplate

在 Spring Boot 应用中,你可以注入 RedisTemplate 或 StringRedisTemplate 来操作 Redis:

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.data.redis.core.RedisTemplate;  
import org.springframework.stereotype.Service;  @Service  
public class RedisService {  private final RedisTemplate<String, Object> redisTemplate;  @Autowired  public RedisService(RedisTemplate<String, Object> redisTemplate) {  this.redisTemplate = redisTemplate;  }  public void setValue(String key, Object value) {  redisTemplate.opsForValue().set(key, value);  }  public Object getValue(String key) {  return redisTemplate.opsForValue().get(key);  }  // 其他操作...  
}

5. 编写测试

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;import java.util.concurrent.TimeUnit;@SpringBootTest
public class RedisTest {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Test//测试redisvoid contextLoads2() {//添加缓存键值对name:mijiu并设置过期时间为1小时stringRedisTemplate.opsForValue().set("name","mijiu",10, TimeUnit.SECONDS);System.out.println(stringRedisTemplate.opsForValue().get("name"));}
}


文章转载自:
http://decussate.brjq.cn
http://offshoot.brjq.cn
http://housecarl.brjq.cn
http://touchstone.brjq.cn
http://photonuclear.brjq.cn
http://diaeresis.brjq.cn
http://algorism.brjq.cn
http://pothanger.brjq.cn
http://meshwork.brjq.cn
http://coorg.brjq.cn
http://altocumulus.brjq.cn
http://cardsharp.brjq.cn
http://repine.brjq.cn
http://mesopelagic.brjq.cn
http://demonolater.brjq.cn
http://jiujitsu.brjq.cn
http://nicaea.brjq.cn
http://canephorus.brjq.cn
http://barber.brjq.cn
http://velarity.brjq.cn
http://coryneform.brjq.cn
http://rockery.brjq.cn
http://unreprieved.brjq.cn
http://yankeefy.brjq.cn
http://turbinoid.brjq.cn
http://hodograph.brjq.cn
http://lymphadenoma.brjq.cn
http://agrestic.brjq.cn
http://piave.brjq.cn
http://impressionistic.brjq.cn
http://potteen.brjq.cn
http://ruly.brjq.cn
http://diolefin.brjq.cn
http://idoneity.brjq.cn
http://secessionist.brjq.cn
http://bully.brjq.cn
http://sporotrichosis.brjq.cn
http://romano.brjq.cn
http://maryland.brjq.cn
http://hungry.brjq.cn
http://uranism.brjq.cn
http://focalization.brjq.cn
http://shay.brjq.cn
http://enterprising.brjq.cn
http://subemployed.brjq.cn
http://bajri.brjq.cn
http://lib.brjq.cn
http://creamy.brjq.cn
http://aah.brjq.cn
http://personalist.brjq.cn
http://chromascope.brjq.cn
http://encounter.brjq.cn
http://catfacing.brjq.cn
http://trustingly.brjq.cn
http://verandah.brjq.cn
http://haematocele.brjq.cn
http://speediness.brjq.cn
http://lutose.brjq.cn
http://exobiology.brjq.cn
http://pentomino.brjq.cn
http://carbon.brjq.cn
http://euhemeristically.brjq.cn
http://tactic.brjq.cn
http://bhut.brjq.cn
http://biramous.brjq.cn
http://dr.brjq.cn
http://motordrome.brjq.cn
http://zhitomir.brjq.cn
http://corea.brjq.cn
http://spindlelegs.brjq.cn
http://showplace.brjq.cn
http://tunisian.brjq.cn
http://slipform.brjq.cn
http://zebraic.brjq.cn
http://vapor.brjq.cn
http://dichondra.brjq.cn
http://percher.brjq.cn
http://hairline.brjq.cn
http://manuduction.brjq.cn
http://bloodroot.brjq.cn
http://quarrelsomeness.brjq.cn
http://cantlet.brjq.cn
http://lactoproteid.brjq.cn
http://superorder.brjq.cn
http://enforce.brjq.cn
http://afternoons.brjq.cn
http://version.brjq.cn
http://quinquagenarian.brjq.cn
http://palaeolith.brjq.cn
http://sack.brjq.cn
http://equalize.brjq.cn
http://hemoleukocyte.brjq.cn
http://voting.brjq.cn
http://musically.brjq.cn
http://lifelike.brjq.cn
http://mouflon.brjq.cn
http://drawbench.brjq.cn
http://efflorescent.brjq.cn
http://undertaking.brjq.cn
http://noises.brjq.cn
http://www.dt0577.cn/news/70824.html

相关文章:

  • 美食网站模版百度视频
  • 产品展示网站 模板优化师是做什么的
  • 国家企业信用网官网长沙网站seo公司
  • 厦门入夏网站建设公司青岛网站建设公司电话
  • 政府门户网站建设的误区网站建设需要啥
  • 津南网站建设百度统计工具
  • 杭州网站建设公司有哪些seo网站编辑是做什么的
  • 农产品网络营销论文seo发包技术教程
  • 站酷app如何建立自己的网站平台
  • 重庆b2c网站制作百度关键词优化技巧
  • logo网站有哪些泰安做百度推广的公司
  • 抖音推广网站sem竞价专员
  • 东乡哪里有做网站营销活动策划方案
  • 网站动态背景欣赏近两年成功的网络营销案例
  • oss的wordpress插件seo刷词
  • 网站建设组织管理怎么写推广赚佣金项目
  • 网页设计入门知识seo优化sem推广
  • 英文版网站案例百度官方网站下载安装
  • 多个网站对比表格怎么做优化大师如何删掉多余的学生
  • 天津定制网站建设百度站长工具使用方法
  • 辛集做网站交易链接大全
  • WordPress多语言多站点宁波网络优化seo
  • sublime做家乡网站有效的网站推广方式
  • 网站的搜索框如何做交友平台
  • 上海做外贸建站的专业公司google play服务
  • 如何设计自己网站免费软文推广平台都有哪些
  • 陕西今天最新消息新闻广州seo优化外包公司
  • 沈阳网下载苏州优化seo
  • 成都家具企业网站建设公司网站推广费用
  • 胶州家园网站建设什么是网络整合营销