store pojo

596 views
Skip to first unread message

manish kapur

unread,
Nov 16, 2011, 12:49:23 PM11/16/11
to jedis...@googlegroups.com
Whats the best way to store a pojo class object(as value) for a given key??

--
Regards,
MANISH KAPUR
B.E Computer Science(NSIT)
7838826181

Jonathan Leibiusky

unread,
Nov 16, 2011, 1:14:43 PM11/16/11
to jedis...@googlegroups.com
You can serialize the object using java object serializer or you can use a project that uses internally Jedis, called JOhm (https://github.com/xetorthio/johm) it is a little outdated, as soon as I release the new version of Jedis I will update JOhm with all the new features.

Jonathan

Jakub Białek

unread,
Nov 17, 2011, 3:24:03 AM11/17/11
to jedis...@googlegroups.com
You can also serialize object to json format which is more compact and faster than default java serialization and also human readable. I usually
use jackson library.

Best regards,
Ragnor

manish kapur

unread,
Nov 17, 2011, 6:02:12 AM11/17/11
to jedis...@googlegroups.com
Thanx guys.
but doing this will cost an extra overhead..isnt it?

Also i just learnt about redis template in spring.has anyone worked on that?

On 11/17/11, Jakub Białek <ragn...@gmail.com> wrote:
> You can also serialize object to json format which is more compact and
> faster than default java serialization and also human readable. I usually

> use jackson <http://jackson.codehaus.org/>library.

Jakub Białek

unread,
Nov 17, 2011, 6:42:36 AM11/17/11
to jedis...@googlegroups.com
Here is: http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking an interesting comparison of different serialization ways. If serialized object is send via network then size of serialized object is important.

I've used redis spring template and it's useful when redis holds only one type of data i.e. key-value pair of type String-String. In my cases usually redis holds mixed data even in one structure i.e. one hash contains integer key - string value (json serialized object) and string key - integer value (counter), then using redis spring template require defining and creating many templates beans, so I prefer to use jedis API directly.

Best regards,
Ragnor

manish kapur

unread,
Nov 17, 2011, 12:45:33 PM11/17/11
to jedis...@googlegroups.com
but there are java serializers for this

check this

4.6 Serializers

From the framework perspective, the data stored in Redis are just bytes. While Redis itself supports various types, for the most part these refer to the way the data is stored rather then what it represents. It is up to the user to decide whether the information gets translated into Strings or any other objects. The conversion between the user (custom) types and raw data (and vice-versa) is handled in Spring Redis Redis through the RedisSerializer interface (package org.springframework.data.redis.serializer) which as the name implies, takes care of the serialization process. Multiple implementations are available out of the box, two of which have been already mentioned before in this documentation: the StringRedisSerializer and the JdkSerializationRedisSerializer. However one can use OxmSerializer for Object/XML mapping through Spring 3 OXM support or JacksonJsonRedisSerializer for storing data in JSON format. Do note that the storage format is not limited only to values - it can be used for keys, values or hashes without any restrictions.


so i guess it can solve the problem..or am i thinking in wrong direction?


manish kapur

unread,
Nov 18, 2011, 1:19:39 AM11/18/11
to jedis...@googlegroups.com
hi jakub
can u share ur implementation of redis template,m finding some problems using it

On 11/17/11, manish kapur <mann...@gmail.com> wrote:
> but there are java serializers for this
>
> check this
> 4.6 Serializers
>
> From the framework perspective, the data stored in Redis are just bytes.
> While Redis itself supports various types, for the most part these refer to
> the way the data is stored rather then what it represents. It is up to the
> user to decide whether the information gets translated into Strings or any
> other objects. The conversion between the user (custom) types and raw data
> (and vice-versa) is handled in Spring Redis Redis through the
> RedisSerializer interface (package
> org.springframework.data.redis.serializer)
> which as the name implies, takes care of the serialization process.
> Multiple implementations are available out of the box, two of which have
> been already mentioned before in this documentation: the
> StringRedisSerializer and the JdkSerializationRedisSerializer. However one
> can use OxmSerializer for Object/XML mapping through Spring 3

> OXM<http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/oxm.html>support


> or
> JacksonJsonRedisSerializer for storing data in

> JSON<http://en.wikipedia.org/wiki/JSON>format. Do note that the


> storage format is not limited only to values - it
> can be used for keys, values or hashes without any restrictions.
>
>
> so i guess it can solve the problem..or am i thinking in wrong direction?
>

Jakub Białek

unread,
Nov 18, 2011, 3:55:10 AM11/18/11
to jedis...@googlegroups.com
Hi,

Below is code that I use to store Long values in redis using redis template.

// typed template
public class LongRedisTemplate extends RedisTemplate<String, Long> {

    public LongRedisTemplate(RedisConnectionFactory connectionFactory) {
        this.setConnectionFactory(connectionFactory);
        this.setKeySerializer(this.getStringSerializer());
        this.setHashKeySerializer(this.getStringSerializer());

        LongRedisSerializer longSerializer = new LongRedisSerializer();
        this.setHashValueSerializer(longSerializer);
        this.setValueSerializer(longSerializer);
    }
}

// serializer
public class LongRedisSerializer implements RedisSerializer<Long> {

    private static final String NULL = "null";

    private Charset charset = Charset.forName("UTF-8");

    @Override
    public byte[] serialize(Long t) throws SerializationException {
        String value = NULL;
        if (t != null) {
            value = t.toString();
        }
        return value.getBytes(charset);
    }

    @Override
    public Long deserialize(byte[] bytes) throws SerializationException {
        String value = new String(bytes, charset);
        if (value == null || NULL.equals(value)) {
            return null;
        }

        return Long.valueOf(value);
    }
}


//  beans definitions
    <!-- Jedis ConnectionFactory -->
    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" ref="redisHost" />
        <property name="port" ref="redisPort" />
    </bean>

    <bean id="dbLongRedisTemplate" class="...LongRedisTemplate">
        <constructor-arg ref="jedisConnectionFactory" />
        <qualifier value="database" />
    </bean>


with such definitions to store Long values use injected LongRedisTemplate object.

Best regards,
Ragnor

manish kapur

unread,
Nov 18, 2011, 7:44:42 AM11/18/11
to jedis...@googlegroups.com
Thanks Ragnor
that was really informative
In some of the examples on net i say this
Redis Template template...
template.set("hello","world");

but there isnt any set method for redis template.how do i do this?

Jakub Białek

unread,
Nov 18, 2011, 8:01:55 AM11/18/11
to jedis...@googlegroups.com
Read this documentation, there are two main ways to execute redis command. As you see in RedisTemplate javadoc you may:
  • template.boundValueOps("hello").set("world")
  • template.opsForValue().set("hello", "world")

The same convention for set, zset, list and hashes.

manish kapur

unread,
Nov 18, 2011, 9:19:11 AM11/18/11
to jedis...@googlegroups.com
@Jakub

that solved my problem..:)
thanx a lot..

On 11/18/11, Jakub Białek <ragn...@gmail.com> wrote:

> Read this
> documentation<http://static.springsource.org/spring-data/data-redis/docs/current/reference/redis.html#redis:template>,


> there are two main ways to execute redis command. As you see in

> RedisTemplate<http://static.springsource.org/spring-data/data-redis/docs/current/api/>javadoc
> you may:
>
> - template.boundValueOps("hello").set("world")
> - template.opsForValue().set("hello", "world")

Reply all
Reply to author
Forward
0 new messages