Currently I'm using Redis for following purpose:
- Cache web pages.
- Cache SQL query resulting in single result.
For example:
@Cacheable(value = "memberCache", key = "#username.concat('')")
public MemberModel findByUsername(String username) {
return memMapper.findByUsername(username);
}
But the problem is how to cache SQL query resulting in multiple results.
e.g.:
public List<MemberModel> findWhichAgeBiggerThan(int age) {
return memMapper.ageBiggerThan(age);
}
Now the result turns out to become a List
. Of course I can cache this result too, but if a new user just registers to system, the result of this query might be changed. If this happens, cache will be stale. How to solve problems like this?
Thank you pretty pretty much!
Instead of using List you can use Redis Hash :
Your Method will be like this:
This whole map can be saved against a super key say "members" and individual data can be saved with student1, student2 keys as below
To Add any other data simply use the above command with that paricular data like
here members will add/update the existing "nembers" key with the new data.