Problems and solutions encountered by Spring using redis

One, configuration file

<!-- 加载Properties文件 -->
  <bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath*:config.properties</value>
      </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
  </bean>

  <!-- 配置JedisPoolConfig实例 -->
  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!--最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制-->
    <property name="maxIdle" value="300" />
    <!--连接池的最大数据库连接数。设为0表示无限制-->
    <property name="maxTotal" value="600" />
    <!--最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制-->
    <property name="maxWaitMillis" value="1000" />
    <!--在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的-->
    <property name="testOnBorrow" value="true" />
    <!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3-->
    <property name="numTestsPerEvictionRun" value="3"/>
    <!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)-->
    <property name="minEvictableIdleTimeMillis" value="300000"/>
    <!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1-->
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    <!--在空闲时检查有效性, 默认false -->
    <property name="testWhileIdle" value="true" />
  </bean>

  <!-- 配置JedisConnectionFactory,类似于数据库的连接池 -->
  <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="${redis.host}"></property>
    <property name="port" value="${redis.port}"></property>
    <property name="password" value="${redis.password}"></property>
    <property name="database" value="${redis.dbIndex}"></property>
    <property name="poolConfig" ref="jedisPoolConfig"></property>
    <property name="timeout" value="100000"></property>
  </bean>

  <!-- 配置RedisTemplate -->
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory"></property>
    <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!! -->
    <property name="keySerializer" >
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    </property>
    <property name="valueSerializer" >
      <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
    </property>
    <property name="hashKeySerializer">
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    </property>
    <property name="hashValueSerializer">
      <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
    </property>
    <!--开启事务 -->
    <property name="enableTransactionSupport" value="true"></property>
  </bean>

Note: You can see that in my redis configuration, StringRedisSerializer serialization is used for the hash key, and GenericJackson2JsonRedisSerializer serialization is used for the value.

Second, pit one

// 源代码
HashOperations ho = redisTemplate.opsForHash();
Boolean flag = ho.hasKey(“key”, "key1");
if (flag) {
  // 处理逻辑
}

The above code, when the value in redis does not exist, according to the official documentation, it should return false. But when I use it, the hasKey method sometimes returns null and sometimes false, resulting in a null pointer exception. After doing it for a long time, I didn't figure out why it returned false. In the end, there was no way for me to compromise (the god can reply to me if he knows).

// 妥协后代码
HashOperations ho = redisTemplate.opsForHash();
Boolean flag = ho.hasKey(“key”, "key1");
if (flag != null && flag) {
  // 处理逻辑
}

Three, pit two

// 将Map放到redis的hash中
  public void putRedisHash1(){
    HashOperations ho = redisTemplate.opsForHash();
    Map<String, Boolean> tempMap = new HashMap<String, Boolean>(3){
   
   {
      put("isRegistered",false);
      put("isWeChat",false);
      put("isAliPay",false);
    }};
    ho.put("key", "key1", tempMap);
  }

  // 将Map放到redis的hash中
  public void putRedisHash2(){
    HashOperations ho = redisTemplate.opsForHash();
    Map<String, Boolean> tempMap = new HashMap<String, Boolean>(3);
    tempMap.put("isRegistered",false);
    tempMap.put("isWeChat",false);
    tempMap.put("isAliPay",false);
    ho.put("key", "key1", tempMap);
  }

The functions of these two methods are the same, but the first method is to put the value into it at the same time as the Map is initialized. The second method is to put the value into it after the Map is initialized. The execution result is the same, but in redis The storage format is completely different. Since the value of Hash is serialized using GenericJackson2JsonRedisSerializer, it will store it for the convenience of deserialization

The @class "" field, due to the different timing of the Map initialization, causes the same content to be inconsistent in the @class content in redis. For ease of use, the second method is recommended, which is to put the value into it after the Map is initialized.


Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/113958814
Recommended