All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.houkunlin.system.dict.starter.store.LocalDictStore Maven / Gradle / Ivy

package com.houkunlin.system.dict.starter.store;

import com.houkunlin.system.dict.starter.DictUtil;
import com.houkunlin.system.dict.starter.bean.DictTypeVo;
import com.houkunlin.system.dict.starter.bean.DictValueVo;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 使用本地 Map 来存储系统字典信息。当不存在 Redis 环境时默认使用该存储方式。
 *
 * @author HouKunLin
 */
@AllArgsConstructor
public class LocalDictStore implements DictStore, InitializingBean {
    private static final Logger logger = LoggerFactory.getLogger(LocalDictStore.class);
    private static final ConcurrentHashMap CACHE_TYPE = new ConcurrentHashMap<>();
    private static final ConcurrentHashMap CACHE_SYSTEM_TYPE = new ConcurrentHashMap<>();
    private static final ConcurrentHashMap CACHE_TEXT = new ConcurrentHashMap<>();
    private final RemoteDict remoteDict;

    @Override
    public void store(final DictTypeVo dictType) {
        final List children = dictType.getChildren();
        if (children == null) {
            removeDictType(dictType.getType());
        } else {
            CACHE_TYPE.put(dictType.getType(), dictType);
        }
    }

    @Override
    public void storeSystemDict(DictTypeVo dictType) {
        final List children = dictType.getChildren();
        if (children == null) {
            CACHE_SYSTEM_TYPE.remove(dictType.getType());
        } else {
            CACHE_SYSTEM_TYPE.put(dictType.getType(), dictType);
        }
    }

    @Override
    public void store(final Iterator iterator) {
        iterator.forEachRemaining(valueVo -> {
            final String dictKey = DictUtil.dictKey(valueVo);
            final String title = valueVo.getTitle();
            if (title == null) {
                CACHE_TEXT.remove(dictKey);
                if (logger.isDebugEnabled()) {
                    logger.debug("[removeDictValue] 字典值文本被删除 {}", dictKey);
                }
            } else {
                CACHE_TEXT.put(dictKey, title);
                // @since 1.4.6 - START
                final String dictParentKey = DictUtil.dictParentKey(valueVo);
                final Object parentValue = valueVo.getParentValue();
                if (parentValue == null) {
                    CACHE_TEXT.remove(dictParentKey);
                } else {
                    CACHE_TEXT.put(dictParentKey, parentValue.toString());
                }
                // @since 1.4.6 - END
            }
        });
    }

    @Override
    public void removeDictType(final String dictType) {
        CACHE_TYPE.remove(dictType);
        if (logger.isDebugEnabled()) {
            logger.debug("[removeDictType] 字典类型被删除 {}", dictType);
        }
        final String prefix = DictUtil.VALUE_PREFIX.concat(dictType);
        CACHE_TEXT.entrySet().removeIf(entry -> {
            final String entryKey = entry.getKey();
            if (entryKey != null && entryKey.startsWith(prefix)) {
                logger.debug("[removeDictType] 字典值文本被删除 {}", entryKey);
                return true;
            }
            return false;
        });
    }

    @Override
    public Set dictTypeKeys() {
        return CACHE_TYPE.keySet();
    }

    @Override
    public Set systemDictTypeKeys() {
        return CACHE_SYSTEM_TYPE.keySet();
    }

    @Override
    public DictTypeVo getDictType(final String type) {
        final DictTypeVo typeVo = CACHE_TYPE.get(type);
        if (typeVo != null) {
            return typeVo;
        }
        return remoteDict.getDictType(type);
    }

    @Override
    public String getDictText(final String type, final String value) {
        final String title = CACHE_TEXT.get(DictUtil.dictKey(type, value));
        if (title != null) {
            return title;
        }
        return remoteDict.getDictText(type, value);
    }

    @Override
    public String getDictParentValue(final String type, final String value) {
        return CACHE_TEXT.get(DictUtil.dictParentKey(type, value));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("使用 {} 存储数据字典信息", getClass().getName());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy