Java Code Examples for org.springframework.data.redis.core.ReactiveRedisTemplate

The following examples show how to use org.springframework.data.redis.core.ReactiveRedisTemplate. These examples are extracted from open source projects. 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
@Bean
public ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(
    ReactiveRedisConnectionFactory reactiveRedisConnectionFactory, ResourceLoader resourceLoader) {

    FstSerializationRedisSerializer serializer = new FstSerializationRedisSerializer(() -> {
        FSTConfiguration configuration = FSTConfiguration.createDefaultConfiguration()
            .setForceSerializable(true);
        configuration.setClassLoader(resourceLoader.getClassLoader());
        return configuration;
    });
    @SuppressWarnings("all")
    RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext
        .newSerializationContext()
        .key((RedisSerializer)new StringRedisSerializer())
        .value(serializer)
        .hashKey(StringRedisSerializer.UTF_8)
        .hashValue(serializer)
        .build();

    return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
 
Example 2
Source Project: soul   Source File: RateLimiterPluginDataHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handlerPlugin(final PluginData pluginData) {
    if (Objects.nonNull(pluginData) && pluginData.getEnabled()) {
        //init redis
        RateLimiterConfig rateLimiterConfig = GsonUtils.getInstance().fromJson(pluginData.getConfig(), RateLimiterConfig.class);
        //spring data redisTemplate
        if (Objects.isNull(Singleton.INST.get(ReactiveRedisTemplate.class))
                || Objects.isNull(Singleton.INST.get(RateLimiterConfig.class))
                || !rateLimiterConfig.equals(Singleton.INST.get(RateLimiterConfig.class))) {
            LettuceConnectionFactory lettuceConnectionFactory = createLettuceConnectionFactory(rateLimiterConfig);
            lettuceConnectionFactory.afterPropertiesSet();
            RedisSerializer<String> serializer = new StringRedisSerializer();
            RedisSerializationContext<String, String> serializationContext =
                    RedisSerializationContext.<String, String>newSerializationContext().key(serializer).value(serializer).hashKey(serializer).hashValue(serializer).build();
            ReactiveRedisTemplate<String, String> reactiveRedisTemplate = new ReactiveRedisTemplate<>(lettuceConnectionFactory, serializationContext);
            Singleton.INST.single(ReactiveRedisTemplate.class, reactiveRedisTemplate);
            Singleton.INST.single(RateLimiterConfig.class, rateLimiterConfig);
        }
    }
}
 
Example 3
Source Project: soul   Source File: RedisRateLimiter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This uses a basic token bucket algorithm and relies on the fact that Redis scripts
 * execute atomically. No other operations can run between fetching the count and
 * writing the new count.
 *
 * @param id            is rule id
 * @param replenishRate replenishRate
 * @param burstCapacity burstCapacity
 * @return {@code Mono<Response>} to indicate when request processing is complete
 */
@SuppressWarnings("unchecked")
public Mono<RateLimiterResponse> isAllowed(final String id, final double replenishRate, final double burstCapacity) {
    if (!this.initialized.get()) {
        throw new IllegalStateException("RedisRateLimiter is not initialized");
    }
    List<String> keys = getKeys(id);
    List<String> scriptArgs = Arrays.asList(replenishRate + "", burstCapacity + "", Instant.now().getEpochSecond() + "", "1");
    Flux<List<Long>> resultFlux = Singleton.INST.get(ReactiveRedisTemplate.class).execute(this.script, keys, scriptArgs);
    return resultFlux.onErrorResume(throwable -> Flux.just(Arrays.asList(1L, -1L)))
            .reduce(new ArrayList<Long>(), (longs, l) -> {
                longs.addAll(l);
                return longs;
            }).map(results -> {
                boolean allowed = results.get(0) == 1L;
                Long tokensLeft = results.get(1);
                RateLimiterResponse rateLimiterResponse = new RateLimiterResponse(allowed, tokensLeft);
                log.info("RateLimiter response:{}", rateLimiterResponse.toString());
                return rateLimiterResponse;
            }).doOnError(throwable -> {
                log.error("Error determining if user allowed from redis:{}", throwable.getMessage());
            });
}
 
Example 4
Source Project: spring-session   Source File: IndexDocTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unused")
void newReactiveRedisSessionRepository() {
	LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
	RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
			.<String, Object>newSerializationContext(new JdkSerializationRedisSerializer()).build();

	// tag::new-reactiveredissessionrepository[]
	// ... create and configure connectionFactory and serializationContext ...

	ReactiveRedisTemplate<String, Object> redisTemplate = new ReactiveRedisTemplate<>(connectionFactory,
			serializationContext);

	ReactiveSessionRepository<? extends Session> repository = new ReactiveRedisSessionRepository(redisTemplate);
	// end::new-reactiveredissessionrepository[]
}
 
Example 5
Source Project: Sentinel-Dashboard-Nacos   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory){
    RedisSerializationContext<String, String> serializationContext = RedisSerializationContext
        .<String, String>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new StringRedisSerializer())
        .build();
    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example 6
