blockcachemanager.java

来自「用jbuilder写的源程序」· Java 代码 · 共 102 行

JAVA
102
字号
/*
 * Copyright 2003-2006 the original author or authors.
 * Licensed 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.
 * 
 */
package com.jdon.model.query.cache;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.log4j.Logger;

import com.jdon.controller.cache.CacheKey;
import com.jdon.controller.cache.CacheKeyFactory;
import com.jdon.controller.cache.CacheManager;

/**
 * query block manager.
 * batch query will get a collection result that it is a block,
 * this block will be cached, next time, we lookup the result
 * in the block that existed in cache, maybe in the block there 
 * are those promary keys collection, so reducing visiting database.
 * 
 * the block is made of the primary keys of all models.
 * 
 * @author <a href="mailto:banqiao@jdon.com">banq</a>
 *
 */
public class BlockCacheManager extends CacheKeyFactory {
    private final static Logger logger = Logger.getLogger(BlockCacheManager.class);

    public final static String CACHE_TYPE_BLOCK = "BLOCK";

    private CacheManager cacheManager;

    private List<CacheKey> cacheKeys;

    public BlockCacheManager(CacheManager cacheManager) {
        super(cacheManager);

        this.cacheKeys = new CopyOnWriteArrayList<CacheKey>();
        this.cacheManager = cacheManager;

    }

    public List getBlockKeysFromCache(QueryConditonDatakey qckey) {
        CacheKey cacheKey = getCacheKey(qckey);
        return (List) cacheManager.fetchObject(cacheKey);

    }

    public CacheKey getCacheKey(QueryConditonDatakey qckey) {
        return createCacheKey(qckey, Integer.toString(qckey.getBlockStart()));
    }

    public void saveBlockKeys(QueryConditonDatakey qckey, List keys) {
        CacheKey cacheKey = getCacheKey(qckey);
        cacheManager.putObect(cacheKey, keys);
        cacheKeys.add(cacheKey);
    }

    public Integer getAllCountsFromCache(QueryConditonDatakey qckey) {
        CacheKey cacheKey = getCacheKey(qckey);
        return (Integer) cacheManager.fetchObject(cacheKey);
    }

    public void saveAllCounts(QueryConditonDatakey qckey, Integer allCount) {
        CacheKey cacheKey = getCacheKey(qckey);
        cacheManager.putObect(cacheKey, allCount);
        cacheKeys.add(cacheKey);
    }

    public CacheKey createCacheKeyImp(Object dataKey, String typeName) {
        return new CacheKey(CACHE_TYPE_BLOCK, dataKey, typeName);
    }

    public synchronized void clearCache() {
        try {
            Iterator iter = cacheKeys.iterator();
            while (iter.hasNext()) {
                CacheKey cacheKey = (CacheKey) iter.next();
                cacheManager.removeObect(cacheKey);
            }
        } catch (Exception e) {
            logger.error(e);
        } finally {
            cacheKeys.clear();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?