2

I want to cache the content in a DB table like below, on the Java heap of an JavaEE application server.

| group   | key     | value                                  |
| ------- | ------- | -------------------------------------- |
| CHAR(3) | CHAR(5) | VARCHAR(256)                           |
| ------- | ------- | -------------------------------------- |
| 001     | 0       | male                                   |
| 001     | 1       | female                                 |
| 001     | 9       | other                                  |
| 002     | 004     | Afghanistan                            |
| 002     | 008     | Albania                                |
| 002     | 010     | Antarctica                             |
| 002     | 012     | Algeria                                |
| ...     | ...     | ...                                    |
| 003     | LAX     | Los Angeles International Airport      |
| 003     | SIN     | Singapore Changi International Airport |
| ...     | ...     | ...                                    |

I think the JCache is most general way for those purposes, but how can I save the order of these records in the cache, like LinkedHashMap?

(The configuration for specific JCache implementation is acceptable.)

EDIT

This is a typical One True Lookup Table (OTLT) in the legacy enterprise application now I am working on. The primary sort key is group, and secondary sort key is key.

OTLT has some disadvantages in performance, so I want to use use cache mechanism as a countermeasure to this disadvantages.

SATO Yusuke
  • 1,600
  • 15
  • 39
  • "order" is based on some "key". Db table in own sense , JChache in similar. I guess, some connection group+"string key" is Your JCache key??? – Jacek Cz Dec 07 '17 at 16:48
  • Example show, You have some trouble with type of "key", not string, not number ... like adding apples to peaches – Jacek Cz Dec 07 '17 at 16:49
  • Just as you said, this table layout is somehow weird. But this kind of OTLT-style tables often appear in the legacy enterprise application. I add some information in the question. – SATO Yusuke Dec 13 '17 at 15:35

1 Answers1

1

Since you need to cache what looks like a map-of-maps model I would suggest you take also into account your access pattern (ie how your application reads these data). My guess is that you would need to do two things with these data:

  1. list all values available in a single group (eg to populate a "Country" drop-down)
  2. locate a single value by group and key (to display a user's "Country" in their profile page)

Given these assumed requirements, these are fulfilled with a Cache<String, LinkedHashMap<String, String>> structure. Use the group as the cache's key and a LinkedHashMap<String, String> of all key->value mappings in that group as the cache's value. In this arrangement, requirement 1 is fulfilled with a single lookup in the Cache, while requirement 2 is fulfilled with one lookup in the Cache (to obtain the LinkedHashMap key->value mappings) and one more lookup in the LinkedHashMap. Note that instead of a LinkedHashMap as the cache's value type you can use a TreeMap, essentially any kind of map that maintains iteration order will do.

Due to requirement 1, I would avoid a group/key mangling scheme (like using group + "-" + key as you cache's key) as it would require a full iteration of the cache's entry set (with Cache.iterator()) and pattern matching each one of the keys to decide whether it belongs to the group you're looking for or not.

The JCache-standard way to create such a Cache with the above configuration would be something like this:

MutableConfiguration<String, LinkedHashMap> config = new MutableConfiguration<String, LinkedHashMap>()
         .setTypes(String.class, LinkedHashMap.class);
CachingProvider provider = Caching.getCachingProvider();
CacheManager defaultCacheManager = provider.getCacheManager();
Cache<String, LinkedHashMap> cache = defaultCacheManager
        .createCache("cache", config);

Observe that due to Java generics limitations, it is not possible to do this:

MutableConfiguration<String, LinkedHashMap<String, String>> config = new MutableConfiguration<String, LinkedHashMap<String, String>()
        .setTypes(String.class, LinkedHashMap<String, String>.class);

Also, you may consider implementing a CacheLoader to have your cache populated from the lookup table, as well as a strategy for refreshing your cache contents (either via expiry or by explicitly evicting the cache contents).