org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory Java Examples

The following examples show how to use org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedisConfig.java    From SpringMVC-Project with MIT License 8 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(hostName);
    redisStandaloneConfiguration.setPort(port);

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setMaxWaitMillis(maxWaitMillis);
    poolConfig.setMaxTotal(maxTotal);

    LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder()
            .commandTimeout(Duration.ofSeconds(10))
            .shutdownTimeout(Duration.ZERO)
            .poolConfig(poolConfig)
            .build();

    LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, lettucePoolingClientConfiguration);
    lettuceConnectionFactory.setShareNativeConnection(false);

    return lettuceConnectionFactory;
}
 
Example #2
Source File: RedisCacheFactory.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Override
protected RedisConnectionFactory createConnectionClient(String hostName, int port) {
	RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
	configuration.setHostName(hostName);
	configuration.setPort(port);
	if (JEDIS_AVAILABLE) {
		return new JedisConnectionFactory(configuration);
	}
	else if (LETTUCE_AVAILABLE) {
		return new LettuceConnectionFactory(configuration);
	}
	else {
		throw new IllegalArgumentException("No Jedis or lettuce client on classpath. "
				+ "Please add one of the implementation to your classpath");
	}
}
 
Example #3
Source File: EmbeddedRedis.java    From distributed-lock with MIT License 6 votes vote down vote up
@PostConstruct
public void start() throws IOException {
  // find free port
  final var serverSocket = new ServerSocket(0);
  final var port = serverSocket.getLocalPort();
  serverSocket.close();

  server = RedisServer.builder().setting("bind 127.0.0.1").port(port).build(); // bind to ignore windows firewall popup each time the server starts
  server.start();

  final var connectionFactory = new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", port));
  connectionFactory.setDatabase(0);
  connectionFactory.afterPropertiesSet();

  // set the property so that RedisAutoConfiguration picks up the right port
  System.setProperty("spring.redis.port", String.valueOf(port));
}
 
Example #4
Source File: CacheConfig.java    From UserCenter with MIT License 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #5
Source File: LettuceConfig.java    From DisCal-Discord-Bot with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
	final LettuceConnectionFactory factory = new LettuceConnectionFactory();

	factory.setHostName(BotSettings.REDIS_HOSTNAME.get());
	factory.setPort(Integer.valueOf(BotSettings.REDIS_PORT.get()));
	factory.setPassword(BotSettings.REDIS_PASSWORD.get());

	return factory;
}
 
Example #6
Source File: LettuceConfig.java    From DisCal-Discord-Bot with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
	final LettuceConnectionFactory factory = new LettuceConnectionFactory();

	factory.setHostName(BotSettings.REDIS_HOSTNAME.get());
	factory.setPort(Integer.valueOf(BotSettings.REDIS_PORT.get()));
	factory.setPassword(BotSettings.REDIS_PASSWORD.get());

	return factory;
}
 
Example #7
Source File: RedisConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,因此支持序列化
 */
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #8
Source File: Redis.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public LettuceConnectionFactory getConnectionFactory() {
  if (clusterConnection.valid()) {
    return new LettuceConnectionFactory(clusterConnection.getConfig());
  }
  if (connection.valid()) {
    RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration(
        connection.getAddress(),
        connection.getPort());
    if (!connection.noPassword()) {
      standaloneConfig.setPassword(RedisPassword.of(connection.getPassword()));
    }
    return new LettuceConnectionFactory(standaloneConfig);
  }
  throw new InvalidConfigException("No valid connection configured");
}
 
Example #9
Source File: RedisCacheConfig.java    From x7 with Apache License 2.0 5 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(hostName, port);
    LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config);
    connectionFactory.afterPropertiesSet();
    return connectionFactory;
}
 
Example #10
Source File: L3RedisCacheConfig.java    From x7 with Apache License 2.0 5 votes vote down vote up
@Bean
public L3CacheStorageCustomizer l3CacheStorageCustomizer(){

    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(hostName, port);
    config.setPassword(password);
    config.setDatabase(database);

    LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config);
    connectionFactory.afterPropertiesSet();


    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
            Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);

    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    stringRedisTemplate.setConnectionFactory(connectionFactory);
    stringRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
    stringRedisTemplate.setHashKeySerializer(new StringRedisSerializer());

    stringRedisTemplate.afterPropertiesSet();

    return () -> {

        L3CacheStorage l3CacheStorage = new DefaultL3CacheStorage();
        ((DefaultL3CacheStorage) l3CacheStorage).setStringRedisTemplate(stringRedisTemplate);

        return l3CacheStorage;
    };
}
 
Example #11
Source File: RedisConnectionFactoryCloudConfigTestHelper.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
public static void assertNoPoolProperties(RedisConnectionFactory connector) {
	if (connector instanceof JedisConnectionFactory) {
		assertFalse(((JedisConnectionFactory) connector).getUsePool());
	} else if (connector instanceof LettuceConnectionFactory) {
		LettuceClientConfiguration config = ((LettuceConnectionFactory) connector).getClientConfiguration();
		assertThat(config, instanceOf(LettuceClientConfiguration.class));
	}
}
 
