Skip to content

Commit

Permalink
refactor!: Renamed spring module and added sample demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Aug 1, 2024
1 parent e281bde commit 3095e9c
Show file tree
Hide file tree
Showing 221 changed files with 95 additions and 28 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_description = Spring LettuceMod
project_description = LettuceMod Spring

sourceCompatibility = 17
targetCompatibility = 17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@ dependencies {
api project(':lettucemod')
api 'org.apache.commons:commons-pool2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

eclipse {
project {
name = 'lettucemod-spring'
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.redis.spring.lettucemod;
package com.redis.lettucemod.spring;

import java.time.Duration;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster.Refresh;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.redis.lettucemod.RedisModulesClient;
Expand All @@ -31,7 +32,7 @@
import io.lettuce.core.support.ConnectionPoolSupport;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({ RedisProperties.class })
@EnableConfigurationProperties(RedisProperties.class)
public class RedisModulesAutoConfiguration {

@Bean
Expand Down Expand Up @@ -70,13 +71,10 @@ ClientResources clientResources() {
return DefaultClientResources.create();
}

@Bean(destroyMethod = "shutdown")
AbstractRedisClient client(RedisURI redisURI, RedisProperties properties, ClientResources clientResources) {
if (properties.getCluster() == null || CollectionUtils.isEmpty(properties.getCluster().getNodes())) {
RedisModulesClient client = RedisModulesClient.create(clientResources, redisURI);
client.setOptions(clientOptions(ClientOptions.builder(), properties).build());
return client;
}
@Bean(destroyMethod = "shutdown", name = "redisModulesClusterClient")
@ConditionalOnProperty(name = "spring.data.redis.cluster.nodes[0]")
RedisModulesClusterClient clusterClient(RedisURI redisURI, RedisProperties properties,
ClientResources clientResources) {
RedisModulesClusterClient client = RedisModulesClusterClient.create(clientResources, redisURI);
ClusterClientOptions.Builder builder = ClusterClientOptions.builder();
Refresh refreshProperties = properties.getLettuce().getCluster().getRefresh();
Expand All @@ -93,6 +91,15 @@ AbstractRedisClient client(RedisURI redisURI, RedisProperties properties, Client
return client;
}

@Bean(destroyMethod = "shutdown", name = "redisModulesClient")
@ConditionalOnMissingBean(name = "redisModulesClusterClient")
RedisModulesClient redisModulesClient(RedisURI redisURI, RedisProperties properties,
ClientResources clientResources) {
RedisModulesClient client = RedisModulesClient.create(clientResources, redisURI);
client.setOptions(clientOptions(ClientOptions.builder(), properties).build());
return client;
}

@Bean(name = "redisConnection", destroyMethod = "close")
@ConditionalOnBean(AbstractRedisClient.class)
StatefulRedisModulesConnection<String, String> redisConnection(AbstractRedisClient client) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
com.redis.spring.lettucemod.RedisModulesAutoConfiguration
com.redis.lettucemod.spring.RedisModulesAutoConfiguration
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
import org.testcontainers.junit.jupiter.Testcontainers;

import com.redis.lettucemod.RedisModulesClient;
import com.redis.lettucemod.RedisModulesUtils;
import com.redis.lettucemod.api.StatefulRedisModulesConnection;
import com.redis.lettucemod.cluster.RedisModulesClusterClient;
import com.redis.lettucemod.search.Suggestion;
import com.redis.lettucemod.spring.RedisModulesAutoConfiguration;
import com.redis.testcontainers.RedisStackContainer;

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.resource.ClientResources;

/**
Expand All @@ -37,12 +41,17 @@ class AutoConfigurationTests {
@Test
void defaultConfiguration() {
this.contextRunner.run((context) -> {
assertThat(context.getBean("client")).isInstanceOf(RedisModulesClient.class);
assertThat(context).hasSingleBean(RedisModulesClient.class);
assertThat(context).hasSingleBean(AbstractRedisClient.class);
if (context.containsBean("redisModulesClient")) {
assertThat(context.getBean("redisModulesClient")).isInstanceOf(RedisModulesClient.class);
}
if (context.containsBean("redisModulesClusterClient")) {
assertThat(context.getBean("redisModulesClusterClient")).isInstanceOf(RedisModulesClusterClient.class);
}
assertThat(context).hasSingleBean(StatefulRedisModulesConnection.class);
assertThat(context).hasSingleBean(ClientResources.class);
RedisModulesClient client = context.getBean(RedisModulesClient.class);
StatefulRedisModulesConnection<String, String> connection = client.connect();
AbstractRedisClient client = context.getBean(AbstractRedisClient.class);
StatefulRedisModulesConnection<String, String> connection = RedisModulesUtils.connection(client);
String key = "suggestIdx";
connection.sync().ftSugadd(key, Suggestion.of("rome", 1));
connection.sync().ftSugadd(key, Suggestion.of("romarin", 1));
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions samples/lettucemod-spring-demo/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
project_description = Spring LettuceMod Demo

sourceCompatibility = 17
targetCompatibility = 17
testSourceCompatibility = 17
testTargetCompatibility = 17
4 changes: 4 additions & 0 deletions samples/lettucemod-spring-demo/lettucemod-spring-demo.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation project(':lettucemod-spring')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.redis.spring.lettucemod.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LettucemodSpringDemoApplication {

public static void main(String[] args) {
SpringApplication.run(LettucemodSpringDemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.redis.spring.lettucemod.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.redis.lettucemod.RedisModulesUtils;
import com.redis.lettucemod.api.StatefulRedisModulesConnection;

import io.lettuce.core.AbstractRedisClient;

@Component
public class RedisService implements InitializingBean {

private final Logger log = LoggerFactory.getLogger(getClass());

private final AbstractRedisClient client;

public RedisService(AbstractRedisClient client) {
this.client = client;
}

@Override
public void afterPropertiesSet() throws Exception {
log.info("Pinging Redis");
StatefulRedisModulesConnection<String, String> connection = RedisModulesUtils.connection(client);
String reply = connection.sync().ping();
if ("PONG".equalsIgnoreCase(reply)) {
log.info("Successfully pinged Redis (response: {})", reply);
} else {
log.error("Failed to ping Redis (response: {})", reply);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=lettucemod-spring-demo
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ apply plugin: 'org.kordamp.gradle.kordamp-parentbuild'
rootProject.name = 'lettucemod'

projects {
directories = ['subprojects']
directories = ['core', 'samples']

plugins {
all {
Expand All @@ -27,7 +27,7 @@ projects {
path(':') {
id 'org.kordamp.gradle.java-project'
}
dirs(['subprojects']) {
dirs(['core', 'samples']) {
id 'java-library'
id 'org.springframework.boot'
id 'io.spring.dependency-management'
Expand Down

This file was deleted.

0 comments on commit 3095e9c

Please sign in to comment.