Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 31 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Java Lint

on:
push:
branches:
- main
- release-*
pull_request:
branches:
- main
- release-*
workflow_dispatch:

jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: 17
distribution: "temurin"
cache: "maven"

- name: Run Spotless Check
run: ./mvnw spotless:check
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Minimal Spring Boot example that demonstrates OpenTelemetry integration
* with Valkey-GLIDE via {@link StringValkeyTemplate}.
* Minimal Spring Boot example that demonstrates OpenTelemetry integration with Valkey-GLIDE via
* {@link StringValkeyTemplate}.
*
* <p>This example exists to verify and showcase that Valkey commands executed
* through Spring Data Valkey automatically emit OpenTelemetry signals when
* OpenTelemetry is enabled via application properties.</p>
* <p>This example exists to verify and showcase that Valkey commands executed through Spring Data
* Valkey automatically emit OpenTelemetry signals when OpenTelemetry is enabled via application
* properties.
*
* <p>Telemetry is emitted while the application runs and Valkey commands are
* executed. Traces can be inspected via the configured OpenTelemetry backend,
* for example by viewing the collector logs:</p>
* <p>Telemetry is emitted while the application runs and Valkey commands are executed. Traces can
* be inspected via the configured OpenTelemetry backend, for example by viewing the collector logs:
*
* <pre>
* docker logs -f boot-telemetry-otel-collector-1
Expand All @@ -25,8 +24,7 @@
@SpringBootApplication
public class SpringBootTelemetryExample implements CommandLineRunner {

@Autowired
private StringValkeyTemplate valkeyTemplate;
@Autowired private StringValkeyTemplate valkeyTemplate;

public static void main(String[] args) {
SpringApplication.run(SpringBootTelemetryExample.class, args);
Expand Down
108 changes: 53 additions & 55 deletions examples/cache/src/main/java/example/cache/CacheExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,79 +18,77 @@
import io.valkey.springframework.data.valkey.cache.ValkeyCacheConfiguration;
import io.valkey.springframework.data.valkey.cache.ValkeyCacheManager;
import io.valkey.springframework.data.valkey.connection.valkeyglide.ValkeyGlideConnectionFactory;
import java.time.Duration;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.time.Duration;

/**
* Example demonstrating Spring Cache abstraction with Valkey.
*/
/** Example demonstrating Spring Cache abstraction with Valkey. */
public class CacheExample {

public static void main(String[] args) {
public static void main(String[] args) {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
UserService userService = context.getBean(UserService.class);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);
UserService userService = context.getBean(UserService.class);

System.out.println("First call (cache miss):");
long start = System.currentTimeMillis();
System.out.println(userService.getUserById("1"));
System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms");
System.out.println("First call (cache miss):");
long start = System.currentTimeMillis();
System.out.println(userService.getUserById("1"));
System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms");

System.out.println("\nSecond call (cache hit):");
start = System.currentTimeMillis();
System.out.println(userService.getUserById("1"));
System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms");
System.out.println("\nSecond call (cache hit):");
start = System.currentTimeMillis();
System.out.println(userService.getUserById("1"));
System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms");

// Cleanup
context.getBean(ValkeyCacheManager.class).getCacheNames()
.forEach(name -> context.getBean(ValkeyCacheManager.class).getCache(name).clear());
// Cleanup
context
.getBean(ValkeyCacheManager.class)
.getCacheNames()
.forEach(name -> context.getBean(ValkeyCacheManager.class).getCache(name).clear());

context.close();
}
context.close();
}

@Configuration
@EnableCaching
static class Config {
@Configuration
@EnableCaching
static class Config {

@Bean
public ValkeyGlideConnectionFactory connectionFactory() {
return new ValkeyGlideConnectionFactory();
}
@Bean
public ValkeyGlideConnectionFactory connectionFactory() {
return new ValkeyGlideConnectionFactory();
}

@Bean
public ValkeyCacheManager cacheManager(ValkeyGlideConnectionFactory connectionFactory) {
ValkeyCacheConfiguration config = ValkeyCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10));
return ValkeyCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.build();
}
@Bean
public ValkeyCacheManager cacheManager(ValkeyGlideConnectionFactory connectionFactory) {
ValkeyCacheConfiguration config =
ValkeyCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10));
return ValkeyCacheManager.builder(connectionFactory).cacheDefaults(config).build();
}

@Bean
public UserService userService() {
return new UserService();
}
}
@Bean
public UserService userService() {
return new UserService();
}
}

