[keycloak-dev] How the cache works

Bill Burke bburke at redhat.com
Thu Jul 3 15:12:53 EDT 2014


Wanted to write up how the cache works.  Its not very efficient on how 
it decides to do invalidations, but this was the easiest approach.

* There are RealmAdapter, UserAdapter, etc. in the cache module just 
like any other adapter
* These adapter objects are created once per session and reused with 
each lookup
* Any non-read method invoked will cause a persistence adapter to be 
found for that model.  The cache adapter object will then, from then on, 
delegate to that persistence adapter.
* Invalidations happen at the end of a transaction
* Any create, update, or delete method invoked will cause a invalidation 
event to be registered.
* If you call RealmModel.removeOAuthClient(), RealmModel.addRole(), 
etc...this also causes the realm to be registered for invalidation. 
Same thing with applications etc.  This is true for everything but user 
sessions and users.
* Anything registered for invalidation will cause the cache provider to 
return a persistent model for any top level lookups.
* persistent adapter methods should never try and typecast a Model 
reference to an adapter class.  This is problematic for JPA as you need 
to set up entity relationships.  To get around this, 
EntityManager.getReference() is used.  Doesn't seem to be as much a 
problem in the mongo one.

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com


More information about the keycloak-dev mailing list