{"rsdb":{"rid":"395056","subhead":"","postdate":"0","aid":"272459","fid":"57","uid":"1","topic":"1","content":"

\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u5f0f\u90fd\u9700\u8981\uff1a<\/p>

1.\u6dfb\u52a0\u4f9d\u8d56<\/p>

2.\u5199\u914d\u7f6e\u4fe1\u606f<\/p>

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
# \u8fde\u63a5\u8d85\u65f6\u65f6\u95f4 \u5355\u4f4d ms\uff08\u6beb\u79d2\uff09
spring.redis.timeout=3000<\/p>

# \u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5927\u7a7a\u95f2\u8fde\u63a5\uff0c\u9ed8\u8ba4\u503c\u4e5f\u662f8\u3002
spring.redis.pool.max-idle=8
#\u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5\uff0c\u9ed8\u8ba4\u503c\u4e5f\u662f0\u3002
spring.redis.pool.min-idle=0
# \u5982\u679c\u8d4b\u503c\u4e3a-1\uff0c\u5219\u8868\u793a\u4e0d\u9650\u5236\uff1b\u5982\u679cpool\u5df2\u7ecf\u5206\u914d\u4e86maxActive\u4e2ajedis\u5b9e\u4f8b\uff0c\u5219\u6b64\u65f6pool\u7684\u72b6\u6001\u4e3aexhausted(\u8017\u5c3d)\u3002
spring.redis.pool.max-active=8
# \u7b49\u5f85\u53ef\u7528\u8fde\u63a5\u7684\u6700\u5927\u65f6\u95f4\uff0c\u5355\u4f4d\u6beb\u79d2\uff0c\u9ed8\u8ba4\u503c\u4e3a-1\uff0c\u8868\u793a\u6c38\u4e0d\u8d85\u65f6\u3002\u5982\u679c\u8d85\u8fc7\u7b49\u5f85\u65f6\u95f4\uff0c\u5219\u76f4\u63a5\u629b\u51faJedisConnectionException
spring.redis.pool.max-wait=-1<\/p>

\u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528Autoconfiguration\u81ea\u52a8\u52a0\u8f7d\u3002<\/strong><\/p>

\u56e0\u4e3a\u4e0a\u9762\u5f15\u5165\u4e86spring-boot-start-data-redis\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528RedisAutoConfiguration\u7c7b\u52a0\u8f7dproperties\u6587\u4ef6\u7684\u914d\u7f6e\u3002<\/p>


**
 * Standard Redis configuration.
 *\/
@Configuration
protected static class RedisConfiguration {<\/p>

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory)
                    throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }<\/p>

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(
            RedisConnectionFactory redisConnectionFactory)
                    throws UnknownHostException {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}<\/p>

\u6b64\u79cd\u65b9\u5f0f\u4f1a\u9ed8\u8ba4\u52a0\u8f7dapplicaiton\u4e2d\u7684redis\u914d\u7f6e\uff0c\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u4e24\u79cdbean<\/p>

RedisTemplate<Object,Object> \u53ef\u4ee5\u5bf9Redis\u4e2dkey\u548cvalue\u90fd\u4e3aobject\u7c7b\u578b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c,\u9ed8\u8ba4\u4f1a\u5c06\u5bf9\u8c61\u4f7f\u7528JdkSerializationRedisSerializer\u8fdb\u884c\u5e8f\u5217\u5316<\/p>

StringRedisTemplate\u53ef\u4ee5\u5bf9Redis\u4e2dkey\u548cvalue\u90fd\u662fString\u7c7b\u578b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>

\u65b9\u5f0f\u4e8c\uff1a\u81ea\u5df1\u5199\u4ee3\u7801\u52a0\u8f7d\u3002<\/strong><\/p>

@Configuration
public class RedisConfig{<\/p>

  private Logger logger = LoggerFactory.getLogger(this.getClass());<\/p>

  @Value("${spring.redis.host}")
  private String host;<\/p>

  @Value("${spring.redis.port}")
  private int port;<\/p>

  @Value("${spring.redis.timeout}")
  private int timeout;<\/p>

  @Value("${spring.redis.password}")
  private String password;<\/p>

  @Value("${spring.redis.database}")
  private int database;<\/p>

  @Value("${spring.redis.pool.max-idle}")
  private int maxIdle;<\/p>

  @Value("${spring.redis.pool.min-idle}")
  private int minIdle;<\/p>

  \/**
  * redis\u6a21\u677f\uff0c\u5b58\u50a8\u5173\u952e\u5b57\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u662fJdk\u5e8f\u5217\u5316
  * @Description:
  * @param factory
  * @return
  *\/
  @Bean
  public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
      RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
      redisTemplate.setConnectionFactory(factory);
      RedisSerializer<String> redisSerializer = new StringRedisSeria","orderid":"0","title":"SpringBoot\u96c6\u6210Redis\u7684\u4e09\u79cd\u65b9\u5f0f(\u4e00)","smalltitle":"","mid":"0","fname":"\u6570\u636e\u5e93\u7f16\u7a0b","special_id":"0","bak_id":"0","info":"0","hits":"442","pages":"4","comments":"0","posttime":"2023-07-26 08:18:14","list":"1690330694","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"SpringBoot<\/A> \u96c6\u6210<\/A> Redis<\/A> \u65b9\u5f0f<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"119.59.235.169","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"SpringBoot\u96c6\u6210Redis\u7684\u4e09\u79cd\u65b9\u5f0f","lastview":"1715493146","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}