⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelcachemanager.java

📁 一个非常好的FRAMWRK!是一个外国组织做的!不!
💻 JAVA
字号:
/**
 * Copyright 2003-2005 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.cache;

import com.jdon.container.pico.Startable;
import com.jdon.controller.cache.CacheKey;
import com.jdon.controller.cache.CacheKeyFactory;
import com.jdon.controller.cache.CacheManager;
import com.jdon.controller.model.Model;
import com.jdon.model.config.ModelXmlConfig;
import com.jdon.model.mapping.ModelMapping;
import com.jdon.util.Debug;

public class ModelCacheManager implements Startable {

    private final static String module = ModelCacheManager.class.getName();

    protected CacheManager cacheManager;

    private CacheKeyFactory cacheKeyFactory;

    private ModelXmlConfig modelFactory;

    public ModelCacheManager(ModelXmlConfig modelFactory, CacheManager cacheManager) {
        this.cacheManager = cacheManager;
        this.modelFactory = modelFactory;
        this.cacheKeyFactory = new ModelCacheKeyFactory(cacheManager);
    }

    public void start() {
        Debug.logVerbose("ModelCacheManager start ...", module);
    }

    public void stop() {

    }

    /**
     * 将数据ID, Form名称组合成Key, 将对应的Model保存到缓存
     * 
     * 基于一个Form名称对应一个Model类型
     * 
     * @param dataKey
     * @param formName
     * @param model
     */
    public void setCache(Object dataKey, String formName, Model model) {
        CacheKey cachKey = getCacheKey(dataKey, formName);
        if (cachKey != null)
            saveToCache(cachKey, model);
    }

    private void saveToCache(CacheKey cachKey, Model model) {
        if ((model != null) && (model.isCacheable())) { //保存到缓存中
            cacheManager.putObect(cachKey, model);
        }
    }

    /**
     * 将数据ID, Model类类型组合成Key, 将对应的Model保存到缓存
     * 
     * @param dataKey
     * @param modelClassName
     * @param model
     */
    public void setCache2(Object dataKey, String modelClassName, Model model) {
        CacheKey cachKey = getCacheKey2(dataKey, modelClassName);
        if (cachKey != null)
            saveToCache(cachKey, model);
    }

    /**
     * 将数据ID, Form名称组合成Key的对应Model从缓存中取出
     * 
     * 基于一个Form名称对应一个Model类型
     * 
     * @param dataKey
     * @param formName
     * @return Model
     */
    public Model getCache(Object dataKey, String formName) {
        CacheKey cachKey = getCacheKey(dataKey, formName);
        if (cachKey != null)
            return getModelFromCache(cachKey);
        else
            return null;
    }

    private Model getModelFromCache(CacheKey cachKey) {
        Model model = (Model) cacheManager.fetchObject(cachKey);
        if ((model != null) && model.isModified()) { //如果缓存中指向的Model已经被修改,就放弃。
            model = null;
        }
        return model;
    }

    /**
     * 将数据ID, Model类类型组合成Key的对应Model从缓存中取出
     * 
     * @param dataKey
     * @param modelClassName
     * @return Model
     */
    public Model getCache2(Object dataKey, String modelClassName) {
        CacheKey cachKey = getCacheKey2(dataKey, modelClassName);
        if (cachKey != null)
            return getModelFromCache(cachKey);
        else
            return null;
    }

    /**
     * 清除缓存中该dataKey的相关所有缓存数据 dataKey即数据的主键ID数值
     * 
     * @param dataKey
     * @param formName
     */
    public void removeCache(Object dataKey) {
        cacheManager.removeCache(dataKey, cacheKeyFactory);
    }

    /**
     * 将数据ID, Model类类型组合成Key的对应Model从缓存中取出
     * 
     * @param dataKey
     * @param modelClassName
     * @return
     */
    public void removeCache2(Object dataKey, String modelClassName) {
        CacheKey cachKey = getCacheKey2(dataKey, modelClassName);
        if (cachKey != null)
            cacheManager.removeObect(cachKey);
    }

    /**
     * 生成缓存中保存Model的Key,根据ActionForm名称
     * 
     * @param dataKey
     * @param formName
     * @return String
     */
    public CacheKey getCacheKey(Object dataKey, String formName) {
        ModelMapping modelMapping = this.modelFactory.getModelMapping(formName);
        return cacheKeyFactory.createCacheKey(dataKey, modelMapping.getClassName());
    }

    public CacheKey getCacheKey2(Object dataKey, String modelClassName) {
        return cacheKeyFactory.createCacheKey(dataKey, modelClassName);
    }

    public void clearCache() {
        cacheManager.clear();
    }

}

⌨️ 快捷键说明

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