@Service
static class UserService {
@Service
static class UserService {

@Cacheable("users")
public String getUserById(String id) {
System.out.println("Fetching user from database...");
// Simulate slow database query
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return "User-" + id;
}
}
@Cacheable("users")
public String getUserById(String id) {
System.out.println("Fetching user from database...");
// Simulate slow database query
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return "User-" + id;
}
}
}
93 changes: 47 additions & 46 deletions examples/cluster/src/main/java/example/cluster/ClusterExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,65 @@
import io.valkey.springframework.data.valkey.connection.valkeyglide.ValkeyGlideClientConfiguration;
import io.valkey.springframework.data.valkey.connection.valkeyglide.ValkeyGlideConnectionFactory;
import io.valkey.springframework.data.valkey.core.StringValkeyTemplate;

import java.util.Arrays;
import java.util.List;

/**
* Example demonstrating Spring Data Valkey with a Valkey cluster.
*/
/** Example demonstrating Spring Data Valkey with a Valkey cluster. */
public class ClusterExample {

public static void main(String[] args) {
public static void main(String[] args) {

// Configure cluster nodes (matches Makefile cluster configuration)
ValkeyClusterConfiguration clusterConfig = new ValkeyClusterConfiguration();
List<ValkeyNode> nodes = Arrays.asList(
new ValkeyNode("127.0.0.1", 7379),
new ValkeyNode("127.0.0.1", 7380),
new ValkeyNode("127.0.0.1", 7381),
new ValkeyNode("127.0.0.1", 7382)
);
clusterConfig.setClusterNodes(nodes);
// Configure cluster nodes (matches Makefile cluster configuration)
ValkeyClusterConfiguration clusterConfig = new ValkeyClusterConfiguration();
List<ValkeyNode> nodes =
Arrays.asList(
new ValkeyNode("127.0.0.1", 7379),
new ValkeyNode("127.0.0.1", 7380),
new ValkeyNode("127.0.0.1", 7381),
new ValkeyNode("127.0.0.1", 7382));
clusterConfig.setClusterNodes(nodes);

ValkeyGlideConnectionFactory connectionFactory = new ValkeyGlideConnectionFactory(
clusterConfig, ValkeyGlideClientConfiguration.defaultConfiguration());
connectionFactory.afterPropertiesSet();
System.out.println("Connected to cluster with " + clusterConfig.getClusterNodes().size() + " nodes");
ValkeyGlideConnectionFactory connectionFactory =
new ValkeyGlideConnectionFactory(
clusterConfig, ValkeyGlideClientConfiguration.defaultConfiguration());
connectionFactory.afterPropertiesSet();
System.out.println(
"Connected to cluster with " + clusterConfig.getClusterNodes().size() + " nodes");

try {
StringValkeyTemplate template = new StringValkeyTemplate(connectionFactory);
try {
StringValkeyTemplate template = new StringValkeyTemplate(connectionFactory);

// Use hash tags to keep keys on same cluster node
String key1 = "{user:123}:profile";
String key2 = "{user:123}:settings";
String key3 = "{user:123}:session";
// Use hash tags to keep keys on same cluster node
String key1 = "{user:123}:profile";
String key2 = "{user:123}:settings";
String key3 = "{user:123}:session";

// String operations
template.opsForValue().set(key1, "John Doe");
template.opsForValue().set(key2, "theme=dark,lang=en");
template.opsForValue().set(key3, "active");
// String operations
template.opsForValue().set(key1, "John Doe");
template.opsForValue().set(key2, "theme=dark,lang=en");
template.opsForValue().set(key3, "active");

System.out.println("\nStored user data:");
System.out.println("Profile: " + template.opsForValue().get(key1));
System.out.println("Settings: " + template.opsForValue().get(key2));
System.out.println("Session: " + template.opsForValue().get(key3));
System.out.println("\nStored user data:");
System.out.println("Profile: " + template.opsForValue().get(key1));
System.out.println("Settings: " + template.opsForValue().get(key2));
System.out.println("Session: " + template.opsForValue().get(key3));

// Hash operations
String hashKey = "{user:123}:metadata";
template.opsForHash().put(hashKey, "created", "2025-01-01");
template.opsForHash().put(hashKey, "lastLogin", "2025-01-15");
template.opsForHash().put(hashKey, "loginCount", "42");
// Hash operations
String hashKey = "{user:123}:metadata";
template.opsForHash().put(hashKey, "created", "2025-01-01");
template.opsForHash().put(hashKey, "lastLogin", "2025-01-15");
template.opsForHash().put(hashKey, "loginCount", "42");

System.out.println("\nUser metadata:");
template.opsForHash().entries(hashKey).forEach((field, value) ->
System.out.println(field + ": " + value));
System.out.println("\nUser metadata:");
template
.opsForHash()
.entries(hashKey)
.forEach((field, value) -> System.out.println(field + ": " + value));

// Cleanup
template.delete(Arrays.asList(key1, key2, key3, hashKey));
} finally {
connectionFactory.destroy();
}
}
// Cleanup
template.delete(Arrays.asList(key1, key2, key3, hashKey));
} finally {
connectionFactory.destroy();
}
}
}
Loading