1. 程式人生 > >把資料加入快取中

把資料加入快取中

package com.cmsz.rc.cache;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author cym
 * 
 */
public class AllLevelCache {

    /**
     * 日誌記錄
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(AllLevelCache.class);

    /**
     * BusinessLineCache管理(執行緒同步)
     */
    private static Map<String, String> levelMap = new ConcurrentHashMap<String, String>();

    /**
     * setBusinessLine
     * 
     * @param code
     * 
     * @param businessLine
     */
    public static synchronized void setLevel(String code, String name) {
        if (!levelMap.containsKey(code)) {
            levelMap.put(code, name);
        }
    }

    /**
     * getBusinessLine
     * 
     * 根據傳入的code來唯一標示一個BusinessLine,第一次取用BusinessLine之前 先向map放入BusinessLine
     * 
     * @param code
     * 
     */
    public static synchronized String getLevel(String code) {
        if (levelMap.containsKey(code)) {
            return levelMap.get(code);
        }
        return null;
    }

    public static String getCode(String name) {
        String code = null;
        for (Map.Entry<String, String> entry : levelMap.entrySet()) {
            code = entry.getKey();
            String temp = levelMap.get(code);
            if (name.equals(temp)) {
                return code;
            }
        }
        LOGGER.error("根據業務線名字返回code 為 null ");
        return null;
    }


    /**
     * @description 獲取快取大小
     * @return
     */
    public static Integer getSize() {
        return levelMap.size();
    }

    /**
     * 私有的建構函式
     */
    private AllLevelCache() {

    }

}