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));
    }
}
Thys Andries Michels
  • 761
  • 4
  • 11
  • 23
  • 1
    Use the monitor command from redis-cli to see which commands are sent to the Redis server. http://redis.io/commands/monitor – Didier Spezia Jan 11 '14 at 09:43
  • is your redis a master instance? Your behaviour would happen if you try to delete a key on a slave instance. – user1151446 Jan 21 '14 at 08:36

6 Answers6

56

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

redisTemplate.opsForValue().delete(key);

Try

redisTemplate.delete(key);
Mr. 14
  • 9,228
  • 6
  • 37
  • 54
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

Seun Matt
  • 1,660
  • 1
  • 9
  • 14
2

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;
}
Baldiry
  • 370
  • 2
  • 7
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;
                }
        });
}
0
 public void deleteCustomer(BigInteger id) {
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.delete(String.valueOf(id));
 }
Sumit
  • 1
-2

There is no getOperation to use :

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