Learn

Introducing Spring Data Redis

Brian Sam-Bodden
Author
Brian Sam-Bodden, Developer Advocate at Redis
@Bean
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
  RedisTemplate<?, ?> template = new RedisTemplate<>();
  template.setConnectionFactory(connectionFactory);

  return template;
}
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.host=localhost
spring.redis.port=6379
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
package com.redislabs.edu.redi2read.controllers;

public class HelloRedisController {

}
@RestController
@RequestMapping("/api/redis")
public class HelloRedisController {

}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/redis")
public class HelloRedisController {

  @Autowired
  private RedisTemplate<String, String> template;
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
private static final String STRING_KEY_PREFIX = "redi2read:strings:";
@PostMapping("/strings")
@ResponseStatus(HttpStatus.CREATED)
public Map.Entry<String, String> setString(@RequestBody Map.Entry<String, String> kvp) {
  return kvp;
}
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
./mvnw clean spring-boot:run
$ curl --location --request POST 'http://localhost:8080/api/redis/strings' \
  --header 'Content-Type: application/json' \
  --data-raw '{ "database:redis:creator": "Salvatore Sanfilippo" }'
{"database:redis:creator":"Salvatore Sanfilippo"}
@PostMapping("/strings")
@ResponseStatus(HttpStatus.CREATED)
public Map.Entry<String, String> setString(@RequestBody Map.Entry<String, String> kvp) {
  template.opsForValue().set(STRING_KEY_PREFIX + kvp.getKey(), kvp.getValue());

  return kvp;
}
$ redis-cli MONITOR
1617346602.221390 [0 172.19.0.1:58396] "SET" "redi2read:strings:database:redis:creator" "Salvatore Sanfilippo"
127.0.0.1:6379> KEYS *
1) "redi2read:strings:database:redis:creator"
127.0.0.1:6379> TYPE "redi2read:strings:database:redis:creator"
string
127.0.0.1:6379> GET "redi2read:strings:database:redis:creator"
"Salvatore Sanfilippo"
127.0.0.1:6379>
@GetMapping("/strings/{key}")
public Map.Entry<String, String> getString(@PathVariable("key") String key) {
  String value = template.opsForValue().get(STRING_KEY_PREFIX + key);

  if (value == null) {
    throw new ResponseStatusException(HttpStatus.NOT_FOUND, "key not found");
  }

  return new SimpleEntry<String, String>(key, value);
}
import java.util.AbstractMap.SimpleEntry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.server.ResponseStatusException;
$ curl --location --request GET 'http://localhost:8080/api/redis/strings/database:redis:creator'
{"database:redis:creator":"Salvatore Sanfilippo"}
1617347871.901585 [0 172.19.0.1:58284] "GET" "redi2read:strings:database:redis:creator"
{
  "timestamp": "2021-04-02T07:45:10.303+00:00",
  "status": 404,
  "error": "Not Found",
  "trace": "org.springframework.web.server.ResponseStatusException: 404...\n",
  "message": "key not found",
  "path": "/api/redis/strings/database:neo4j:creator"
}