Reputation: 39
I want to cache the List in redis. Below is the service method for the same:-
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Cacheable(value = "notes", key = "#userId")
public ResponseEntity<?> getAllNotes(Integer userId) {
HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
//Check if the cacheKey contains this userId
if (hashOperations.hasKey(cacheKey, userIdAsKey)) {
List<NoteResponse> noteResponses = (List<NoteResponse>) hashOperations.get(cacheKey, userIdAsKey);
return ResponseEntity.ok(noteResponses);
}
List<Note> notes = noteRepository.findAllByUserId(userId);
List<NoteResponse> noteResponses = Helper.getNoteResponse(notes);
hashOperations.put(cacheKey, userIdAsKey, noteResponses);
return ResponseEntity.ok(noteResponses);
}
but i am getting this exception: java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [org.springframework.http.ResponseEntity]
Upvotes: 3
Views: 6463
Reputation: 41
Add implements Serializable to the end of the NoteResponse for example
public class NoteResponse implements Serializable
Upvotes: 3