how we can configure spring boot application to store and retrieve java collections like lists and Date object in redis cache?

414 Views Asked by At

how we can configure spring boot application to store and retrieve java collections like lists and Date object in redis cache? Can anyone help me on this?

Redis configuration in spring boot to store and retrieve objects stored in redis cache that can have lists and Date objects as fields.

1

There are 1 best solutions below

0
On

A Redis hash is a data type that represents a mapping between a string field and a string value which you can convert as list of any type of data

@RedisHash("User")
@Getter
@Setter
public class Student implements Serializable {

    private String userId;
    private String name;
    private int age;
  }

and then call it on repository

User user = User().builder.id("testUser").name("testName").age(20).expiration(86400).build(); userRepository.save(user);

You can also easily do this with Redis based framework - Redisson. It offers wrapped collections like java.util.List, java.util.Set, java.util.Map and many more.

Config config = ...
RedissonClient redisson = Redisson.create(config);
List<String> list = redisson.getList("myList");
list.add("1");
list.add("2");
list.add("3");

list.remove("2")