public RateCheckRedisRateLimiter(ReactiveRedisTemplate<String, String> redisTemplate,
                                 RedisScript<List<Long>> script, Validator validator) {
    super(Config.class, CONFIGURATION_PROPERTY_NAME, validator);
    this.redisTemplate = redisTemplate;
    this.script = script;
    initialized.compareAndSet(false, true);
}
 
Example 7
@Override
@SuppressWarnings("unchecked")
public void setApplicationContext(ApplicationContext context) throws BeansException {
    if (initialized.compareAndSet(false, true)) {
        this.redisTemplate = context.getBean("stringReactiveRedisTemplate", ReactiveRedisTemplate.class);
        this.script = context.getBean(REDIS_SCRIPT_NAME, RedisScript.class);
        if (context.getBeanNamesForType(Validator.class).length > 0) {
            this.setValidator(context.getBean(Validator.class));
        }
    }
}
 
Example 8
Source Project: spring-fu   Source File: RedisReactiveInitializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
  public void initialize(GenericApplicationContext context) {
RedisReactiveAutoConfiguration redisAutoConfiguration = new RedisReactiveAutoConfiguration();
context.registerBean("reactiveRedisTemplate", ReactiveRedisTemplate.class,
		() -> redisAutoConfiguration.reactiveRedisTemplate(context.getBean(ReactiveRedisConnectionFactory.class), context),
		(definition) -> ((RootBeanDefinition) definition).setTargetType(ResolvableType.forClassWithGenerics(ReactiveRedisTemplate.class, Object.class, Object.class)));
context.registerBean("reactiveStringRedisTemplate", ReactiveStringRedisTemplate.class,
		() -> redisAutoConfiguration.reactiveStringRedisTemplate(context.getBean(ReactiveRedisConnectionFactory.class)));
  }
 
Example 9
Source Project: Sentinel   Source File: RedisConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> stringReactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory){
    RedisSerializationContext<String, String> serializationContext = RedisSerializationContext
        .<String, String>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new StringRedisSerializer())
        .build();
    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example 10
@Bean
public ReactiveRedisTemplate<String, Post> reactiveJsonPostRedisTemplate(
    ReactiveRedisConnectionFactory connectionFactory) {

    RedisSerializationContext<String, Post> serializationContext = RedisSerializationContext
        .<String, Post>newSerializationContext(new StringRedisSerializer())
        .hashKey(new StringRedisSerializer())
        .hashValue(new Jackson2JsonRedisSerializer<>(Post.class))
        .build();


    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example 11
Source Project: POC   Source File: RedisCachingConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Book> reactiveJsonBookRedisTemplate(
		ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {

	RedisSerializationContext<String, Book> serializationContext = RedisSerializationContext
			.<String, Book>newSerializationContext(new StringRedisSerializer()).hashKey(new StringRedisSerializer())
			.hashValue(new Jackson2JsonRedisSerializer<>(Book.class)).build();

	return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
 
Example 12
/**
 * Configures a {@link ReactiveRedisTemplate} with {@link String} keys and a typed
 * {@link Jackson2JsonRedisSerializer}.
 */
@Bean
public ReactiveRedisTemplate<String, Person> reactiveJsonPersonRedisTemplate(
		ReactiveRedisConnectionFactory connectionFactory) {

	Jackson2JsonRedisSerializer<Person> serializer = new Jackson2JsonRedisSerializer<>(Person.class);
	RedisSerializationContextBuilder<String, Person> builder = RedisSerializationContext
			.newSerializationContext(new StringRedisSerializer());

	RedisSerializationContext<String, Person> serializationContext = builder.value(serializer).build();

	return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example 13
/**
 * Configures a {@link ReactiveRedisTemplate} with {@link String} keys and {@link GenericJackson2JsonRedisSerializer}.
 */
@Bean
public ReactiveRedisTemplate<String, Object> reactiveJsonObjectRedisTemplate(
		ReactiveRedisConnectionFactory connectionFactory) {

	RedisSerializationContextBuilder<String, Object> builder = RedisSerializationContext
			.newSerializationContext(new StringRedisSerializer());

	RedisSerializationContext<String, Object> serializationContext = builder
			.value(new GenericJackson2JsonRedisSerializer("_type")).build();

	return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}
 
Example 14
private ReactiveRedisTemplate<String, Object> createReactiveRedisTemplate() {
	RedisSerializer<String> keySerializer = new StringRedisSerializer();
	RedisSerializer<Object> defaultSerializer = (this.defaultRedisSerializer != null) ? this.defaultRedisSerializer
			: new JdkSerializationRedisSerializer(this.classLoader);
	RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
			.<String, Object>newSerializationContext(defaultSerializer).key(keySerializer).hashKey(keySerializer)
			.build();
	return new ReactiveRedisTemplate<>(this.redisConnectionFactory, serializationContext);
}
 
Example 15
Source Project: tutorials   Source File: RedisConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, Employee> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class);
    RedisSerializationContext.RedisSerializationContextBuilder<String, Employee> builder = RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
    RedisSerializationContext<String, Employee> context = builder.value(serializer)
        .build();
    return new ReactiveRedisTemplate<>(factory, context);
}
 
Example 16
Source Project: jetlinks-community   Source File: JetLinksConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(initMethod = "startup")
public RedisClusterManager clusterManager(JetLinksProperties properties, ReactiveRedisTemplate<Object, Object> template) {
    return new RedisClusterManager(properties.getClusterName(), properties.getServerId(), template);
}
 
Example 17
@Bean
public ReactiveRedisTemplate<String,String> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory){
  return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
}
 
Example 18
public FareRepository(ReactiveRedisTemplate<String, String> reactiveRedisTemplate,
    ObjectMapper mapper) {
  this.reactiveRedisTemplate = reactiveRedisTemplate;
  this.mapper = mapper;
}
 
Example 19
@Bean
public ReactiveRedisTemplate<String,String> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory){
  return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
}
 
