Getting Started
An easy way to bootstrap setting up a working environment is to create a Spring-based project via start.spring.io or create a Spring project in Spring Tools.
Examples Repository
Section titled “Examples Repository”The GitHub spring-data-valkey repository hosts several examples that you can download and play around with to get a feel for how the library works.
Hello World
Section titled “Hello World”First, you need to set up a running Valkey server. Spring Data Valkey requires Valkey 2.6 or above and Spring Data Valkey integrates with Valkey GLIDE, Lettuce and Jedis, popular open-source Java libraries for Valkey.
Now you can create a simple Java application that stores and reads a value to and from Valkey.
Create the main application to run, as the following example shows:
import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import io.valkey.springframework.data.connection.ValkeyConnectionFactory;import io.valkey.springframework.data.connection.glide.ValkeyGlideConnectionFactory;import io.valkey.springframework.data.core.ValkeyTemplate;
public class ValkeyApplication {
private static final Log LOG = LogFactory.getLog(ValkeyApplication.class);
public static void main(String[] args) {
ValkeyConnectionFactory factory = new ValkeyGlideConnectionFactory();
ValkeyTemplate<String, String> template = new ValkeyTemplate<>(); template.setConnectionFactory(factory); template.afterPropertiesSet();
template.opsForValue().set("foo", "bar");
LOG.info("Value at foo:" + template.opsForValue().get("foo"));
template.getConnectionFactory().getConnection().close(); }}Even in this simple example, there are a few notable things to point out:
- You can create an instance of
io.valkey.springframework.data.core.ValkeyTemplatewith aio.valkey.springframework.data.connection.ValkeyConnectionFactory. Connection factories are an abstraction on top of the supported drivers. - For reactive programming with
ReactiveValkeyTemplate, only Lettuce is supported. - There’s no single way to use Valkey as it comes with support for a wide range of data structures such as plain keys (“strings”), lists, sets, sorted sets, streams, hashes and so on.