Redis's client implementation, mainly divided into Spring-redis-data, Jredis.
Record the learning experience of spring-redis-data;
In the Spring-redis-data, I used it mainly to save, take and clear.
Redis Configuration redis-manager-config.properties:
Server address for Redis.host=192.168.1.20//redis
Service port for Redis.port=6400//redis
redis.pass=1234xxxxx//Password
redis.default.db=0//Linked Database
redis.timeout=100000//Client Time-out unit is milliseconds
redis.maxactive=300//Maximum number of connections
redis.maxidle=100//Maximum idle number
[HTML] View plaincopy
redis.maxwait=1000//Maximum setup Connection wait time
redis.testonborrow=true//Indicates whether the test is performed before the connection is removed from the pool, if the test fails, the connection is removed from the pool and an attempt is made to remove the other
Configure in Spring
<bean id= "Propertyconfigurerredis" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">
<property name= "Order" value= "1"/>
<property name= "Ignoreunresolvableplaceholders" value= "true"/>
<property name= "Locations" >
<list>
<value>classpath:config/redis-manager-config.properties</value>
</list>
</property>
</bean>
<!--Jedis pool configuration-->
<bean id= " Jedispoolconfig "class=" redis.clients.jedis.JedisPoolConfig;
<property name= "maxactive" value= "${redis.maxactive}"/>
< Property Name= "Maxidle" value= "${redis.maxidle}"/>
<property Name= "maxwait" value= "${redis.maxwait}"/>
<property name= " Testonborrow "value=" ${redis.testonborrow} "/>
</bean>
<!--spring data redis-->
<bean id= "Jedisconnectionfactory" Class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory";
<property name= "Usepool" value= "true" ></PROPERTY>
<property name= "HostName" value= "${redis.host}"/>
<property name= "Port" value= "${redis.port}"/>
<property name= "Password" value= "${redis.pass}"/>
< Property Name= "Timeout" value= "${redis.timeout}"/>
<property Name= "Database" value= "${redis.default.db}" ></PROPERTY>
<constructor-arg index= "0" ref= "jedispoolconfig"/>
</bean>
<bean id= "Redistemplate" class= "Org.springframework.data.redis.core.StringRedisTemplate" >
<property name= "ConnectionFactory" ref= "Jedisconnectionfactory"/>
</bean>
<!--Configure a base class (the following business class inherits from the Class), inject redistemplate
<bean id= "Redisbase" abstract= "true" >
<property name= "template" ref= "Redistemplate" ></property>
</bean>
Java code:
Copy Codecode example:
public class Redisbase {
private stringredistemplate template;
/**
* @return The template
*/
Public Stringredistemplate gettemplate () {
return template;
}
/**
* @param template The template to set
*/
public void SetTemplate (stringredistemplate template) {
This.template = template;
}
}
Go on:
Write, read, and clear the cache for specific Redis values!
First: Write
Copy Codecode example:
Public class Studentcountdo {
private Long ID;
private String StudentID;
private Long commentheadcount;
private Long docattitudescores;
private Long guideservicescores;
private Long treateffectcount;
private Long treateffectscores;
private String gmtmodified;
private String gmtcreated;
private Long waitingtimescores;
}
Stringredistemplate template = GetTemplate ();//Get the template injected above
Save as hash general key to add a prefix, easy to clear all such key
Boundhashoperations<string, String, string> ops = Template.boundhashops ("Student:" +studentcount.getstudentid ( ));
map<string, string> data = new hashmap<string, string> ();
Data.put ("StudentID", Commentutils.convertnull (Studentcount.getstudentid ()));
Data.put ("Commentheadcount", Commentutils.convertlongtostring (Studentcount.getcommentheadcount ()));
Data.put ("Docattitudescores", Commentutils.convertlongtostring (Studentcount.getdocattitudescores ()));
Data.put ("Guideservicesscores", Commentutils.convertlongtostring (Studentcount.getguideservicescores ()));
Data.put ("Treateffectcount", Commentutils.convertlongtostring (Studentcount.gettreateffectcount ()));
Data.put ("Treateffectscores", Commentutils.convertlongtostring (Studentcount.gettreateffectscores ()));
Data.put ("Waitingtimescores", Commentutils.convertlongtostring (Studentcount.getwaitingtimescores ()));
try {
Ops.putall (data);
} catch (Exception e) {
Logger.error (Commentconstants.write_expert_comment_count_redis_error + studentcount.studentcount (), E);
}
Second, take out
Copy Codecode example: Public studentcountdo getstudentcommentcountinfo (String studentid) {
Final String strkey = "Student:" + StudentID;
Return GetTemplate (). Execute (new rediscallback<studentcountdo> () {
@Override
Public Studentcountdo Doinredis (redisconnection connection) throws DataAccessException {
byte[] Bkey = GetTemplate (). Getstringserializer (). Serialize (strkey);
if (connection.exists (Bkey)) {
list<byte[]> value = Connection.hmget (Bkey,
GetTemplate (). Getstringserializer (). Serialize ("StudentID"), GetTemplate ()
. Getstringserializer (). Serialize ("Commentheadcount"), GetTemplate ()
. Getstringserializer (). Serialize ("Docattitudescores"), GetTemplate ()
. Getstringserializer (). Serialize ("Guideservicesscores"), GetTemplate ()
. Getstringserializer (). Serialize ("Treateffectcount"), GetTemplate ()
. Getstringserializer (). Serialize ("Treateffectscores"), GetTemplate ()
. Getstringserializer (). Serialize ("Waitingtimescores"));
Studentcountdo studentcommentcountdo = new Studentcountdo ();
Studentcommentcountdo.setexpertid (GetTemplate (). Getstringserializer (). Deserialize (Value.get (0)));
Studentcommentcountdo.setcommentheadcount (Long.parselong (GetTemplate () Getstringserializer ()
. Deserialize (Value.get (1)));
Studentcommentcountdo.setdocattitudescores (Long.parselong (GetTemplate () Getstringserializer ()
. Deserialize (Value.get (2)));
Studentcommentcountdo.setguideservicescores (Long.parselong (GetTemplate () Getstringserializer ()
. Deserialize (Value.get (3)));
Studentcommentcountdo.settreateffectcount (Long.parselong (GetTemplate () Getstringserializer ()
. Deserialize (Value.get (4)));
Studentcommentcountdo.settreateffectscores (Long.parselong (GetTemplate () Getstringserializer ()
. Deserialize (Value.get (5)));
Studentcommentcountdo.setwaitingtimescores (Long.parselong (GetTemplate () Getstringserializer ()
. Deserialize (Value.get (6)));
return studentcommentcountdo;
}
return null;
}
});
}
The process of saving and fetching is actually to serialize the fields of the object into the HashMap, and take them out in the order in which they were deposited.
Third Purge
This is based on the previous prefix is very simple, a code will be done!
Copy Codecode example: private void Clear (String pattern) {
Stringredistemplate template = GetTemplate ();
set<string> keys = Template.keys (pattern);
if (!keys.isempty ()) {
Template.delete (keys);
}
}
Pattern is passed in as student: You can erase all caches of that type!
A getting Started instance of Redis implementation Spring-redis-data