I am not familiar with Apache Torque. But in general I found it very odd
that the file chache.ccf has to be at the top of your root directory. I
dislike this on the current JCS code.

What if another library also makes use of JCS? Such library may already
have included the chache.ccf file in their JAR file. If such library is
in the class path before yours, then that file gets loaded. But if yours
is loaded first, then the library may not work since it won't find its
regions definitions.

Instead of using JCS class to create the cache instances, I created a
CacheFactory class. This class allows to have multiple cache.ccf files
(but I named them jcs.properties). In this way, you can use JCS from
multiple class loaders and there is no need to merge all cache
definitions in the same cache.ccf

Hoep you find it useful. You will also need the file
jcsdefaults.properties located in the same package as the class
CacheFactory. 

In the jcs.properties files, just define your own cache instance
properties. The defaults are defined in jcsdefaults.properties

Creating a cache is then performed by calling

JCS myCache = CacheFactory.createCache(clazz, region)
The clazz argument defines the location of the jcs.properties files that
must contain the definition of the region.

-Jorge



public class CacheFactory {

        private static final String DEFAULT_VALUES_SETTINGS_FILE =
"jcsdefaults.properties";

        private static Properties defaultProperties = new Properties();

        private static String defaultsFile = null;

        private CacheFactory() {

        }


        /**
         * An auxiliary method to create JCS caches.
         * This method looks for the file jcs.properties in the same
package as the
         * provided Class. The cache is created by using the region
cache and
         * element attributes specified in the jcs.properties file.
         *
         * Note that region names are global. Region names must not
contain "/" or "."
         * If you ask two times for the same region, you will get a
different JCS object
         * but they act over the same region, therefore elements in the
cache are accesible
         * to both.
         *
         * The key and elements to be inserted in the cache MUST be
Serializable. This is
         * expected by JCS because it can cache elements to disk or
provide them to a remote
         * agent, even if the cache is just defined as in-memory cache.
         *
         * @param clazz  - A clazz used to specify the location of the
jcs.properties file
         * @param region - The region name
         * @return a JCS cache object with access to the specified
region
         */
        public static JCS createCache(Class clazz, String region) {


                if( defaultProperties.isEmpty()  ) {
                        String pkg =
CacheFactory.class.getPackage().getName();
                        pkg = pkg.replace(".", "/");
                        defaultsFile = "/" + pkg + "/" +
DEFAULT_VALUES_SETTINGS_FILE;
                        JCS.setConfigFilename( defaultsFile );
                        try {
                                defaultProperties.load(
CacheFactory.class.getResourceAsStream(DEFAULT_VALUES_SETTINGS_FILE ));
                        } catch (IOException ioe) {
                                throw new RuntimeException("Unable to
find resource ", ioe );
                        }
                }

                Properties props = new Properties();
                CompositeCacheAttributes cca = new
CompositeCacheAttributes();
                ElementAttributes eca = new ElementAttributes();

                try {
                        // First apply the default values from
jcs.default
                        PropertySetter.setProperties(cca,
defaultProperties, "jcs.default.cacheattributes.");
                        PropertySetter.setProperties(eca,
defaultProperties, "jcs.default.elementattributes.");
                        // Now apply the values from a specific
jcs.propeties file
                        InputStream is =
clazz.getResourceAsStream("jcs.properties");
                        if( is != null ) {
                                props.load( is );
                                PropertySetter.setProperties(cca,props,
"jcs.region." + region + ".cacheattributes.");
                                PropertySetter.setProperties(eca,props,
"jcs.region." + region + ".elementattributes.");
                        }
                } catch (IOException ioe) {
                        throw new RuntimeException("Unable to read
jcs.properties from package " + clazz.getPackage().getName() , ioe);
                }
                JCS resp = null;
                try {
                        resp = JCS.getInstance(region, cca);
                        resp.setDefaultElementAttributes(eca);
                } catch (CacheException e) {
                        throw new RuntimeException("Unable to set
initialize JCS cache for region name [" +
                                                region +"]",e);
                }
                return resp;
        }
}


---------- jcsdefaults.properties ---------------

#---------------- Default attributes for cache regions ----------------
# Default attributes for caches
jcs.default=
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttribut
es
jcs.default.cacheattributes.MaxObjects=250
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory
.lru.LRUMemoryCache
jcs.default.cacheattributes.UseMemoryShrinker=false
jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=600
jcs.default.cacheattributes.ShrinkerIntervalSeconds=300

# Default attributes for cached elements
jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.default.elementattributes.IsEternal=true
#jcs.default.elementattributes.MaxLifeSeconds=60
#jcs.default.elementattributes.IsSpool=true
#jcs.default.elementattributes.IsRemote=true
#jcs.default.elementattributes.IsLateral=true


-----Original Message-----
From: Graham Leggett [mailto:[EMAIL PROTECTED] 
Sent: Saturday, July 05, 2008 6:24 PM
To: JCS Users List
Subject: Finding cache.ccf

Hi all,

I am trying to configure JCS embedded within Apache Torque, which in 
theory is as simple as creating cache.ccf and placing it in the
classpath.

In practice, JCS.getInstance(region) returns null every time, and I have

spent the last few hours stabbing in the dark as to why.

Some questions:

- Is there a way that I can receive confirmation that JCS found a 
cache.ccf file, and if so, where it found the file?

- If JCS did find the file, does JCS respond to configuration errors by 
throwing an exception with a suitable error message, or blindly 
returning null?

The following places for cache.ccf don't work:

- In the root of the jar containing the code that uses JCS
- In the /WEB-INF directory of the webapp that wraps the code
- In the /WEB-INF/classes directory of the webapp that wraps the code

The cache.ccf file looks like this:

# Default setup
jcs.default=
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttribut
es
jcs.default.cacheattributes.MaxObjects=10000
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory
.lru.LRUMemoryCache

jcs.region.com_domain_Signature=
jcs.region.com_domain_Signature.cacheattributes=org.apache.stratum.jcs.e
ngine.CompositeCacheAttributes
jcs.region.com_domain_Signature.cacheattributes.MaxObjects=1200
jcs.region.com_domain_Signature.cacheattributes.MemoryCacheName=org.apac
he.stratum.jcs.engine.memory.lru.LRUMemoryCache

Does anyone have any ideas?

Regards,
Graham
--

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to