Spring Redis Delete does not delete key
Asked Answered
U

6

21

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));
    }
}
Undetermined answered 11/1, 2014 at 5:36 Comment(2)
Use the monitor command from redis-cli to see which commands are sent to the Redis server. redis.io/commands/monitorCoprology
is your redis a master instance? Your behaviour would happen if you try to delete a key on a slave instance.Ila
P
57

ValueOperations does NOT have delete method. So the following won't work:

redisTemplate.opsForValue().delete(key);

Try

redisTemplate.delete(key);
Prosenchyma answered 16/8, 2015 at 8:36 Comment(1)
I wonder if this was a conscious decision on the part of the API designers and their thought process behind itJockstrap
C
3

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;
}
Costrel answered 22/6, 2020 at 22:4 Comment(0)
I
2

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

Icehouse answered 4/6, 2020 at 11:0 Comment(2)
A very helpful solution if you're a fan of super awkward race conditions.Birthright
All jokes aside, this option should not be used and redisTemplate.delete should be substituted in its placeJockstrap
B
0

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;
                }
        });
}
Boehmer answered 24/2, 2016 at 10:34 Comment(0)
S
0
 public void deleteCustomer(BigInteger id) {
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.delete(String.valueOf(id));
 }
Skerry answered 12/4, 2023 at 16:47 Comment(0)
O
-2

There is no getOperation to use :

@Override
public void deleteCustomer(BigInteger id) {
    redisTemplate.opsForValue().delete(String.valueOf(id));
}
Orgeat answered 20/12, 2014 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.