Redis real-Combat tutorials, Redis cache tutorials, Redis message Publishing, subscriptions, Redis Message Queuing tutorials

Source: Internet
Author: User
Tags redis server

A: This tutorial uses the environment: ubuntu12.x, jdk1.7, Intellij idea, spring3.2.8, Redis server 3.0,jedis client 2.7.3

Spring-data-redis 1.6.0

II: Redis Server Installation Tutorial Here is an unknown solution

Three: Examples of Redis cache features are:

Spring Configuration:

<bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" >
<property name= "maxtotal" value= "/>"
<property name= "Maxidle" value= "ten"/>
<property name= "Maxwaitmillis" value= "/>"
<property name= "Minidle" value= "1"/>
<property name= "Testonborrow" value= "true"/>
</bean>


<bean id= "Jedisfactory" class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name= "Port" value= "6379"/>
<property name= "HostName" value= "10.3.11.147"/>
<property name= "Poolconfig" >
<ref bean= "Jedispoolconfig"/>
</property>
<property name= "Timeout" value= "10000"/>
</bean>


<bean id= "Redistemplate" class= "Org.springframework.data.redis.core.RedisTemplate" >
<property name= "ConnectionFactory" ref= "Jedisfactory"/>
<!--If you do not configure serializer, then the intelligent use of String when stored, if stored with the user type, then the error user can ' t cast to string!!! -
<!--<property name= "Keyserializer" >-->
<!--<bean class= "Org.springframework.data.redis.serializer.StringRedisSerializer"/>-->
<!--</property>-->
<!--<property name= "ValueSerializer" >-->
<!--<bean class= "Org.springframework.data.redis.serializer.StringRedisSerializer"/>-->
<!--</property>-->
</bean>


The cache uses the test class:

public class Testsringdatajedis {
Static Redistemplate redistemplate;


public void Set (String key,object value) {
Valueoperations Understanding into Map<object,object>


Redistemplate.opsforvalue (). Set ("Redis-key", "I ' m Test Spring-data-redis");
Valueoperations valueoperations = Redistemplate.opsforvalue ();
Valueoperations.set (Key,value);


Boundvalueoperations's understanding to do some minor manipulation of the saved values
Boundvalueoperations boundvalueoperations = Redistemplate.boundvalueops (key);
}
Public Object get (String key) {
Return Redistemplate.opsforvalue (). get (key);
}
public void setlist (String key,list<?> value) {
Listoperations can be understood as list<object>
Listoperations listoperations= redistemplate.opsforlist ();
Listoperations.leftpush (key, value);
. Leftpushall (value);
}
Public Object getList (String key) {
Listoperations can be understood as list<object>
Return Redistemplate.opsforlist (). Leftpop ("Test-list");
}
public void Setset (String key,set<?> value) {
Setoperations setoperations= Redistemplate.opsforset ();
Setoperations.add (key, value);
}
Public Object Getset (String key) {
Return Redistemplate.opsforset (). Members (key);
}


public void Sethash (String key,map<string,?> value) {
Hashoperations hashoperations = Redistemplate.opsforhash ();
Hashoperations.putall (Key,value);
}
Public Object Gethash (String key) {
Return Redistemplate.opsforhash (). Entries (key);
}


public void Delete (String key) {
Redistemplate.delete (key);
}
public void ClearAll () {
Redistemplate.multi ();
//    }


public static void Main (string[] args) {
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Redis.xml");
Redistemplate = (redistemplate) ctx.getbean ("Redistemplate");
Testsringdatajedis Jedis = new Testsringdatajedis ();
String
Jedis.set ("test-string", "good-the victory of China's anti-Japanese War");
System.out.println ("test-string =" + Jedis.get ("test-string"));
Pojo
User User =new User ();
User.setname ("Deng");
User.setbirthday (New Date ());
User.setsex (TRUE);
Jedis.set ("Test-user", user);
System.out.println ("Test-user =" + Jedis.get ("Test-user"));
System.out.println ("Test-user:name =" + (user) Jedis.get ("Test-user"). GetName ());
List
list<string> list = new arraylist<string> ();
List.add ("Zhang San");
List.add ("John Doe");
List.add ("Leper");
String key_list = "Test-list";
Jedis.setlist (key_list, list);


List<string> test_list = (list<string>) jedis.getlist (key_list);
for (int i = 0; i < test_list.size (); i++) {
SYSTEM.OUT.PRINTLN (i + "=" + Test_list.get (i));
}
Map
String Key_map = "Test-map";
map<string,object> map = new hashmap<string, object> ();
Map.put ("Map1", "map-Zhang San");
Map.put ("Map2", "map-John Doe");
Map.put ("Map3", "map-Leper");
Jedis.sethash (key_map, map);
Map<string,object> Getmap = (map<string,object>) jedis.gethash (KEY_MAP);
Return
}
}

Four: Message subscription and release

Spring Configuration:

<bean id= "Listener" class= "Com.dengyang.redis.TestMessage"/>

<redis:listener-container connection-factory= "Jedisfactory" >
<!--the method attribute can skipped as the default method name is "Handlemessage"-
<!--topic represents the Listening channel, is a regular match is actually the channel you want to subscribe to--
<redis:listener ref= "Listener" method= "Handlemessage" topic= "*"/>
</redis:listener-container>


Message Publishing:

/**
* Publish Channel messages
* @param pkey
* @param message
* @return Returns the number of subscribers
*/

redistemplate. Convertandsend ("java""Java I post messages!");


Public Long Publish (final string channel,final string message) {
TODO auto-generated Method Stub
Long Recvs = (long) this.getredistemplate (). Execute (new rediscallback<object> () {


Public Object Doinredis (redisconnection connection)
Throws DataAccessException {
TODO auto-generated Method Stub
Return Connection.publish (Channel.getbytes (), message.getbytes ());
}
});
return RECVS;
}

Message Processing classes:

public class TestMessage {
Static Redistemplate redistemplate;


public static void Main (string[] args) {
New Classpathxmlapplicationcontext ("/redis.xml");
while (true) {
System.out.println ("Current time:" + new Date ());
try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
public void Handlemessage (Serializable message) {
if (message = = NULL) {
SYSTEM.OUT.PRINTLN ("null");
} else if (Message.getclass (). IsArray ()) {
System.out.println (Arrays.tostring ((object[)));
} else if (message instanceof list<?>) {
SYSTEM.OUT.PRINTLN (message);
} else if (message instanceof map<?,? >) {
SYSTEM.OUT.PRINTLN (message);
} else {
SYSTEM.OUT.PRINTLN (message);
}
}
}

V: use of Message Queuing

private redistemplate redistemplate;

The queue key is the message channel, the value message content

Public Long puttoqueue (final string key, final string value) {

Long L = (long) this.getredistemplate (). Execute (new rediscallback<object> () {
Public Object Doinredis (redisconnection connection)
Throws DataAccessException {
TODO auto-generated Method Stub
Return Connection.lpush (Key.getbytes (), value.getbytes ());
}
});
return l;
}

//Read message (no message is read in queue) key is the message channel
public string Getfromqueue (final string key) {
TODO auto-generated Method Stub
Byte[] B = (byte[]) this.getredistemplate (). Execute (new rediscallback<object> () {
Public Object Doinredis (redisconnection connection)
Throws DataAccessException {
TODO auto-generated Method Stub
Return Connection.lpop (Key.getbytes ());
}
});
if (b! = null)
{
return new String (b);
}
return null;
}

Redis real-Combat tutorials, Redis cache tutorials, Redis message Publishing, subscriptions, Redis Message Queuing tutorials

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.