Example #12
Source File: RedisConfig.java    From spring-boot-redis-guava-caffeine-cache with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public LettuceConnectionFactory factoryA(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfigA) {
    LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
            .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    return new LettuceConnectionFactory(redisConfigA, clientConfiguration);
}
 
Example #13
Source File: RedisConfig.java    From ueboot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(type = {"org.springframework.data.redis.core.RedisTemplate"})
public RedisTemplate<?, ?> redisTemplate(
        LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().build(),ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setConnectionFactory(redisConnectionFactory);
    template.afterPropertiesSet();
    return template;
}
 
Example #14
Source File: CacheConfig.java    From UserCenter with MIT License 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #15
Source File: LettuceRedisInitializer.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
private LettuceConnectionFactory getLettuceConnectionFactory(GenericApplicationContext context) {
    final LettuceConnectionConfiguration configuration = new LettuceConnectionConfiguration(redisProperties, context.getBeanProvider(RedisSentinelConfiguration.class), context.getBeanProvider(RedisClusterConfiguration.class));
    final ClientResources clientResources = DefaultClientResources.create();
    try {
        return configuration.redisConnectionFactory(context.getBeanProvider(LettuceClientConfigurationBuilderCustomizer.class), clientResources);
    } catch (UnknownHostException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #16
Source File: RedisConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,因此支持序列化
 */
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #17
Source File: HomeController.java    From hello-spring-cloud with Apache License 2.0 5 votes vote down vote up
private String toString(RedisConnectionFactory redisConnectionFactory) {
    if (redisConnectionFactory == null) {
        return "<none>";
    } else {
        if (redisConnectionFactory instanceof JedisConnectionFactory) {
            JedisConnectionFactory jcf = (JedisConnectionFactory) redisConnectionFactory;
            return jcf.getHostName().toString() + ":" + jcf.getPort();
        } else if (redisConnectionFactory instanceof LettuceConnectionFactory) {
            LettuceConnectionFactory lcf = (LettuceConnectionFactory) redisConnectionFactory;
            return lcf.getHostName().toString() + ":" + lcf.getPort();
        }
        return "<unknown> " + redisConnectionFactory.getClass();
    }
}
 
Example #18
Source File: RedisCacheConfig.java    From SpringBootBucket with MIT License 5 votes vote down vote up
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisStandaloneConfiguration redisConf = new RedisStandaloneConfiguration();
    redisConf.setHostName(env.getProperty("spring.redis.host"));
    redisConf.setPort(Integer.parseInt(env.getProperty("spring.redis.port")));
    redisConf.setPassword(RedisPassword.of(env.getProperty("spring.redis.password")));
    return new LettuceConnectionFactory(redisConf);
}
 
Example #19
Source File: HerokuRedisConfig.java    From spring-redis-websocket with Apache License 2.0 5 votes vote down vote up
@SneakyThrows
private LettuceConnectionFactory lettuceConnectionFactory() {
	String redisUrl = System.getenv("REDIS_URL");
	URI redistogoUri = new URI(redisUrl);
	RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redistogoUri.getHost(), redistogoUri.getPort());
	redisStandaloneConfiguration.setPassword(redistogoUri.getUserInfo().split(":", 2)[1]);
	return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
 
Example #20
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #21
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #22
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #23
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #24
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #25
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #26
Source File: commons.java    From SpringBootLearn with Apache License 2.0 5 votes vote down vote up
/**
 * RedisTemplate
 *
 * @return
 */
@Bean
public RedisTemplate<String, Serializable> limitRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #27
Source File: RedisCacheAutoConfiguration.java    From netty-learning-example with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #28
Source File: RedisAutoConfig.java    From unimall with Apache License 2.0 5 votes vote down vote up
/**** 用户SESSION专用数据源 ****/
@Bean
public LettuceConnectionFactory userLettuceConnectionFactory(
        RedisStandaloneConfiguration userRedisConfig,GenericObjectPoolConfig userPoolConfig) {
    LettuceClientConfiguration clientConfig =
            LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(5000))
                    .poolConfig(userPoolConfig).build();
    return new LettuceConnectionFactory(userRedisConfig, clientConfig);
}
 
Example #29
Source File: RedisAutoConfig.java    From unimall with Apache License 2.0 5 votes vote down vote up
/**** 缓存专用数据源 ****/
@Bean
public LettuceConnectionFactory defaultLettuceConnectionFactory(
        RedisStandaloneConfiguration defaultRedisConfig,GenericObjectPoolConfig defaultPoolConfig) {
    LettuceClientConfiguration clientConfig =
            LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(5000))
                    .poolConfig(defaultPoolConfig).build();
    return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
}
 
Example #30
Source File: RedisConfiguration.java    From alcor with Apache License 2.0 5 votes vote down vote up
@Bean
LettuceConnectionFactory lettuceConnectionFactory() {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setHostName(redisHostName);
    configuration.setPort(redisHostPort);
    return new LettuceConnectionFactory(configuration);
}