Home > AI > Backend > SpringBoot > Redis >

Access data with Redis

Step 1: setup Redis Server

$ brew install redis
$ redis-server

Step 2: setup SpringBoot Redis Client

Install the dependency

<dependency>
    <groupId>org.springframework.data</groupId>. 
    <artifactId>spring-data-redis</artifactId>. 
    <version>2.3.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>

Codes:

RedisConfig.java

@Configuration
@ComponentScan("com.example.demo.redis")
@EnableRedisRepositories(basePackages = "com.example.demo.redis")
@PropertySource("classpath:application.properties")
public class RedisConfig {

    /**
     * Use the Jedis Redis Client to connect with Redis Server
     * @return
     */
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }


    /**
     * For querying data with a custom repository.
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        return template;
    }
}

Student.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Student")
public class Student implements Serializable {

    public enum Gender {
        MALE, FEMALE
    }

    private String id;
    private String name;
    private Gender gender;
    private int grade;
}

StudentRepository.java

@Repository
public interface StudentRepository extends CrudRepository<Student, String> {
}

StudentController.java

@RestController
public class StudentController {

    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/saveOneStudent")
    public void saveOneStudent() {
        Student student = new Student(
                "Eng2015001", "John Doe", Student.Gender.MALE, 1);
        studentRepository.save(student);
    }

    @GetMapping("/getOneStudent")
    public Student getOneStudent() {
        Student retrievedStudent =
                studentRepository.findById("Eng2015001").get();
        return retrievedStudent;
    }

    @GetMapping("/updateOneStudent")
    public Student updateOneStudent() {
        Student retrievedStudent =
                studentRepository.findById("Eng2015001").get();
        retrievedStudent.setName("Richard Watson");
        studentRepository.save(retrievedStudent);
        return retrievedStudent;
    }

    @GetMapping("/deleteOneStudent")
    public void deleteOneStudent() {
        studentRepository.deleteById("Eng2015001");
    }

    @GetMapping("/findAllStudents")
    public List<Student> findAllStudents() {
        Student engStudent = new Student(
                "Eng2015001", "John Doe", Student.Gender.MALE, 1);
        Student medStudent = new Student(
                "Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
        studentRepository.save(engStudent);
        studentRepository.save(medStudent);

        List<Student> students = new ArrayList<>();
        studentRepository.findAll().forEach(students::add);
        return students;
    }
}

Test with Postman

GET http://localhost:8084/findAllStudents

Test with redis-cli

$ redis-cli
$ HGETALL Student:Eng2015001

Leave a Reply