Spring-data-redis--Simple use of Spring-data-redis

Source: Internet
Author: User
Tags redis server

Spring-data-redis Serialization Policy

Spring-data-redis offers a variety of serializer strategies that are very handy for developers using Jedis. SDR offers 4 built-in serializer:

    • Jdkserializationredisserializer: Using the JDK serialization means (serializable interface, Objectinputstrean,objectoutputstream), the data is stored in a byte stream, JDK serialization and deserialization of data
    • Stringredisserializer: String encoding, data is stored as String
    • Jacksonjsonredisserializer:json Format Storage
    • Oxmserializer:xml Format Storage

Among them, Jdkserializationredisserializer and Stringredisserializer are the most basic serialization strategies, where "Jacksonjsonredisserializer" and " Oxmserializer "are all based on stirng storage, so they are more" advanced "serialization (eventually using string parsing and building Java objects).

The redistemplate needs to declare 4 kinds of serializer, the default is "Jdkserializationredisserializer":

1) Keyserializer: For normal k-v operation, Key takes a serialization strategy
2) serialization strategy taken by Valueserializer:value
3) Hashkeyserializer: In the hash data structure, Hash-key's serialization strategy
4) serialization strategy for Hashvalueserializer:hash-value

In any case, Key/hashkey is recommended to use Stringredisserializer. This Redis server uses the command to see the configuration as follows

1. Maven Configuration

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>< Spring.version>4.1.1.release</spring.version></properties><dependencies><dependency ><groupid>org.springframework</groupid><artifactid>spring-context-support</artifactid ><version>${spring.version}</version></dependency><dependency><groupId> Org.springframework.data</groupid><artifactid>spring-data-redis</artifactid><version> 1.4.1.release</version></dependency><dependency><groupid>redis.clients</groupid> <artifactId>jedis</artifactId><version>2.6.0</version><type>jar</type>< scope>compile</scope></dependency><dependency><groupid>junit</groupid>< artifactid>junit</artifactid><version>4.11</version><scope>test</scope></ Dependency></dependencies> 

2. Spring XML configuration file

Configuring the Jedis buffer pool

<!--Configure Jedis buffer pool--><bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" p:maxtotal= "32" p:maxidle= "6" p:maxwaitmillis= "15000" ></bean>
Configure ConnectionFactory
<!--configuration Jedis connection--><bean id= "ConnectionFactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "p:poolconfig-ref=" Jedispoolconfig "P: Hostname= "127.0.0.1" p:port= "6379" p:usepool= "true" ></bean>
Create Redistemplate
<!--configuration redistemplate utilize Stringserializer--><bean id= "redistemplate" class= " Org.springframework.data.redis.core.RedisTemplate "p:connectionfactory-ref=" ConnectionFactory "><property Name= "Defaultserializer" ><bean class= "Org.springframework.data.redis.serializer.StringRedisSerializer"/ ></property><!--Use string is mostly key on the Redis side with the command to read otherwise the default serialization is not able to read--><property name= "Keyserializer" > <bean class= "Org.springframework.data.redis.serializer.StringRedisSerializer"/></property>< Property Name= "Hashkeyserializer" ><bean class= " Org.springframework.data.redis.serializer.StringRedisSerializer "/></PROPERTY></BEAN> 
3. Case
private static ApplicationContext context = Null;private static Redistemplate redistemplate=null;@ Org.junit.BeforeClasspublic static void Beforeclass () {if (context = = null) {System.out.println ("ctx inited ..."); context = new Classpathxmlapplicationcontext ("Spring-context.xml"), Redistemplate = Context.getbean ("RedisTemplate", Redistemplate.class);}}

1.String

@Testpublic void TestString () {valueoperations opsforvalue = Redistemplate.opsforvalue (); Opsforvalue.set ("String: Name "," Achuan "); Opsforvalue.set (" String:id "," 1 "); Object name = Opsforvalue.get (" String:name "); Assertnotnull (name) ; Assertequals ("Achuan", name.tostring ());}

2.List

When it comes to list Redis, which provides an operation similar to Message Queuing,

        @Testpublic void Testlist () {listoperations opsforlist = redistemplate.opsforlist (); String keyName = "ListKey"; Redistemplate.delete (KeyName); Opsforlist.leftpush (KeyName, "Zhangsan"); o Psforlist.leftpush (KeyName, "Lisi"); Opsforlist.leftpushall (KeyName, "Wangwu", "Zhaoliu", "Qianqi"); Long size = opsforlist.size (keyName); SYSTEM.OUT.PRINTLN ("Size:" + size); for (long i = 0; I &lt; Size i++) {Object value = Opsforlist.index (keyname,i); System.out.println ("I:" +i+ ", Value:" +value.tostring ());} Object Leftpop = Opsforlist.leftpop (keyName); System.out.println ("After pop size:" +opsforlist.size (KeyName));}

3 hashes (sets see for yourself.) )

Get here today, Redis has a lot of things, like cache transcation and so on. Look at the directory structure

Spring-data-redis--Simple use of Spring-data-redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.