Redis is a key-value storage system. It supports storing more value types, including string (string), list (linked list), set (set), and Zset (ordered collections). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. To ensure efficiency, the data is cached in memory. Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis. The following is an integrated standalone and clustered version of Jedis operations for Redis and spring:
First, you are ready to click the version and the cluster version of how to: Jedisclientsingle, Jedisclientcluster
Importorg.springframework.beans.factory.annotation.Autowired;Importcom.taotao.rest.dao.JedisClient;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool; Public classJedisclientsingle{@AutowiredPrivateJedispool Jedispool; Publicstring Get (String key) {//Gets the value of the specified key. If key does not exist, return nil Jedis Jedis=Jedispool.getresource (); String String=Jedis.get (key); Jedis.close (); returnstring; } PublicString Set (string key, String value) {//sets some string value Jedis Jedis=Jedispool.getresource (); String String=Jedis.set (key, value); Jedis.close (); returnstring; } Publicstring Hget (String hkey, String key) {//Gets the value of the specified field in the hash table Jedis Jedis=Jedispool.getresource (); String String=Jedis.hget (hkey, key); Jedis.close (); returnstring; } Public LongHset (String hkey, String key, String value) {//To assign a value to a field in a hash table Jedis Jedis=Jedispool.getresource (); Longresult =Jedis.hset (hkey, key, value); Jedis.close (); returnresult; } Public Longincr (String key) {//Adds a number value stored in key, if key does not exist, then the value of key is first initialized to 0 and then the INCR operation is performed Jedis Jedis=Jedispool.getresource (); Longresult =JEDIS.INCR (key); Jedis.close (); returnresult; } Public LongExpire (String key,intsecond) {//Set expiry time for key Jedis Jedis=Jedispool.getresource (); Longresult =Jedis.expire (key, second); Jedis.close (); returnresult; } Public LongTTL (String key) {//Returns the remaining expiration time of key in seconds Jedis Jedis=Jedispool.getresource (); Longresult =Jedis.ttl (key); Jedis.close (); returnresult; } Public Longdel (String key) {//delete Jedis Jedis based on key=Jedispool.getresource (); Longresult =Jedis.del (key); Jedis.close (); returnresult; } Public LongHdel (String hkey, String key) {//deletes one or more of the specified fields in the hash table key Jedis Jedis=Jedispool.getresource (); Longresult =Jedis.hdel (hkey, key); Jedis.close (); returnresult; }}
Importorg.springframework.beans.factory.annotation.Autowired;Importcom.taotao.rest.dao.JedisClient;ImportRedis.clients.jedis.JedisCluster; Public classJedisclientcluster{@AutowiredPrivateJediscluster Jediscluster; Publicstring Get (String key) {returnJediscluster.get (key); } Publicstring Set (String key, String value) {//TODO auto-generated Method Stub returnJediscluster.set (key, value); } Publicstring Hget (String hkey, String key) {//TODO auto-generated Method Stub returnJediscluster.hget (hkey, key); } Public LongHset (String hkey, String key, String value) {//TODO auto-generated Method Stub returnJediscluster.hset (hkey, key, value); } Public Longincr (String key) {//TODO auto-generated Method Stub returnJEDISCLUSTER.INCR (key); } Public LongExpire (String key,intsecond) { //TODO auto-generated Method Stub returnJediscluster.expire (key, second); } Public LongTTL (String key) {//TODO auto-generated Method Stub returnJediscluster.ttl (key); } Public Longdel (String key) {//TODO auto-generated Method Stub returnJediscluster.del (key); } Public LongHdel (String hkey, String key) {//TODO auto-generated Method Stub returnJediscluster.hdel (hkey, key); }
Configuration file: Applicationcontext-jedis.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp//Www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd"><!--Jedis stand-by-<bean id= "Redisclient"class= "Redis.clients.jedis.JedisPool" > <constructor-arg name= "host" value= "192.168.242.135" ></ constructor-arg> <constructor-arg name= "Port" value= "6379" ></constructor-arg> </bean> & Lt;bean id= "Jedisclient"class= "Com.*.*." Jedisclientsingle "></bean> <!--Jedis Cluster Edition--<bean id=" Redisclient "class= "Redis.clients.jedis.JedisCluster" > <constructor-arg name= "Nodes" > <set> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.242.135" ></ constructor-arg> <constructor-arg name= "Port" value= "7001" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.242.135" ></ constructor-arg> <constructor-arg name= "Port" value= "7002" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.242.135" ></ constructor-arg> <constructor-arg name= "Port" value= "7003" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.242.135" ></ constructor-arg> <constructor-arg name= "Port" value= "7004" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.242.135" ></ constructor-arg> <constructor-arg name= "Port" value= "7005" ></constructor-arg> </bean> <beanclass= "Redis.clients.jedis.HostAndPort" > <constructor-arg name= "host" value= "192.168.242.135" ></ constructor-arg> <constructor-arg name= "Port" value= "7006" ></constructor-arg> </bean> </set> </constructor-arg> </bean> <bean id= "jedisclientclu Sterclass= "Com.*.*." Jedisclientcluster "></bean></beans>
Complete the above steps, using Jedisclientsingle, Jedisclientcluster call the specific method to operate
Jedis operation Redis and spring integration in Java