Spring Boot Redis
Get started
If you don’t have redis in your development environment, please check my other entry Redis
Start redis with command
sudo /etc/init.d/redis-server start
Dependency
To use redis in spring boot, you need to add starter-redis to build.gradle
build.gradle
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-redis")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Please update your dependencies.
Sample Program
The good sample is in git-hub.
Main.java
@SpringBootApplication
@ComponentScan(value="com.atmarkplant")
public class Main implements CommandLineRunner {
@Autowired
private StringRedisTemplate template;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Override
public void run(String... args) throws Exception {
ValueOperations<String, String> ops = this.template.opsForValue();
String key = "spring.boot.redis.sample.value";
if (!this.template.hasKey(key)) {
ops.set(key, "data");
}
System.out.println("Found key " + key + ", value=" + ops.get(key));
}
}
