Reputation: 781
I am trying to delete a redis key but for some reason it is not delete but also not throwing an exception. Here is my code to delete:
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));
}
}
Upvotes: 21
Views: 58023
Reputation: 1
public void deleteCustomer(BigInteger id) {
redisTemplate.setDefaultSerializer(new StringRedisSerializer());
redisTemplate.delete(String.valueOf(id));
}
Upvotes: 0
Reputation: 390
Before spring boot 2, if you didn't specify a serializer when building resttemplate, on redis you see keys like this:
"xac\xed\x00\x05t\x008mx.company.support.catalog.dao.keys"
But when trying to delete it with redisTemplate.delete(key)
the key is not erased
One easy way is to get the key in byte and proceed to delete it.
Example in your class:
@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;
}
Upvotes: 3
Reputation: 1848
An alternative technique for deleting using ValueOperations
is to set an empty value that will expire almost immediately. Redis will handle the eviction itself.
For example, you can set a value like this:
valueOperations.set("key", "value");
When you want to delete you can then do something like this:
valueOperations.set("key", "", 1, TimeUnit.MILLISECONDS);
It's essential that the key is the same in both operations
Upvotes: 3
Reputation: 1
Try this:
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;
}
});
}
Upvotes: 0
Reputation: 9528
ValueOperations does NOT have delete method. So the following won't work:
redisTemplate.opsForValue().delete(key);
Try
redisTemplate.delete(key);
Upvotes: 59
Reputation: 1
There is no getOperation to use :
@Override
public void deleteCustomer(BigInteger id) {
redisTemplate.opsForValue().delete(String.valueOf(id));
}
Upvotes: -2