2

I am trying to do this.

I have a list of objects (Custom objects), I want to save them all in a single register in Redis, is it possible to save them as ajax somehow? I was reading about Jackson but I couldn't figure it how.

So far I only have this

@Autowired
private StringRedisTemplate redisTmpl;

And I can save like this

redisTmpl.opsForValue().set("foo", "bar");

Works pretty good, but instead of Bar , I want to save my list of objects (using this StringRedisTemplate.

Any idea how to do it?

Or maybe using another way? But I need to save all the list in just one key.

Thanks

3
  • I'm not a Java dev, but you need to serialize that list into JSON and store the JSON string in the whole key........ Commented Dec 15, 2015 at 8:48
  • Check out the Spring Data Redis docs, you can use different serializers: docs.spring.io/spring-data/redis/docs/current/reference/html/… Commented Dec 15, 2015 at 10:51
  • ajax is negotiation protocol not serialization method, I believe you mean JSON instead. Commented Dec 15, 2015 at 20:09

5 Answers 5

5

I Found the way to do it....

To save all the list you can use jackson, on this way

ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(myList);

And later you just save it on same way

redisTmpl.opsForValue().set("foo", jsonInString);
Sign up to request clarification or add additional context in comments.

1 Comment

how can i convert the jsonInString object back into an object. Any suggestions ?
0

If you can serialize your object as JSON, you can store it as string in redis. but for this you may need to create serializer/deserializer methods for your class.

1 Comment

I am using Jackson for it, and it saves it great, but I cannot get it back in same format (hashmap)
0

you should use spring support to convert your values to JSON like below example. It also convert your key to string directly.

<bean id="serializer" class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
    <constructor-arg>
        <value type="java.lang.Class">your.class.path.to.be.saved.in.redis</value>
    </constructor-arg>
</bean>

<bean id="template" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="redisConnectionFactory">
    <property name="valueSerializer" ref="serializer"/>
    <property name="keySerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    </property>
   </bean>

1 Comment

@jpganz18 you can use the internal support from spring rides data to convert your object into json format by using serializers as mentioned in the answer... if you find this helpful please vote up.
0

You can try Redisson as well. It supports many codecs like Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization. It's very easy in use:

List<Object> list = ...
redisson.getBucket("yourKey").set(list);

// or use Redis list object

redisson.getList("yourKey").addAll(list);

Comments

0

Using redisson you can do this:

ObjectMapper mapper = new ObjectMapper();
RList<MyObject> list = redissonCLient.getList("myKey");
list.add(mapper.writeValueAsString(new MyObject("test")));

The code above will save a Redis List in JSON format with the MyObjects values.

To retrieve the list you can do:

RList<MyObject> list = redissonCLient.getList("myKey");
List<MyObject> myObjects = mapper.readValue(list.toString(), new TypeReference<List<MyObject>>(){});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.