Spring provides specialized support for Redis: Spring-data-redis. In addition, similar to the following:
I think most people are concerned about Spring-data-hadoop, Spring-data-mongodb, Spring-data-redis and SPRING-DATA-JPA.
First, brief
Spring's dedicated data operations are packaged independently in the Spring-data series, and Spring-data-redis is naturally a standalone package for Redis.
The current version of 1.0.1, mainly for the Jedis, Jredis, RJC, and the SRP and other Redis client encapsulation, while supporting transactions. It's already making me drool. Of course, the current version does not support sharding. For example, the previous article through the Jedis through the client configuration, to achieve a consistent hash, to achieve the purpose of sharding. Again, if you had written springjdbc in spring1.x, you would now feel familiar.
After some ideological struggle, I finally gave up the Jedis native realization, embraced Spring-data-redis. Why? Because, I need a framework with a transactional mechanism, a framework that does not require explicit invocation of object pool operations. These spring-data-redis have been solved. As for sharding, the current data volume requirements are not big, look forward to Redis 3.0.
Second, the configuration
Object Pool configuration:
<bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "Maxactive" Value= "${redis.pool.maxactive}"/> <property name= "Maxidle" value= "${redis.pool.maxidle}"/> < Property Name= "Maxwait" value= "${redis.pool.maxwait}"/> <property name= "Testonborrow" value= "${ Redis.pool.testOnBorrow} "/> </bean>
Factory implementation:
class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property Name= "HostName" value= "${redis.ip}"/> <property name= "Port" value= "${redis.port}"/> < Property Name= "Poolconfig" ref= "Jedispoolconfig"/> </bean>
Template class:
class= "Org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref= " Jedisconnectionfactory "/>
Isn't it much like configuring a JdbcTemplate? It's really that simple.
The redis.properties configuration is as follows:
#最大分配的对象数 redis.pool.maxactive=1024 #最大能够保持idel状态的对象数 redis.pool.maxidle=200 #当池内没有返回对象时, maximum wait time redis.pool.maxwait=1000 #当调用borrow The object method, does the validity check redis.pool.testonborrow=true #IP redis.ip= 10.11.20.140 #Port
Only one node is currently available, so expect Redis 3.0,sharding.
Three, GET, SET, del operation
Redis at the beginning of the practice, at present is to use memcached more, only these basic operations, here caught dead
Suppose to do a Userdao:
Public interface Userdao { /** * @param uid * @param address * /Void Save (user user); /** * @param uid * @return * /User Read (String uid); /** * @param uid * /void Delete (String uid);
The user object has such two properties:
public class User implements Serializable { private static final long serialversionuid = -1267719235225203410l; Private String uid;
Implementing these three methods requires the support of Redistemplate:
@Autowired private redistemplate<serializable, serializable> redistemplate;
Why serialize generics? The data that I have stored is serializable content. And more why? In fact, I can not answer a lot, while practicing learning, I good grasp must tell you
1. Save-set
Make a save artificial, using the Redis set command:
@Override public void Save (final User user) { redistemplate.execute (new rediscallback<object> () { @ Override public Object Doinredis (redisconnection connection) throws DataAccessException { Connection.set ( Redistemplate.getstringserializer (). Serialize ( "user.uid." + User.getuid ()), Redistemplate.getstringserializer (). Serialize ( user.getaddress ())); return null; }
Here is the method callback through the template class. In the spring framework, it is easy to control transactions, and if you have studied spring's DAO source code, you must be familiar with it.
1. Incoming parameters require a final identification and prohibit modification within the method.
2. Call Redisconnection's set method to implement the Redis set command.
3. Both key and value need to be serialize.
4. Serialization operations are best done using the serializer provided by Redistemplate.
That's a springjdbctemplate with that year.
2. Get-get
To get the object out of Redis, it's a bit cumbersome to serialize key, and it's best to determine if the key exists and avoid working hard. If the key value exists, the data needs to be deserialized.
@Override public User Read (final String uid) { return Redistemplate.execute (New rediscallback<user> () { @ Override public User Doinredis (redisconnection connection) throws DataAccessException { byte[] key = Redistemplate.getstringserializer (). Serialize ( "user.uid." + uid); if (connection.exists (key)) { byte[] value = Connection.get (key); String address = Redistemplate.getstringserializer () . Deserialize (value); User user = new user (); User.setaddress (address); User.setuid (UID); return user; } return null; } }); }
When writing springjdbc, it is such a field of a field assembled, don't mention how tiring. Well, with Spring-data-redis, I'm back.
1. Remember to use generics, such as rediscallback<user> ()
2. Using the same serialization/deserialization serializer
3. It is recommended to use Connection.exists (key) to determine the existence of key values, to avoid useless
3. Delete-del
Delete, simple point, but also need to toss for a while:
@Override public void Delete (final String uid) { redistemplate.execute (new rediscallback<object> () { Public Object Doinredis (redisconnection connection) { Connection.del (Redistemplate.getstringserializer (). Serialize ( "user.uid." + uid)); return null; }
Make a testcase, for the time being, I'll use it.
4. TestCase
Import static org.junit.assert.*; Import Org.junit.Before; Import Org.junit.Test; Import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; Import Org.zlex.redis.dao.UserDao; Import Org.zlex.redis.domain.User; public class Userdaotest {private ApplicationContext app; Private Userdao Userdao; @Before public void before () throws Exception {app = new Classpathxmlapplicationcontext ("Applicationcontext.x ML "); Userdao = (Userdao) app.getbean ("Userdao"); } @Test public void Crud () {//--------------Create---------------String uid = "u123456"; String Address1 = "Shanghai"; User user = new user (); User.setaddress (ADDRESS1); User.setuid (UID); Userdao.save (user); ---------------Read---------------user = Userdao.read (UID); Assertequals (Address1, user.getaddress ()); // --------------Update------------String address2 = "Beijing"; User.setaddress (ADDRESS2); Userdao.save (user); user = Userdao.read (UID); Assertequals (Address2, user.getaddress ()); --------------Delete------------userdao.delete (UID); user = Userdao.read (UID); Assertnull (user); } }
Seemingly less update, perhaps later operation of the hash, will be used.
See what the console has to gain:
Redis 127.0.0.1:6379> get user.uid.u123456 (nil) Redis 127.0.0.1:6379> get user.uid.u123456 "\xe5\x8c\x97\xe4\xba \xac "Redis 127.0.0.1:6379> get user.uid.u123456" \xe4\xb8\x8a\xe6\xb5\xb7 "Redis 127.0.0.1:6379> del user.uid.u123456 (integer) 1redis 127.0.0.1:6379> get user.uid.u123456 (nil) Redis 127.0.0.1:6379> get user.uid.u123456 "\xe4\xb8\x8a\xe6\xb5\xb7"
Well, you can start using it to save something.
This article is reproduced from: http://www.zhangsr.com/cms/blog/viewUserBlog.action?blogId=528
Conquer Redis + Jedis + spring--configuration & General Operations