JCS---一个实现

来源:互联网 发布:重庆淘宝设计运营培训 编辑:程序博客网 时间:2024/05/16 12:29

Db4oObjManager:



package com.wl.cache;


import net.sourceforge.wurfl.core.resource.ModelDevice;

import org.apache.jcs.JCS;

import com.zx.dao.WurflDao;

public class Db4oObjManager
{
    private static Db4oObjManager instance;
    private static int checkedOut = 0;
    private static JCS bookCache;

    private Db4oObjManager()
    {
        try
        {
            bookCache = JCS.getInstance("testCache1");
        }
        catch (Exception e)
        {
            System.out.println("bookCache is null");
            // Handle cache region initialization failure
        }

        // Do other initialization that may be necessary, such as getting
        // references to any data access classes we may need to populate
        // value objects later
    }

    /**
     * Singleton access point to the manager.
     */
    public static Db4oObjManager getInstance()
    {
        synchronized (Db4oObjManager.class)
        {
            if (instance == null)
            {
                instance = new Db4oObjManager();
            }
        }

        synchronized (instance)
        {
            instance.checkedOut++;
        }

        return instance;
    }
    /**
     * Retrieves a BookVObj.  Default to look in the cache.
     */
    public ModelDevice getBookVObj(String id)
    {
        return getBookVObj(id, true);
    }

    /**
     * Retrieves a BookVObj. Second argument decides whether to look
     * in the cache. Returns a new value object if one can't be
     * loaded from the database. Database cache synchronization is
     * handled by removing cache elements upon modification.
     */
    public ModelDevice getBookVObj(String id, boolean fromCache)
    {
        ModelDevice vObj = null;

        // First, if requested, attempt to load from cache

        if (fromCache)
        {
//            System.out.println(bookCache);
            vObj = (ModelDevice) bookCache.get("testCache1" + id);
        }

        // Either fromCache was false or the object was not found, so
        // call loadBookVObj to create it

        if (vObj == null)
        {
            vObj = loadBookVObj(id);
        }
//        else
//        {
//            System.out.println("bookCache get device from cache "+ id);
//        }

        return  vObj;
    }

    /**
     * Creates a BookVObj based on the id of the BOOK table.  Data
     * access could be direct JDBC, some or mapping tool, or an EJB.
     */
    public ModelDevice loadBookVObj(String id)
    {
//        ModelDevice vObj = new BookVObj();
        ModelDevice vObj = null;

//        vObj.bookID = id;

        try
        {
            boolean found = false;

            // load the data and set the rest of the fields
            // set found to true if it was found
            
            vObj=WurflDao.queryById(id);
            
            if(vObj!=null)
            {
                found = true;
            }
            else
            {
                System.out.println("cant find modeldevice from db");
            }
            

            // cache the value object if found

            if (found)
            {
                // could use the defaults like this
                // or specify special characteristics

                // put to cache

                bookCache.put("testCache1" + id, vObj);
                System.out.println("bookCache put "+ id);
            }

        }
        catch (Exception e)
        {
            // Handle failure putting object to cache
        }

        return vObj;
    }
    
    /**
     * Stores BookVObj's in database.  Clears old items and caches
     * new.
     */
    public void storeBookVObj(ModelDevice vObj)
    {
        try
        {
            // since any cached data is no longer valid, we should
            // remove the item from the cache if it an update.

            if (vObj.getID() != "")
            {
                bookCache.remove("testCache1" + vObj.getID());
            }

            // put the new object in the cache

            bookCache.put("testCache1" + vObj.getID(), vObj);
        }
        catch (Exception e)
        {
            // Handle failure removing object or putting object to cache.
        }
    }
}





cache.ccf :


# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
# #############################################################
# ################# DEFAULT CACHE REGION  #####################
# sets the default aux value for any non configured caches


# #############################################################
# ################# CACHE REGIONS AVAILABLE ###################
# Regions preconfirgured for caching
jcs.region.testCache1=DC
jcs.region.testCache1.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.testCache1.cacheattributes.MaxObjects=10000
jcs.region.testCache1.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.testCache1.cacheattributes.UseMemoryShrinker=true
jcs.region.testCache1.cacheattributes.ShrinkerIntervalSeconds=30
jcs.region.testCache1.cacheattributes.MaxMemoryIdleTimeSeconds=300
jcs.region.testCache1.cacheattributes.MaxSpoolPerRun=100
jcs.region.testCache1.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.testCache1.elementattributes.IsEternal=false
jcs.region.testCache1.elementattributes.IsLateral=true