question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RedisCache.java get(...) method will return null in some cases [DATAREDIS-777]

See original GitHub issue

Sophist Wu opened DATAREDIS-777 and commented

org.springframework.data.redis.cache.RedisCache#get(org.springframework.data.redis.cache.RedisCacheKey)

public RedisCacheElement get(final RedisCacheKey cacheKey) {

		Assert.notNull(cacheKey, "CacheKey must not be null!");

		Boolean exists = (Boolean) redisOperations.execute(new RedisCallback<Boolean>() {

			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.exists(cacheKey.getKeyBytes());
			}
		});

		if (!exists.booleanValue()) {
			return null;
		}
                // The problem here is : What if the key expires, at this moments? The below return clause , will lookup in redis server for the key again, which would return null in the end.
		return new RedisCacheElement(cacheKey, fromStoreValue(lookup(cacheKey)));
	}

This problem doesn’t exist in 2.x and 1.7.x , but we don’t want upgrade to 2.x right now


Affects: 1.8.10 (Ingalls SR10)

Issue Links:

  • DATAREDIS-673 RedisCache.get(…) returns false positive cache hit when key is deleted in-flight (“duplicates”)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
spring-projects-issuescommented, Dec 30, 2020

Sophist Wu commented

I see what you changed in this version : to check from redis server , see if the corresponding value exist at redis server, if not , null will be returned (in 1.8.10 will return RedisCacheElement(cacheKey, fromStoreValue(null)).


public RedisCacheElement get(final RedisCacheKey cacheKey) {

		Assert.notNull(cacheKey, "CacheKey must not be null!");

		Boolean exists = (Boolean) redisOperations.execute(new RedisCallback<Boolean>() {

			@Override
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.exists(cacheKey.getKeyBytes());
			}
		});

		if (!exists) {
			return null;
		}

		byte[] bytes = doLookup(cacheKey);

		// safeguard if key gets deleted between EXISTS and GET calls.
		if (bytes == null) {
			return null;
		}

		return new RedisCacheElement(cacheKey, fromStoreValue(deserialize(bytes)));
	}

Okay, many thanks, the logic seems correct, I will try if that works!

0reactions
spring-projects-issuescommented, Dec 30, 2020

Sophist Wu commented

Thank you, Mark, It works now with 1.8.11 ! We can close this jira now

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Can Make Redis server return NULL or False instead of ...
So what We do are when a client request API it first go to Redis to get data if there is no data...
Read more >
Spring Data Redis
However, values can be null as long as the underlying serializer accepts them. Read the Javadoc of each serializer for more information. For ......
Read more >
Spring Boot Redis: Ultimate Guide to Redis Cache with Spring ...
Here we see a few things: @CachePut is an annotation which does what it says. The return of a method is getting put...
Read more >
Enhanced Mapping of Java Objects to Hashes
For Java objects mapped with SDR's @RedisHash annotation we enhance ... Then we'll use a CommandLineRunner @Bean annotated method to create ...
Read more >
Spring Caching with Redis - Medium
So Redis can be used as a caching system or as a full-fledged database. ... On top of the GET method, we use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found