Spring data redis模糊key,并删除数据

1、使用spring data Redis 中的StringRedisTemplate模糊匹配key
模糊匹配的方法,源码如下:
public Set keys(K pattern) {
   final byte[] rawKey = rawKey(pattern);
   Set rawKeys = execute(new RedisCallback>() {
      public Set doInRedis(RedisConnection connection) {
         return connection.keys(rawKey);
      }
   }, true);
   return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set) rawKeys;
}
使用方法
// 用星号 * 来匹配,获得一个Set集合
Set keySet = stringRedisTemplate.keys(“user:token:”+"*");
2、要删除模糊匹配key出来的key集合,可以像下面这样写:
Set keySet = stringRedisTemplate.keys(“user:token:”+"*");
Iterator iterator = keySet.iterator();
while (iterator.hasNext()) {
    String nextKey = iterator.next();
    String nextValue = stringRedisTemplate.opsForValue().get(nextKey);
    if (null != nextValue && nextValue.equals(userIdVal)) {
        stringRedisTemplate.delete(nextKey);
    }
}  
3.1、同时StringRedisTemplate可以解决Redis的key乱码问题的,其实StringRedisTemplate是继承了RedisTemplate。
其中StringRedisTemplate类在构造方法中已经对key、value和hashkey、hashvalue做了字符编码指定“UTF-8”了。
直接上StringRedisTemplate的源码,如下:
public class StringRedisTemplate extends RedisTemplate {

   /**
    * Constructs a new StringRedisTemplate instance. {@link #setConnectionFactory(RedisConnectionFactory)}
    * and {@link #afterPropertiesSet()} still need to be called.
    */
   public StringRedisTemplate() {
      RedisSerializer stringSerializer = new StringRedisSerializer();
      setKeySerializer(stringSerializer);
      setValueSerializer(stringSerializer);
      setHashKeySerializer(stringSerializer);
      setHashValueSerializer(stringSerializer);
   }

   /**
    * Constructs a new StringRedisTemplate instance ready to be used.
    * 
    * @param connectionFactory connection factory for creating new connections
    */
   public StringRedisTemplate(RedisConnectionFactory connectionFactory) {
      this();
      setConnectionFactory(connectionFactory);
      afterPropertiesSet();
   }

   protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
      return new DefaultStringRedisConnection(connection);
   }
}
接下来看看 StringRedisSerializer
RedisSerializer stringSerializer = new StringRedisSerializer();
StringRedisSerializer已经默认指定了字符编码为UTF-8了,源码:
public class StringRedisSerializer implements RedisSerializer {

   private final Charset charset;

   public StringRedisSerializer() {
      this(Charset.forName("UTF8"));
   }

   public StringRedisSerializer(Charset charset) {
      Assert.notNull(charset, "Charset must not be null!");
      this.charset = charset;
   }

   public String deserialize(byte[] bytes) {
      return (bytes == null ? null : new String(bytes, charset));
   }

   public byte[] serialize(String string) {
      return (string == null ? null : string.getBytes(charset));
   }
}
3.2、 另外,解决RedisTemplate乱码问题,还有下面这种姿势,直接码上
@Configuration
public class RedisCharsetConfig {
    @Bean(name="redisTemplate")
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
      RedisTemplate template = new RedisTemplate<>();
      RedisSerializer redisSerializer = new StringRedisSerializer();
      template.setConnectionFactory(factory);
      //key序列化方式
      template.setKeySerializer(redisSerializer);
      //value序列化
      template.setValueSerializer(redisSerializer);
      //value hashmap序列化
      template.setHashValueSerializer(redisSerializer);
      //key haspmap序列化
      template.setHashKeySerializer(redisSerializer);
      return template;
   }
}

the end.


– 日积月累,点点滴滴,这次记录这个小小的问题,以便日后看到这个博客标题,就会想起曾经多么傻仔,独苦笑之,扪着心。

– 学海无涯,不能淡定,据说这门专业学几辈子的,据说这个职业是造航母火箭,为崛起而不断的白了头,不空悲切,继而学。


你可能感兴趣的:(Redis)