Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Configuring a cache with pinned objects only #264

Closed
marceloverdijk opened this issue Sep 11, 2018 · 6 comments
Closed

[Question] Configuring a cache with pinned objects only #264

marceloverdijk opened this issue Sep 11, 2018 · 6 comments

Comments

@marceloverdijk
Copy link
Contributor

marceloverdijk commented Sep 11, 2018

What is the best way to create a cache and pre-load it with pinned objects. No object should be evicted and no new objects will be added. More of a static cache offering useful cache stats.

Would a cache with the following characteristics do the job:

  • set maximum size to 0 configure no maximum size
  • set maximum weight to an arbitrary value
  • configure no expiration
  • configure a custom Weigher returning simply 0. Would be an idea to add a PinnedWeigher to the lib?
  • load the object from e.g. the database and put them one by one in the cache.

I think this should do the trick, or are there any caveats I should think of?

@ben-manes
Copy link
Owner

If no objects are to be evicted, maybe you can use an unbounded cache? That is optimized for as a lightweight wrapper on top of ConcurrentHashMap. Then if your code never explicitly removes, the cache won't either. This would merely be using a setting without any bounding types.

Alternatively, if you are fetching it up front you might prefer to use Guava's Suppliers.memoize(...) and an ImmutableMap or equivalent. You won't get built-in stats, but it might be more natural if the data isn't really distinct key/value pairs but a glob.

@marceloverdijk
Copy link
Contributor Author

thx @ben-manes I would like to use a Caffeine cache as I could change the configuration easily if I later find out I could not cache everything and need to implement an eviction strategy after all.

What is a unbounded cache and how could I create that? Does it differ?

@ben-manes
Copy link
Owner

An unbounded cache is merely Caffeine.newBuilder().build(). You can turn on various features, like stats or refreshAfterWrite, without an explicit bound. This way if the interfaces are more natural or you want to quickly adjust the configuration, its pretty flexible to your needs. It should only fail when incompatible features (like writer + async) are requested, but otherwise give you what you ask for in an optimized variant.

@marceloverdijk
Copy link
Contributor Author

ah, I'm using unbound caches than already :-)
So I think above assumptions are correct then.

@ben-manes
Copy link
Owner

yeah, just don't use a custom weigher as no pinning is needed. Pining is if you use eviction but some items have to be excluded temporarily because there is a client-side lock. Kind of an edge case for some infrastructure projects (like databases) and natively supported by coincidence, so explained in the FAQ. For yours, just don't enable the features and you should be set.

@marceloverdijk
Copy link
Contributor Author

oke thx, I will remove the custom Weigher and maximum weight setting. Thx again for quick reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants