Skip to content
Advertisement

Spring data redis – How to use hashOperation’s scan method to get keys or values based on pattern?

I have never worked on Redis and Spring boot. I want to use Redis as a cache.

I have been using hashOperations for get/set operations. I have configured the RedisTemplate to stop weird hashes from getting prepended to a key.

I have a class called Post which I am caching. A user can create multiple posts. The key gets generated like this : userId::postId and the post data gets cached.

How to use scan method of RedisTemplate to get all posts of a particular user? I tried ScanOptions and the pattern * but I am certainly doing something wrong as I am not getting any data. There aren’t many links or youtube videos on scan and ScanOptions so I am finding it difficult to implement.

This is what I have wrote for getting all posts of a user:

JavaScript

This is the savePost method

JavaScript

Thanks for helping!

Advertisement

Answer

hashOperations.scan scans within a hash. But you want to scan within whole database (Redis).

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement