Suppose I have an authorization session store implemented as a Cache<String, Session>
, where Cache
is Guava cache or something similar and Session
is some map-like object with session data. Suppose the keys are some sessionId
strings generated during the initial authentication. The cache itself has some expiry strategy so that, for example, after 30m of inactivity a session gets evicted and a callback with two arguments, the cache key and cache value is eventually called for a full clean-up.
Suppose now I want to map different login options of essentially the same user to the same sessionId
. For example, a follow up request can be made with the same login credentials which should resolve to already present session, or an API request that is linked to the same user can be placed or a browser can ask for data using the session Id in its cookie -- all of those should resolve to the same session and session Id.
One strategy to implement this would be to maintain a Map<String, String>
from the original authorization
header (or some hash of it) to the sessionId
and then go to the original cache. This one can be a simple map for which a missing value would be identical to a situation where there is a value but not corresponding session in the session cache. I could eventually clean-up all the entries matching the same sessionId when the session cache expiry callback is called. This solution is straightforward to implement, but it is a bit ugly.
Another strategy, much more appealing to me, would be to actually use the original authorization
header (or some hash of it) along with the sessionId
as the key for the session cache pointing to exactly the same session (so multiple keys, at least two, pointing to the same session value). Adding data is trivial, but eviction is tricky as all key-value pairs for the same session (value) must be evicted at once and the callback must be called just once (as some finalization logic should be executed for the session, but only once).
Any suggestion for existing caching libraries supporting such functionality or any ideas how to implement the idea on top of e.g. Guava Cache or any similar caching solution?