Spring-Data-Redisのいくつかの比較について

9647 ワード

Spring Data redisのいくつかのオブジェクトのシーケンス化の比較Spring問題について
      spring data redis,     《Spring Data》    (         ,           ,   ),       ,   8    Spring data redis   。 
redis      list set hash        ,       POJO     ,                      。  ,Spring data      Serializer,    : 

JacksonJsonRedisSerializerJdkSerializationRedisSerializerOxmSerializer
以下を参照してください.http://static.springsource.org/spring-data/data-keyvalue/docs/1.0.x/api/
ここで、私は第一に三者の使用をテストしたいです.第二に、それらの使用効果を見たいです.
準備作業
ソースをダウンロードして私は直接《Spring Data》の本のソースコードの基礎の上で直して、ここから本のソースコードをダウンロードします:https://github.com/SpringSource/spring-data-book
redisサブプロジェクトを開くと、Mavenで組織されているので、パッケージの問題に関心を持つ必要はありません.
テストのEntityを追加
Redisを使用してPOJOを保存する対象をテストしたいのでcom.oreilly.springdata.redisパッケージの下にUserオブジェクトを作成する、Javaコードコレクションコードpackage com.oreilly.springdata.redis;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable;
/**
  • @author : stamen
  • @date: 13-7-16 */@XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "user") public class User implements Serializable { @XmlAttribute private String userName; @XmlAttribute private int age; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 以降,OXMおよびJacksonを用いてオブジェクトシーケンスを行う必要があり,オブジェクトのシーケンス化を制御するためにJSR 175の注釈をつけた.

  • ApplicationConfigの変更
    ApplicationConfigはSpringコンテナの構成クラスで、あなたの環境によって変更するには、私の変更は:Javaコードコレクションコードpackage com.oreilly.springdata.redis;
    import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.OxmSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; import org.springframework.oxm.jaxb.Jaxb2Marshaller;
    import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap;
    /**
  • @author Jon Brisbin */@Configuration public abstract class ApplicationConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory cf = new JedisConnectionFactory(); cf.setHostName("10.188.182.140"); cf.setPort(6379); cf.setPassword("superman"); cf.afterPropertiesSet(); return cf; } @Bean public RedisTemplate redisTemplate() { RedisTemplate rt = new RedisTemplate(); rt.setConnectionFactory(redisConnectionFactory()); return rt; } private static Map jaxbContextHashMap = new ConcurrentHashMap(); @Bean public OxmSerializer oxmSerializer()throws Throwable{Jaxb 2 marshaller jaxb 2 marshaller=new Jaxb 2 marshaller();Mapproperties=new HashMap();///Marshaller属性properties.put(Marshaller.JAXB_FORMAT_OUT、Boolean.TRUE);////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////,"utf-8");//配置xml自動インデント属性jaxb 2 marshaller.setClassesToBeBound(User.class);//マッピングされたxmlクラスはJAXB環境のjaxb 2 marshallerに入れる.setMarshallerProperties(properties);//Marshallerプロパティreturn new OxmSerializer(jaxb 2 marshaller,jaxb 2 marshaller);public static enum StringSerializer implements RedisSerializer { INSTANCE;
    @Override  
    public byte[] serialize(String s) throws SerializationException {  
        return (null != s ? s.getBytes() : new byte[0]);  
    }  
    
    @Override  
    public String deserialize(byte[] bytes) throws SerializationException {  
        if (bytes.length > 0) {  
            return new String(bytes);  
        } else {  
            return null;  
        }  
    }  
    } public static enum LongSerializer implements RedisSerializer { INSTANCE;
    @Override  
    public byte[] serialize(Long aLong) throws SerializationException {  
        if (null != aLong) {  
            return aLong.toString().getBytes();  
        } else {  
            return new byte[0];  
        }  
    }  
    
    @Override  
    public Long deserialize(byte[] bytes) throws SerializationException {  
        if (bytes.length > 0) {  
            return Long.parseLong(new String(bytes));  
        } else {  
            return null;  
        }  
    }  
    } public static enum IntSerializer implements RedisSerializer { INSTANCE;
    @Override  
    public byte[] serialize(Integer i) throws SerializationException {  
        if (null != i) {  
            return i.toString().getBytes();  
        } else {  
            return new byte[0];  
        }  
    }  
    
    @Override  
    public Integer deserialize(byte[] bytes) throws SerializationException {  
        if (bytes.length > 0) {  
            return Integer.parseInt(new String(bytes));  
        } else {  
            return null;  
        }  
    }  
    }

  • }
    1)redisConnectionFactory()Redsiサーバへの接続方法(Redisのインストール方法、参照:http://redis.io/download)2)oxmSerializer()は、Jaxb 2 marshallerベースのOxmSerializer Bean(後述)を定義するために追加されました.
    テストケースの作成
      KeyValueSerializersTest,                    : 

    JdkSerializationRedisSerializerを使用したシーケンス化
    Javaコードお気に入りコード@Test public void testJdkSerialiable(){RedisTemplate redis=new RedisTemplate();redis.setConnectionFactory(connectionFactory);redis.setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);redis.setValue Serializer(new J d k SerializationRedisrializer();redis.afterPropertiesSet();
    ValueOperations ops = redis.opsForValue();  
    
    User user1 = new User();  
    user1.setUserName("user1");  
    user1.setAge(20);  
    
    String key1 = "users/user1";  
    User user11 = null;  
    
    long begin = System.currentTimeMillis();  
    for (int i = 0; i < 100; i++) {  
        ops.set(key1,user1);  
        user11 = (User)ops.get(key1);  
    }  
    long time = System.currentTimeMillis() - begin;  
    System.out.println("jdk time:"+time);  
    assertThat(user11.getUserName(),is("user1"));  

    }
    JdkSerializationRedisSerializerは、Serializableを実装したすべてのクラスをシーケンス化することをサポートします.このテスト例を実行すると、redis-cliは「users/user 1」キーで対応する値を表示できます.内容は次のとおりです.参照
    redis 127.0.0.1:6379> get users/user1 "\xac\xed\x00\x05sr\x00!com.oreilly.springdata.redis.User\xb1\x1c\xcd\xed%\xd8\x02\x00\x02I\x00\x03ageL\x00\buserNamet\x00\x12Ljava/lang/String;xp\x00\x00\x00\x14t\x00\x05user1"
    strlenで対応する文字長を表示するには:参照
    redis 127.0.0.1:6379> strlen users/user1 (integer) 104
    上記のコードは100回のストレージと取得を行い、jdk time:266を参照するのに時間がかかります.
    JacksonJsonRedisSerializerを使用してシーケンス化
    Javaコードコレクションコード@Test public void testJacksonSerialiable(){RedisTemplate redis=new RedisTemplate();redis. setConnectionFactory(connectionFactory);redis. setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);redis. setValueSerializer(new JacksonJsonRedisSerializer(User.class);redis. afterPropertiesSet();redis. afterPropertiesSet();
    ValueOperations ops = redis.opsForValue();  
    
    User user1 = new User();  
    user1.setUserName("user1");  
    user1.setAge(20);  
    
    User user11 = null;  
    String key1 = "json/user1";  
    
    long begin = System.currentTimeMillis();  
    for (int i = 0; i < 100; i++) {  
        ops.set(key1,user1);  
        user11 = (User)ops.get(key1);  
    }  
    long time = System.currentTimeMillis() - begin;  
    
    System.out.println("json time:"+time);  
    assertThat(user11.getUserName(),is("user1"));  

    }
       ,  redis        : 

    参照
    redis 127.0.0.1:6379> get json/user1 "{\"userName\":\"user1\",\"age\":20}"redis 127.0.0.1:6379> strlen json/user1 (integer) 29

    参照
    json time:224 

    OxmSerialiableシーケンス化の使用
    Javaコードお気に入りコード@Test public void testOxmSerialiable()throws Throwable{RedisTemplate redis=new RedisTemplate()redis.setConnectionFactory(connectionFactory);redis.setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);
    redis.setValueSerializer(oxmSerializer);  
    redis.afterPropertiesSet();  
    
    ValueOperations ops = redis.opsForValue();  
    
    User user1 = new User();  
    user1.setUserName("user1");  
    user1.setAge(20);  
    
    User user11 = null;  
    String key1 = "oxm/user1";  
    
    long begin = System.currentTimeMillis();  
    for (int i = 0; i < 100; i++) {  
        ops.set(key1,user1);  
        user11 = (User)ops.get(key1);  
    }  
    long time = System.currentTimeMillis() - begin;  
    
    System.out.println("oxm time:"+time);  
    assertThat(user11.getUserName(),is("user1"));  

    }
       ,  redis        : 

    参照
    redis 127.0.0.1:6379> get oxm/user1 ""redis 127.0.0.1:6379> strlen oxm/user1 (integer) 90

    参照
    oxm time:335 

    小結
            ,JdkSerializationRedisSerializer     (   JDK   ),                。JSON           ,          ,         。 OxmSerialiabler         (        Marshaller  )。            JacksonJsonRedisSerializer  POJO    。 

    原文住所:http://stamen.iteye.com/blog/1907984