my ideal cache using guava

前端 未结 2 1220
星月不相逢
星月不相逢 2020-12-13 07:03

Off and on for the past few weeks I\'ve been trying to find my ideal cache implementation using guava\'s MapMaker. See my previous two questions here and here to follow my t

2条回答
  •  青春惊慌失措
    2020-12-13 07:43

    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

提交回复
热议问题