我想将List缓存到redis中。以下是相同的服务方法:-
@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);
}
但我收到此异常:java.lang.IllegalArgumentException:DefaultSerializer需要可序列化的有效负载,但收到了[org.springframework.http.ResponseEntity]类型的对象
将 implements Serialized 添加到 NoteResponse 的末尾
公共类 NoteResponse 实现 Serialized
我在这里有同样的问题 --> https://github.com/hendisantika/spring-data-redis-sample
还没找到解决办法。