Search in sources :

Example 1 with TranslateEhcacheManager

use of org.sagacity.sqltoy.translate.cache.impl.TranslateEhcacheManager in project sagacity-sqltoy by chenrenfei.

the class TranslateManager method initialize.

public void initialize(SqlToyContext sqlToyContext) throws Exception {
    if (initialized)
        return;
    logger.debug("开始加载sqltoy的translate缓存翻译配置文件..........................");
    try {
        // 加载和解析缓存翻译的配置
        DefaultConfig defaultConfig = TranslateConfigParse.parseTranslateConfig(sqlToyContext, translateMap, updateCheckers, translateConfig, charset);
        // 配置了缓存翻译
        if (defaultConfig != null) {
            if (translateCacheManager == null)
                translateCacheManager = new TranslateEhcacheManager();
            if (!StringUtil.isBlank(defaultConfig.getDiskStorePath()))
                ((TranslateEhcacheManager) translateCacheManager).setDiskStorePath(defaultConfig.getDiskStorePath());
            translateCacheManager.init();
            initialized = true;
            if (timer == null)
                timer = new Timer();
            /**
             * 每隔1秒执行一次检查(检查各个任务时间间隔是否到达设定的区间,并不意味着一秒执行数据库或调用接口) 正常情况下,这种检查都是高效率的空转不影响性能
             */
            timer.schedule(new CacheCheckTimer(sqlToyContext, translateCacheManager, updateCheckers), 20000, 1000);
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("加载和解析xml过程发生异常!{}", e.getMessage(), e);
        throw e;
    }
}
Also used : Timer(java.util.Timer) DefaultConfig(org.sagacity.sqltoy.translate.model.DefaultConfig) TranslateEhcacheManager(org.sagacity.sqltoy.translate.cache.impl.TranslateEhcacheManager)

Example 2 with TranslateEhcacheManager

use of org.sagacity.sqltoy.translate.cache.impl.TranslateEhcacheManager in project sagacity-sqltoy by chenrenfei.

the class TranslateManager method initialize.

/**
 * @TODO 初始化缓存管理器
 * @param sqlToyContext
 * @param cacheManager           自定义的缓存管理器(一般为null)
 * @param delayCheckCacheSeconds 延时多久进行更新检测
 * @throws Exception
 */
public synchronized void initialize(SqlToyContext sqlToyContext, TranslateCacheManager cacheManager, int delayCheckCacheSeconds) throws Exception {
    // 防止被多次调用
    if (initialized) {
        return;
    }
    try {
        this.sqlToyContext = sqlToyContext;
        initialized = true;
        logger.debug("开始加载sqltoy的translate缓存翻译配置文件:{}", translateConfig);
        // 加载和解析缓存翻译的配置
        DefaultConfig defaultConfig = TranslateConfigParse.parseTranslateConfig(sqlToyContext, translateMap, updateCheckers, translateConfig, charset);
        // 配置了缓存翻译
        if (!translateMap.isEmpty()) {
            // 可以自定义缓存管理器,默认为ehcache实现
            if (cacheManager == null) {
                translateCacheManager = new TranslateEhcacheManager();
            } else {
                translateCacheManager = cacheManager;
            }
            // 设置默认存储路径
            if (!StringUtil.isBlank(defaultConfig.getDiskStorePath()) && translateCacheManager instanceof TranslateEhcacheManager) {
                ((TranslateEhcacheManager) translateCacheManager).setDiskStorePath(defaultConfig.getDiskStorePath());
            }
            // 设置装入具体缓存配置
            translateCacheManager.setTranslateMap(translateMap);
            boolean initSuccess = translateCacheManager.init();
            // 这种检查都是高效率的空转不影响性能
            if (initSuccess && !updateCheckers.isEmpty()) {
                cacheCheck = new CacheUpdateWatcher(sqlToyContext, translateCacheManager, updateCheckers, delayCheckCacheSeconds, defaultConfig.getDeviationSeconds());
                cacheCheck.start();
                logger.debug("sqltoy的translate缓存配置加载完成,已经启动:{} 个缓存更新检测!", updateCheckers.size());
            } else {
                logger.debug("sqltoy的translate缓存配置加载完成,您没有配置缓存更新检测机制或没有配置缓存,将不做缓存更新检测!");
            }
        } else {
            logger.warn("translateConfig={} 中未定义缓存,请正确定义,如不使用缓存翻译可忽视此提示!", translateConfig);
        }
    } catch (Exception e) {
        logger.error("加载sqltoy的translate缓存翻译过程发生异常!{}", e.getMessage(), e);
        throw e;
    }
}
Also used : DefaultConfig(org.sagacity.sqltoy.translate.model.DefaultConfig) TranslateEhcacheManager(org.sagacity.sqltoy.translate.cache.impl.TranslateEhcacheManager)

Aggregations

TranslateEhcacheManager (org.sagacity.sqltoy.translate.cache.impl.TranslateEhcacheManager)2 DefaultConfig (org.sagacity.sqltoy.translate.model.DefaultConfig)2 Timer (java.util.Timer)1