-
How would I go about implementing a Composite keys with caffeine. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
A "composite key" refers to a key that is composed of multiple properties. For example a Point(int x, int y) would be composed into an object and used as a map key. In that case an object that has a valid equals and hashCode implementation, e.g. Java records, would be perfect as a cache key. If you instead are asking how to have multiple keys map to a single value and keep it consistent, then that is a harder problem. These are like database indexes to a row. Similar to a database you can use a primary key (key->value) and secondary keys (key->primary), and then a locking scheme to ensure consistency. A long time ago I wrote an article showing how this can be done using lock striping to decorate a cache. That code worked and could certainly be cleaned up / modernized, so it may be a helpful guide if that is what you need. |
Beta Was this translation helpful? Give feedback.
A "composite key" refers to a key that is composed of multiple properties. For example a Point(int x, int y) would be composed into an object and used as a map key. In that case an object that has a valid equals and hashCode implementation, e.g. Java records, would be perfect as a cache key.
If you instead are asking how to have multiple keys map to a single value and keep it consistent, then that is a harder problem. These are like database indexes to a row. Similar to a database you can use a primary key (key->value) and secondary keys (key->primary), and then a locking scheme to ensure consistency. A long time ago I wrote an article showing how this can be done using lock striping to d…