|
|
|
[
Permlink
| « Hide
]
Alex Atran - [11/Jul/05 07:50 AM ]
I think another method to get All values by group would be useful as well.
I worked on 2.4 version...
In Cache class I implemented a method permits to retrieve all objects that belong to a specified group and it was been necessary add attribute "key" to NeedsRefreshException exception (more details below). /** * Retrieve all objects that belong to the supplied group. * * @param group The group to retrieve. * @param refreshPeriod How long before each object belongs to group needs refresh. * To allow the object to stay in the cache indefinitely, supply * a value of {@link CacheEntry#INDEFINITE_EXPIRY}. * @param cronExpiry A cron expression that specifies fixed date(s) * and/or time(s) that each entry belongs to group should * expire on. * * @return A list of objects from cache that belong to group. * * @throws NeedsRefreshException Thrown when one of the objects is stale. * When this exception occurs, the CacheEntry corresponding to the supplied * key will be locked and other threads requesting this entry will potentially * be blocked until the caller repopulates the cache. If the caller choses not * to repopulate the cache, they <em>must</em> instead call {@link #cancelUpdate(String)}. * * @note [CACHE-149] */ public List getGroupFromCache(String group, int refreshPeriod, String cronExpiry) throws NeedsRefreshException { // get group entries Set groupEntries = cacheMap.getGroup(group); if (groupEntries == null){ return null; } List entryList = new ArrayList(groupEntries.size()); Iterator itr = groupEntries.iterator(); String key; while (itr.hasNext()) { key = (String) itr.next(); entryList.add( getFromCache(key, refreshPeriod, cronExpiry) ); } return entryList; } Here is an example with failover. Tipically we have a method for retrieve an object cached: public Object getFromCache(String key){ Object obj; try { // Get from the cache obj = admin.getFromCache(key); } catch (NeedsRefreshException nre) { try { // Get the value (probably by calling an EJB) obj = new Object(); // Store in the cache admin.putInCache(key, obj); updated = true; } finally { if (!updated) { // It is essential that cancelUpdate is called if the // cached content could not be rebuilt admin.cancelUpdate(myKey); } } } } And here is the method to retrieve objects belong to a specified group: public List getGroupFromCache(String group) { boolean isOk = false; List objList = null; String oldKey = null; while (!isOk){ try { objList = cacheAdmin.getGroupFromCache(group); isOk = true; } catch (NeedsRefreshException ex) { final String key = ex.getKey(); if (key.equals(oldKey)){ throw ex; // probably an application exception } oldKey = key; getFromCache(, true); } } return objList; } } The "key" from NeedsRefreshException helps to figure out which object key needs refresh. To avoid loops whenever NeedsRefreshException, i retrieve object only one time. I have attached the modified classes to this comment. Pasquale Marcoccia | ||||||||||||||||||||||||||||||||||||||||||||||||||||