Search in sources :

Example 1 with BaseCacheConfig

use of com.lcache.config.BaseCacheConfig in project lcache by long172066912.

the class LettuceHandlesPostProcessor method onFail.

@Override
public void onFail(CacheHandleProcessorModel cacheHandleProcessorModel) {
    /**
     * 如果是lua脚本异常,重新缓存
     */
    if (cacheHandleProcessorModel.getE() instanceof RedisNoScriptException) {
        // 获取执行器调用脚本缓存操作
        CacheExecutorFactory.getCacheExecutor(null, cacheHandleProcessorModel.getCacheConfigModel()).loadLuaScripts();
    }
    // Lettuce非连接池方式增加连接重置功能
    if (cacheHandleProcessorModel.getCacheConfigModel().getConnectTypeEnum().equals(ConnectTypeEnum.SIMPLE) && cacheHandleProcessorModel.getE().getMessage().contains(CONNECTION_CLOSE_ERROR)) {
        // 重置连接
        try {
            synchronized (cacheHandleProcessorModel.getCacheConfigModel()) {
                // 获取配置
                BaseCacheConfig config = CacheExecutorFactory.getRedisSourceConfig(cacheHandleProcessorModel.getCacheConfigModel());
                // 判断连接是否有效
                LettuceConnectResource resource = (LettuceConnectResource) RedisConnectionManager.getConnectionResource(cacheHandleProcessorModel.getCacheConfigModel(), config).getResource();
                if (!resource.getStatefulRedisConnection().isOpen()) {
                    // 重置连接
                    RedisConnectionManager.resetConnectionResource(cacheHandleProcessorModel.getCacheConfigModel(), config);
                }
            }
        } catch (Exception e) {
            CacheExceptionFactory.addErrorLog("LettuceHandlesPostProcessor reset connection fail !", e);
        }
    }
}
Also used : BaseCacheConfig(com.lcache.config.BaseCacheConfig) LettuceConnectResource(com.lcache.extend.handle.redis.lettuce.connect.LettuceConnectResource) RedisNoScriptException(io.lettuce.core.RedisNoScriptException) RedisNoScriptException(io.lettuce.core.RedisNoScriptException)

Example 2 with BaseCacheConfig

use of com.lcache.config.BaseCacheConfig in project lcache by long172066912.

the class RedissonClientManager method getJedisRedissonConfig.

/**
 * Jedis配置转Redisson配置
 *
 * @param cacheConfigModel
 * @param cacheConfig
 * @return
 */
private static Config getJedisRedissonConfig(CacheConfigModel cacheConfigModel, BaseCacheConfig cacheConfig) {
    Config config = new Config();
    switch(cacheConfigModel.getConnectTypeEnum()) {
        case SIMPLE:
            JedisConnectSourceConfig simpleConfig = (JedisConnectSourceConfig) cacheConfig;
            config.useSingleServer().setAddress("redis://" + simpleConfig.getHost() + ":" + simpleConfig.getPort()).setPassword(StringUtils.isNotBlank(simpleConfig.getPwd()) ? simpleConfig.getPwd() : null);
            break;
        case POOL:
            JedisConnectSourceConfig poolConfig = (JedisConnectSourceConfig) cacheConfig;
            config.useSingleServer().setAddress("redis://" + poolConfig.getHost() + ":" + poolConfig.getPort()).setPassword(StringUtils.isNotBlank(poolConfig.getPwd()) ? poolConfig.getPwd() : null);
            break;
        case SHARDED:
            return null;
        case CLUSTER:
            JedisClusterConnectSourceConfig clusterConfig = (JedisClusterConnectSourceConfig) cacheConfig;
            for (HostAndPort hostAndPort : clusterConfig.getNodes()) {
                config.useClusterServers().addNodeAddress("redis://" + hostAndPort.getHost() + ":" + hostAndPort.getPort());
            }
            config.useClusterServers().setScanInterval(2000).setPassword(StringUtils.isNotBlank(clusterConfig.getPwd()) ? clusterConfig.getPwd() : null);
            break;
        case CLUSTER_POOL:
            JedisClusterConnectSourceConfig clusterPoolConfig = (JedisClusterConnectSourceConfig) cacheConfig;
            for (HostAndPort hostAndPort : clusterPoolConfig.getNodes()) {
                config.useClusterServers().addNodeAddress("redis://" + hostAndPort.getHost() + ":" + hostAndPort.getPort());
            }
            config.useClusterServers().setScanInterval(2000).setPassword(StringUtils.isNotBlank(clusterPoolConfig.getPwd()) ? clusterPoolConfig.getPwd() : null);
            break;
        default:
            return null;
    }
    return config;
}
Also used : JedisConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig) HostAndPort(redis.clients.jedis.HostAndPort) BaseCacheConfig(com.lcache.config.BaseCacheConfig) JedisClusterConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisClusterConnectSourceConfig) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) Config(org.redisson.config.Config) JedisConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig) LettuceClusterConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceClusterConnectSourceConfig) JedisClusterConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisClusterConnectSourceConfig)

Example 3 with BaseCacheConfig

use of com.lcache.config.BaseCacheConfig in project lcache by long172066912.

the class RedissonClientManager method getRedissonClient.

