Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Redis Delete does not delete key

Tags:

spring

redis

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));
    }
}
like image 523
Thys Andries Michels Avatar asked Jan 11 '14 05:01

Thys Andries Michels


People also ask

Does Redis delete keys?

in redis you don't remove keys but keys remove themselves.

How do I clear a key in Redis?

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.

How do I delete multiple keys in Redis?

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.

What is Spring Data Redis?

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.


1 Answers

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

redisTemplate.opsForValue().delete(key);

Try

redisTemplate.delete(key);
like image 192
Mr. 14 Avatar answered Sep 21 '22 01:09

Mr. 14