Hi,I want to implement something like 2nd level cache in front of Memcached server for caching security tokens in my API. I choose Cache<String, String> with small on-read expiration interval and appropriate CacheLoader<String, String> for loading values from Memcached server. When key expires in guava's Cache, CacheLoader tries to load it from Memcached, but when key expires there, whole cache must return null. This is regular use, token just expires. But CacheLoader.load(key) method cannot return null.I can wrap key with something but I cannot cache nulls because expired tokens are pretty useless in 2nd level cache.How can I use Cache with CacheLoader for expiring data like security tokens?Thank you.--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
Use something like Cache<String, Optional<String>> instead of using null to indicate "there actually isn't any value anywhere, it's absent from Memcached."
Thanks for valuable answers.I try to explain my motivation:1) user calls login to API and obtain security token, token is written to Memcached server with user ID as value (expiration 1 hour) and Cache<String, User> instance (read-expiration 1 minute)2) user calls API within 1 minute interval, passed token is in Cache instance and User object is known, ok3) user calls API after 2 or more minutes, token is not found in Cache instance but CacheLoader loads user ID from Memcached and User object must be loaded from database, ok4) from this point for 1 minute is User object held in Cache instance5) user calls API after 1 hour, token is not found in Cache neither Memcached, error is returned to user and user has to relogin
6) from this point expired security token is not in use anymore
Sure, I can use your advice with Optional wrapper, it has only small footprint
-- after returning error expired token will be placed for 1 minute in Cache instance.
But would not, if CacheLoader supports something like "not found" in load(key) method.
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
I don't think InvalidCacheLoadException has anything to do with anything here. Think of that class like Error. Just don't go there.
> You could use the Null Object Pattern instead, but it's actually a sort of
> antipattern.
Care to explain why the Null Object Pattern is sort of an antipattern?
On Wednesday, September 12, 2012 3:57:59 PM UTC+2, Kevin Bourrillion wrote:I don't think InvalidCacheLoadException has anything to do with anything here. Think of that class like Error. Just don't go there.Currently not - that's what I described as hack. What I've meant was an idea for you:Create a `NoSuchElementCacheLoadException` and let the users throw it in cases like this (i.e., there's no value to be loaded and this information should not be stored).
> You could use the Null Object Pattern instead, but it's actually a sort of
> antipattern.Care to explain why the Null Object Pattern is sort of an antipattern?Every piece of program must know about it. It's easy to forget to handle it and compute some nonsense with it. Sometimes the Null Object works nicely - when you ask it to do something it can throw an exception or do nothing, whatever is right. Sometimes you just add it to a collection as if it was a proper object and it has no change to protest.