我正在尝试删除一个 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));
}
}
最佳答案
ValueOperations 没有delete 方法。所以以下将不起作用:
redisTemplate.opsForValue().delete(key);
尝试
redisTemplate.delete(key);
关于Spring Redis Delete不删除key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21058933/