【问题标题】:Spring Redis Delete does not delete keySpring Redis Delete 不删除键
【发布时间】:2014-01-30 06:56:06
【问题描述】:

我正在尝试删除一个 redis 键,但由于某种原因,它没有被删除,也没有引发异常。这是我要删除的代码:

import com.example.service.CustomerService;
import com.example.model.Customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.*;

@Service
public class RedisCustomerService implements CustomerService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate; 

    private String uniqueIdKey = "customerId";

    private BigInteger uniqueId() {
        long uniqueId = this.redisTemplate.opsForValue().increment(uniqueIdKey, 1);
        return BigInteger.valueOf(uniqueId);
    }

    private String lastNameKey(BigInteger id) {
        return "customer:ln:" + id;
    }

    private String firstNameKey(BigInteger id) {
        return "customer:fn:" + id;
    }

    @Override
    public void deleteCustomer(BigInteger id) {
        redisTemplate.opsForValue().getOperations().delete(String.valueOf(id));
    }
}

【问题讨论】:

  • 使用 redis-cli 中的 monitor 命令查看哪些命令发送到 Redis 服务器。 redis.io/commands/monitor
  • 你的redis是主实例吗?如果您尝试删除从属实例上的密钥,您的行为将会发生。

标签: spring redis


【解决方案1】:

ValueOperations 没有 delete 方法。所以以下方法不起作用:

redisTemplate.opsForValue().delete(key);

试试

redisTemplate.delete(key);

【讨论】:

    【解决方案2】:

    使用ValueOperations 删除的另一种技术是设置一个几乎立即过期的空值。 Redis 将自行处理驱逐。

    例如,您可以这样设置一个值:

    valueOperations.set("key", "value");
    

    当你想删除时,你可以这样做:

    valueOperations.set("key", "", 1, TimeUnit.MILLISECONDS);
    

    两个操作中的密钥必须相同

    【讨论】:

      【解决方案3】:

      在 spring boot 2 之前,如果你在构建 resttemplate 时没有指定序列化器,在 redis 上你会看到这样的键:

      “xac\xed\x00\x05t\x008mx.company.support.catalog.dao.keys”

      但是当尝试使用redisTemplate.delete(key) 删除它时,密钥不会被删除

      一种简单的方法是获取字节中的密钥并继续删除它。

      你班上的例子:

      @Autowired
      private RedisTemplate<String, Object> redisTemplate;
      
      public boolean deleteKeyByPattern(String pattern) {
          Set<byte[]> patternResultConf = redisTemplate.getConnectionFactory().getConnection().keys(pattern.getBytes());
          if(Objects.nonNull(patternResultConf) && !patternResultConf.isEmpty()) {
              redisTemplate.getConnectionFactory().getConnection().del(patternResultConf.toArray(new byte[0][]));
          }
          return true;
      }
      

      【讨论】:

        【解决方案4】:

        试试这个:

        public void deleteCustomer(BigInteger id) {
                redisTemplate.execute(new RedisCallback<String>() {
                        @Override
                        public String doInRedis(RedisConnection redisConnection) throws DataAccessException {
                                redisConnection.del(redisTemplate.getStringSerializer().serialize(String.valueOf(id)));
                                return null;
                        }
                });
        }
        

        【讨论】:

          【解决方案5】:

          没有可使用的getOperation:

          @Override
          public void deleteCustomer(BigInteger id) {
              redisTemplate.opsForValue().delete(String.valueOf(id));
          }
          

          【讨论】:

            猜你喜欢
            • 2022-01-12
            • 2021-07-22
            • 1970-01-01
            • 2018-03-10
            • 1970-01-01
            • 2021-11-11
            • 2020-10-13
            • 2019-12-31
            • 1970-01-01
            相关资源
            最近更新 更多