Example 20
public FareRepository(ReactiveRedisTemplate<String, String> reactiveRedisTemplate,
    ObjectMapper mapper) {
  this.reactiveRedisTemplate = reactiveRedisTemplate;
  this.mapper = mapper;
}
 
Example 21
Source Project: Spring-Boot-2.0-Projects   Source File: TaxiService.java    License: MIT License 4 votes vote down vote up
public TaxiService(ReactiveRedisTemplate<String, String> reactiveRedisTemplate, TaxiRepository taxiRepository) {
    this.reactiveRedisTemplate = reactiveRedisTemplate;
    this.taxiRepository = taxiRepository;
}
 
Example 22
Source Project: Spring-Boot-2.0-Projects   Source File: RedisConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) {
    return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
}
 
Example 23
Source Project: Spring-Boot-2.0-Projects   Source File: TaxiBookingService.java    License: MIT License 4 votes vote down vote up
public TaxiBookingService(RedisTemplate<String, String> redisTemplate, ReactiveRedisTemplate<String, String> reactiveRedisTemplate, TaxiBookingRepository taxiBookingRepository) {
    this.redisTemplate = redisTemplate;
    this.reactiveRedisTemplate = reactiveRedisTemplate;
    this.taxiBookingRepository = taxiBookingRepository;
}
 
Example 24
public EmojiRepository(ReactiveRedisTemplate<String, String> redisTemplate) {
    this.redisTemplate = redisTemplate;
}
 
Example 25
Source Project: JetfireCloud   Source File: DefaultRedisRateLimiter.java    License: Apache License 2.0 4 votes vote down vote up
public DefaultRedisRateLimiter(ReactiveRedisTemplate<String, String> redisTemplate, RedisScript<List<Long>> script, Validator validator) {
    super(redisTemplate, script, validator);
}
 
Example 26
Source Project: Spring-5.0-By-Example   Source File: RedisConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String,String> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory){
  return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
}
 
Example 27
Source Project: Spring-5.0-By-Example   Source File: FareRepository.java    License: MIT License 4 votes vote down vote up
public FareRepository(ReactiveRedisTemplate<String, String> reactiveRedisTemplate,
    ObjectMapper mapper) {
  this.reactiveRedisTemplate = reactiveRedisTemplate;
  this.mapper = mapper;
}
 
Example 28
Source Project: Spring-5.0-By-Example   Source File: RedisConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public ReactiveRedisTemplate<String,String> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory){
  return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
}
 
Example 29
Source Project: Spring-5.0-By-Example   Source File: FareRepository.java    License: MIT License 4 votes vote down vote up
public FareRepository(ReactiveRedisTemplate<String, String> reactiveRedisTemplate,
    ObjectMapper mapper) {
  this.reactiveRedisTemplate = reactiveRedisTemplate;
  this.mapper = mapper;
}
 
Example 30
Source Project: SpringCloud   Source File: DefaultRedisRateLimiter.java    License: Apache License 2.0 4 votes vote down vote up
public DefaultRedisRateLimiter(ReactiveRedisTemplate<String, String> redisTemplate,
                               RedisScript<List<Long>> script,
                               @Qualifier("defaultValidator") Validator validator) {
    super(redisTemplate, script, validator);
}