OSCache - Usage

Beside the JSP tag library and the CacheFilter you can use OSCache through its straightforward API. You can use the GeneralCacheAdministrator to create, flush and administrate the cache. The GeneralCacheAdministrator has a cache instance and delegates different Cache's methods. Furthermore the GeneralCacheAdministrator is in charge of load the cache.properties and create a cache instance with the properties definded. You have to store an instance of the GeneralCacheAdministrator in a static value or use a singleton pattern to access the same GeneralCacheAdministrator. #### Typical use with fail over
    String myKey = "myKey";
    String myValue;
    int myRefreshPeriod = 1000;
    try {
        // Get from the cache
        myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
    } catch (NeedsRefreshException nre) {
        try {
            // Get the value (probably from the database)
            myValue = "This is the content retrieved.";
            // Store in the cache
            admin.putInCache(myKey, myValue);
        } catch (Exception ex) {
            // We have the current content if we want fail-over.
            myValue = (String) nre.getCacheContent();
            // It is essential that cancelUpdate is called if the
            // cached content is not rebuilt
            admin.cancelUpdate(myKey);
        }
    }
#### Typical use without fail over
    String myKey = "myKey";
    String myValue;
    int myRefreshPeriod = 1000;
    try {
        // Get from the cache
        myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
    } catch (NeedsRefreshException nre) {
        try {
            // Get the value (probably from the database)
            myValue = "This is the content retrieved.";
            // Store in the cache
            admin.putInCache(myKey, myValue);
            updated = true;
        } finally {
            if (!updated) {
                // It is essential that cancelUpdate is called if the
                // cached content could not be rebuilt
                admin.cancelUpdate(myKey);
            }
        }
    }
#### Note
| Be Careful If a NeedsRefreshException is raised you have to invoke admin.putInCache or even admin.cancelUpdate to avoid deadlock situation.