/**
 * 获取Redisson客户端
 *
 * @param cacheConfigModel
 * @param cacheConfig
 * @return
 */
public static synchronized RedissonClient getRedissonClient(CacheConfigModel cacheConfigModel, BaseCacheConfig cacheConfig) {
    RedissonClient redissonClient = null;
    String key = CacheConfigUtils.modelToHashKey(cacheConfigModel);
    if (null != redissonMap.get(key)) {
        redissonClient = redissonMap.get(key);
        if (!redissonClient.isShutdown()) {
            return redissonClient;
        }
    }
    Config config = getConfig(cacheConfigModel, cacheConfig);
    if (null == config) {
        CacheExceptionFactory.throwException("RedissonClientManager->getConfig error ! config is null !,cacheConfigModel:[{}],cacheConfig:[{}]", cacheConfigModel.toString() + JSON.toJSONString(cacheConfig));
    }
    // 设置线程数
    config.setThreads(REDISSON_THREAD_NUM);
    // 设置Netty线程数
    config.setNettyThreads(REDISSON_THREAD_NUM);
    redissonClient = Redisson.create(config);
    redissonMap.put(key, redissonClient);
    return redissonClient;
}
Also used : RedissonClient(org.redisson.api.RedissonClient) BaseCacheConfig(com.lcache.config.BaseCacheConfig) JedisClusterConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisClusterConnectSourceConfig) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) Config(org.redisson.config.Config) JedisConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig) LettuceClusterConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceClusterConnectSourceConfig)

Example 4 with BaseCacheConfig

use of com.lcache.config.BaseCacheConfig in project lcache by long172066912.

the class RedissonClientManager method getLettuceRedissonConfig.

/**
 * Lettuce配置转Redisson配置
 *
 * @param cacheConfigModel
 * @param cacheConfig
 * @return
 */
private static Config getLettuceRedissonConfig(CacheConfigModel cacheConfigModel, BaseCacheConfig cacheConfig) {
    Config config = new Config();
    switch(cacheConfigModel.getConnectTypeEnum()) {
        case SIMPLE:
            LettuceConnectSourceConfig simpleConfig = (LettuceConnectSourceConfig) cacheConfig;
            config.useSingleServer().setAddress("redis://" + simpleConfig.getHost() + ":" + simpleConfig.getPort()).setPassword(StringUtils.isNotBlank(simpleConfig.getPwd()) ? simpleConfig.getPwd() : null);
            break;
        case POOL:
            LettuceConnectSourceConfig poolConfig = (LettuceConnectSourceConfig) cacheConfig;
            config.useSingleServer().setAddress("redis://" + poolConfig.getHost() + ":" + poolConfig.getPort()).setPassword(StringUtils.isNotBlank(poolConfig.getPwd()) ? poolConfig.getPwd() : null);
            break;
        case SHARDED:
            return null;
        case CLUSTER:
            LettuceClusterConnectSourceConfig clusterConfig = (LettuceClusterConnectSourceConfig) cacheConfig;
            String pwd = "";
            for (LettuceConnectSourceConfig hostAndPort : clusterConfig.getNodes()) {
                config.useClusterServers().addNodeAddress("redis://" + hostAndPort.getHost() + ":" + hostAndPort.getPort());
                pwd = hostAndPort.getPwd();
            }
            config.useClusterServers().setScanInterval(2000).setPassword(StringUtils.isNotBlank(pwd) ? pwd : null);
            break;
        case CLUSTER_POOL:
            LettuceClusterConnectSourceConfig clusterPoolConfig = (LettuceClusterConnectSourceConfig) cacheConfig;
            String pwd1 = "";
            for (LettuceConnectSourceConfig hostAndPort : clusterPoolConfig.getNodes()) {
                config.useClusterServers().addNodeAddress("redis://" + hostAndPort.getHost() + ":" + hostAndPort.getPort());
                pwd1 = hostAndPort.getPwd();
            }
            config.useClusterServers().setScanInterval(2000).setPassword(StringUtils.isNotBlank(pwd1) ? pwd1 : null);
            break;
        default:
            return null;
    }
    return config;
}
Also used : BaseCacheConfig(com.lcache.config.BaseCacheConfig) JedisClusterConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisClusterConnectSourceConfig) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) Config(org.redisson.config.Config) JedisConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig) LettuceClusterConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceClusterConnectSourceConfig) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) LettuceClusterConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceClusterConnectSourceConfig)

Aggregations

BaseCacheConfig (com.lcache.config.BaseCacheConfig)4 JedisClusterConnectSourceConfig (com.lcache.extend.handle.redis.jedis.config.JedisClusterConnectSourceConfig)3 JedisConnectSourceConfig (com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig)3 LettuceClusterConnectSourceConfig (com.lcache.extend.handle.redis.lettuce.config.LettuceClusterConnectSourceConfig)3 LettuceConnectSourceConfig (com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig)3 Config (org.redisson.config.Config)3 LettuceConnectResource (com.lcache.extend.handle.redis.lettuce.connect.LettuceConnectResource)1 RedisNoScriptException (io.lettuce.core.RedisNoScriptException)1 RedissonClient (org.redisson.api.RedissonClient)1 HostAndPort (redis.clients.jedis.HostAndPort)1