Re: Unable to use OSCache new version

Chris Miller <[email protected]>
Newsgroups gmane.comp.java.open-symphony.os-cache
Message-ID <[email protected]>
The cache grouping functionality has superceeded the pattern flushing, 
so while your current approach should still work, you'd be much better 
off taking advantage of the grouping instead.

It sounds like you want to be able to group the translations by property 
file type (ie, all the 'state_*' would belong to a single group), which 
would make flushing the relevant group of properties files very easy.

One question I have though is why are you using the generateEntryKey() 
method? In your situation it seems you would be FAR better off if your 
cache key was simply the name of the property file, eg "state_hi". This 
is because the generateEntryKey() method of the 
ServletCacheAdministrator uses the request URI as part of the key - as 
far as I can tell that has no relevance in your situation and will cause 
a huge number of duplicate entries to be added to your cache! In fact it 
may be that you don't need to use the ServletCacheAdministrator at all - 
the GeneralCacheAdministrator sounds more suited to your needs, 
GeneralCacheAdministrator is a general-purpose cache wrapper that has no 
dependencies on the web at all. This makes it useful when you want to 
programmatically cache arbitrary objects in your business layer.


I'd suggest some (psuedo)code similar to the following:

// Retrieves a property file from the cache. Call with eg
// getPropertiesFile("state_en");

public PropFile getPropertiesFile(String filename) {
   GeneralCacheAdministrator admin = <retrieve GeneralCacheAdministrator
                                      from application scope or wherever
                                      it is stored>;
   PropFile propFile = null;
   boolean success = true;
   try {
     propFile = (PropFile) admin.getFromCache(filename);
     success = true;
     return propFile;
   }
   catch (NeedsRefreshException nre)
   {
     propFile = <load properties file from disk>;
     String group = filename.substring(0, filename.lastIndexOf('_'));
     admin.putInCache(filename, propFile, new String[] {group});
     success = true;
     return propFile;
   }
   finally
   {
     if (!success)
       admin.cancelUpdate(filename);
   }
}

Note that if the flush is not successful for any reason, it is 
*essential* to call cancelUpdate(). This is due to a new feature in 
OSCache 2.0 - any concurrent requests for a non-exisitent (or stale, if 
cache blocking is enabled) cache entry will queue up waiting for the 
'winning' request to finish populating the cache with the new data. This 
prevents the same cache entry from being built multiple times by 
different threads.


Flushing the cache is easy:

   GeneralCacheAdministrator admin = <retrieve GeneralCacheAdministrator
                                      from application scope>;
   // To flush a single properties file:
   admin.flushEntry("state_en");

   // To flush a group of properties files:
   admin.flushGroup("state");

   // To flush everything:
   admin.flushAll();


I hope this helps clarify the relevant changes in the API for you.

Cheers,
Chris


Debashish Chakrabarty wrote:

> Hi all,
>  
> I have used OSCache in the past but it seems the lates version had made 
> lot of changes to the way it is used now. I have the following purpose 
> to use it:
>  
> I will have property files containing tranlsated data with names like 
> state_hi, state_en, state_tm will containg the translations (english 
> key-lnaguage value pairs) for various Indian state names in Hindi, 
> English and Tamil respectively. In other words the suffix after the 
> unsersocre denotes the language. Once I put these Properties in 
> OSCache (one by one) I  may have to clear the cache:
> (1). for a single property file (say state_hi only)
> (2). For all properties of a group (say all properties begining with 
> state, i.e.for all languages)
> (3).clear all the cache (all keys)
>  
> I remember earlier we used to put something in cache like this:
>  
> ServletCacheAdministrator cacheAdmin = 
> ServletCacheAdministrator.getInstance(getServletContext());
> ServletCache sCache = (ServletCache) cacheadmin.getCache(request, 
> iCacheScope);
> String strCacheKey = cacheAdmin.generateEntryKey(strCacheKey, 
> objRequest, iCacheScope);
> sCache.putInCache(strCacheKey, objData);
>  
> To clear cache, I had used:
>  
> sCache.flushPattern(strCacheKey);
>  
> I see that flushPattern() has been deprecated and in Cache class new 
> methods suc as *putInCache* 
> <file:///C:/Documents%20and%20Settings/debashish/Desktop/oscache-2.0.1-binary/docs/api/com/opensymphony/oscache/base/Cache.html#putInCache(java.lang.String, 
> java.lang.Object, java.lang.String[])>(java.lang.String key, 
> java.lang.Object content, java.lang.String[] groups) have been provided. 
> Unfortunately there is no tutorial on using OSCache API (only guidelines 
> exist to use it as Taglib). I use OSCache using Servlets so I am unable 
> to use it now.
>  
> For the three purposes specified in (1),(2) and (3) above how can I use 
> the API classes to put/clear objects in/from cache? Please help.
>  
> Thanks for your time. If possible please point me to any online tutorila 
> that covers this.
> 
> *Debashish Chakrabarty*
> http://www.jroller.com/page/debashish



-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.