Oracle Coherence

What is Coherence cache as per Wikipedia:


"In computing, cache coherence (also cache coherency) refers to the consistency of data stored in local caches of a shared resource.
In a shared memory multiprocessor system with a separate cache memory for each processor, it is possible to have many copies of any one instruction operand: one copy in the main memory and one in each cache memory. When one copy of an operand is changed, the other copies of the operand must be changed also. Cache coherence is the discipline that ensures that changes in the values of shared operands are propagated throughout the system in a timely fashion."
 
Oracle give you the ability to store information in a shared memory location called coherence. Once you have started using it your life will be allot easier when it comes to managing your Middleware environment. It can also help you with Data Value Mapping for when you send certain values in and will return with a transformed result.
 
A nice feature around Oracle Coherence is that ir's cluster aware so doesn't matter that your environemnt is built around a cluster, it will still be able to use the same values from the same coherence and you can also manage it from one point.

Requirements:
  1. coherence.jar
  2. JDeveloper
  3. Some Java

How to start the cache

  • When installing WebLogic Server it will create a Middleware home for you with some files in it. Located in the Middleware home is a coherence_3.7 directory. Naviagte your way to the lib directory.
  • Within the lib directory, locate the coherence.jar
  • Open you JDeveloper and add coherence.jar into your Libraries and Classpath 
  • You can start your development using Java:

I created some methods that called getValueFromCache() {} and also a setCacheValueItem(){}



See code Below:
  1. Click File new adn create a Generic Project
  2. Give Project name and select Java from list
  3. Click Finish
  4. add coherence.jar into your Libraries and Classpath 
  5. On you project, right click "new" and create new java Class and call it CacheHelper
  6. Add the below code snippets

    public String getValueFromCache(String cacheName, String key)
    {
      try
      {
        CacheFactory.ensureCluster();

        NamedCache cache = CacheFactory.getCache(cacheName);
        return "" + cache.get(key);
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }return null;
    }



    public boolean setCacheValueItem(String cacheName, String key, String value)
    {
      try {
        CacheFactory.ensureCluster();

        NamedCache cache= CacheFactory.getCache(cacheName);
        myCache.put(key, value, -1L);
      }
      catch (Exception ec)
      {
        ec.printStackTrace();
        return false;
      }

      return true;
    }



After using the above code, you can from you main class add some values and view  those added values:

    public static void main(String[] args) {
               
        CacheHelper myCache = new CacheHelper();
        //myCache.addCacheItem(cacheName, key, value)
        myCache.setCacheValueItem("local-SOAStaticCache", "bla.bla.password", "xxxxxxx");
       
       
        String value = myCache.getValueFromCache("local-SOAStaticCache", "bla.bla.password");
        System.out.println("This is my value from coherance: " + value);
    }
}


When you run the above in JDeveloper, it will run your coherence in the JVM and spit out the result from cache...
 
What you want to do with the above is create someting that loads values from a database and evetually have some management utililty (ADF APP) to manage the items in the database that it will load into the cache.

I am using something like this and it works quite well. I have also seen this work quite well in a productioin environment where items can be loaded into a cache database and by restarting only one instance in a cluster it will read and load all new items into cache. Rememeber that it's cluster aware, so it's only neccesary for one instance to be restarted. You can add your new class into you classloader section in the WebLogic config file.

No comments:

Post a Comment