Google Guava Cache的缓存值类需要实现Serializable吗?

huangapple go评论55阅读模式
英文:

Does the class (of the cached-value) needs to implement Serializable in Google Guava Cache?

问题

我正在使用Google Guava Cache来存储类型为LevelOneDTO的类。现在,这个类继承自LevelTwoDTO,而LevelTwoDTO又继承自另外两个级别。

当我使用类型为Cache<String, LevelOneDTO>的Google Guava Cache实例,其中String表示LevelOneDTO的UUID并使用它时,我发现在读取LevelOneDTO的某些字段的值时存在一些问题。这导致了一些问题,当检索其中一个值时,LevelOneDTO对象中的某些字段丢失了。

考虑到Guava Cache可能在存储和读取时对对象进行序列化/反序列化,我向类层次结构的所有4个级别添加了"implements Serializable",而且好像奇迹般地解决了问题!

但是我找不到任何参考文档,说明要存储的值的类型/类需要是可序列化的。有人能解释一下吗?

我相信我已经找到了解决方案,通过添加"implements Serializable"(因为我使用的是Java)解决了问题,但我不知道为什么它解决了问题,也找不到与此相关的文档。有人能解释一下吗?

英文:

I am using Google Guava Cache to store class of type let's say LevelOneDTO. Now this class is extending from LevelTwoDTO, which in turn extends from 2 more levels.

When I use a Google Guava Cache instance of type Cache<String, LevelOneDTO>, where String represents the UUID of LevelOneDTO and use it, I find that there are some issues reading the values of some of the fields in the LevelOneDTO. This has caused some issues wherein, when retrieved the one of the values, some of the fields in the LevelOneDTO objects are missing.

Thinking that the Guava Cache may be serializing/de-serializing the object while storing and reading from it, I added "implements Serializable" to all 4 levels of class hierarchy, and voila! It seems to work as well!

But I could not find any reference documentation where it says that the type/class of value to be stored needs to be serializable? Can someone shed some light on it?

I believe I have found the solution wherein adding "implements Serializable" (as I am using Java) solves the problem but I don't know WHY it is solving the problem, or couldn't find any documentation related to the need of that. Can someone throw some light on it?

答案1

得分: 2

Guava缓存不进行任何序列化或其他操作。它没有记录这一点,因为它不会发生。

英文:

No. The Guava cache doesn't do any serialization or anything. It doesn't document it because it doesn't happen.

huangapple
  • 本文由 发表于 2023年6月9日 05:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定