I would like to remove (invalidate) an item after it was read from a cache.
So item should be present in a cache until a first read.
I've tried adding expireAfterAccess(0, TimeUnit.NANOSECONDS)
but then cache is not populated.
Is there any way to use guava cache in such manner or do I need to invalidate item manually after a read?
This won't work. "Access" means "read or write access" and when it expires immediately after read, then it also expires immediately after write.
You can remove the entry manually. You can use the
asMap()
view in order to do it in a single access:You could switch to Caffeine, which is sort of more advanced Guava cache and offers very flexible
expireAfter(Expiry)
.However, I don't think, that what you want is a job for a cache. As nonces should never be repeated, I can't imagine any reason to store them. Usually, you generate and use them immediately.
You may be doing it wrong and you may want to elaborate, so that a possible security problem gets avoided.