my ideal cache using guava

送分小仙女□ 提交于 2019-11-28 17:35:15

Whether two maps is efficient depends entirely on how expensive getFromDatabase() is, and how big your objects are. It does not seem out of all reasonable boundaries to do something like this.

As for the implementation, It looks like you can probably layer your maps in a slightly different way to get the behavior you want, and still have good concurrency properties.

  1. Create your first map with weak values, and put the computing function getFromDatabase() on this map.
  2. The second map is the expiring one, also computing, but this function just gets from the first map.

Do all your access through the second map.

In other words, the expiring map acts to pin a most-recently-used subset of your objects in memory, while the weak-reference map is the real cache.

-dg

I don't understand the full picture here, but two things.

  1. Given this statement: "this implementation will evict objects even if they are strongly reachable, once their time is up. This could result in multiple objects with the same UID floating around in the environment, which I don't want." -- it sounds like you just need to use weakKeys() and NOT use either timed or size-based eviction.

  2. Or if you do want to bring an "interner" into this, I'd use a real Interners.newWeakInterner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!