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));
}
}
in redis you don't remove keys but keys remove themselves.
Redis Commands There are two major commands to delete the keys present in Redis: FLUSHDB and FLUSHALL. We can use the Redis CLI to execute these commands. The FLUSHDB command deletes the keys in a database. And the FLUSHALL command deletes all keys in all databases.
We can use keys command like below to delete all the keys which match the given patters “ user*" from the Redis. Note :- Not recommended on production Redis instance. I have written Lua script to delete multiple keys by pattern . Script uses the scan command to delete the Redis cache key in incremental iteration.
Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.
ValueOperations does NOT have delete method. So the following won't work:
redisTemplate.opsForValue().delete(key);
Try
redisTemplate.delete(key);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With