Spring integrates Redis complete instance code

  • 2020-07-21 08:05:59
  • OfStack

Those of you who have worked on large software systems know that as the data in the system gets bigger and more complex, the problem that comes with it is that the performance of the system gets worse and worse, especially the performance loss caused by frequent operation of the database. Many performance managers have come up with a number of solutions and frameworks to optimize the performance penalty of such frequent database operations, the two most prominent cache servers being Memcached and Redis. Today, instead of talking about Memcached and Redis itself, we'll focus on how to integrate spring with Redis.

1. pom construction


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
 
 <modelVersion>4.0.0</modelVersion> 
 <groupId>com.x.redis</groupId> 
 <artifactId>springredis</artifactId> 
 <version>0.0.1-SNAPSHOT</version> 
 
 <dependencies> 
 <dependency> 
  <groupId>org.springframework.data</groupId> 
  <artifactId>spring-data-redis</artifactId> 
  <version>1.0.2.RELEASE</version> 
 </dependency> 
 <dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-test</artifactId> 
  <version>3.1.2.RELEASE</version> 
  <scope>test</scope> 
 </dependency> 
  
 <dependency> 
  <groupId>redis.clients</groupId> 
  <artifactId>jedis</artifactId> 
  <version>2.1.0</version> 
 </dependency> 
  
  <dependency> 
  <groupId>junit</groupId> 
  <artifactId>junit</artifactId> 
  <version>4.8.2</version> 
  <scope>test</scope> 
 </dependency> 
 </dependencies> 
</project> 

2. spring configuration file (ES15en.xml)


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
 xmlns:aop="http://www.springframework.org/schema/aop" 
 xsi:schemaLocation=" 
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
 
 <context:property-placeholder location="classpath:redis.properties" /> 
 
 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
  <property name="maxIdle" value="${redis.maxIdle}" /> 
  <property name="maxActive" value="${redis.maxActive}" /> 
  <property name="maxWait" value="${redis.maxWait}" /> 
  <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 
 </bean> 
  
 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
  p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> 
  
 <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 
  <property name="connectionFactory" ref="connectionFactory" /> 
 </bean>   
  
 <bean id="userDao" class="com.lyz.dao.impl.UserDaoImpl" /> 
</beans> 

3, redis properties


# Redis settings 
redis.host=192.168.157.130 
redis.port=6379 
redis.pass=liuyazhuang 
 
 
redis.maxIdle=300 
redis.maxActive=600 
redis.maxWait=1000 
redis.testOnBorrow=true 

4. User entity Class


package com.lyz.entity; 
import java.io.Serializable; 
 
/** 
 * user Entity class  
 * @author liuyazhuang 
 * 
 */ 
public class User implements Serializable { 
  
 private static final long serialVersionUID = -6011241820070393952L; 
 
 private String id; 
  
 private String name; 
  
 private String password; 


 public User() { 
   
 } 
  

 public User(String id, String name, String password) { 
  super(); 
  this.id = id; 
  this.name = name; 
  this.password = password; 
 } 
 
 /** 
  *  To obtain id 
  * @return the id 
  */ 
 public String getId() { 
  return id; 
 } 
 
 /** 
  *  Set up the id 
  * @param id the id to set 
  */ 
 public void setId(String id) { 
  this.id = id; 
 } 
 
 /** 
  *  To obtain name 
  * @return the name 
  */ 
 public String getName() { 
  return name; 
 } 
 
 /** 
  *  Set up the name 
  * @param name the name to set 
  */ 
 public void setName(String name) { 
  this.name = name; 
 } 
 
 /** 
  *  To obtain password 
  * @return the password 
  */ 
 public String getPassword() { 
  return password; 
 } 
 
 /** 
  *  Set up the password 
  * @param password the password to set 
  */ 
 public void setPassword(String password) { 
  this.password = password; 
 } 
} 

5, User operation interface IUserDao


package com.lyz.dao; 
import java.util.List; 
import com.lyz.entity.User;  
/** 
 * user Operation interface  
 * @author liuyazhuang 
 * 
 */ 
public interface IUserDao { 
  
 /** 
  *  new  
  * @param user 
  * @return 
  */ 
 boolean add(User user); 
  
 /** 
  *  A batch of new   use pipeline way  
  * @param list 
  * @return 
  */ 
 boolean add(List<User> list); 
  
 /** 
  *  delete  
  * @param key 
  */ 
 void delete(String key); 
  
 /** 
  *  To delete multiple  
  * @param keys 
  */ 
 void delete(List<String> keys); 
  
 /** 
  *  Modify the  
  * @param user 
  * @return 
  */ 
 boolean update(User user); 
 
 /** 
  *  through key To obtain  
  * @param keyId 
  * @return 
  */ 
 User get(String keyId); 
}

6. Basic abstract classes


package com.lyz.dao.impl; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.data.redis.serializer.RedisSerializer; 
 
 
/** 
 *  Basic abstract classes  
 * @author liuyazhuang 
 * 
 * @param <K> 
 * @param <V> 
 */ 
public abstract class AbstractBaseRedisDao<K, V> { 
  
 @Autowired 
 protected RedisTemplate<K, V> redisTemplate; 
 
 /** 
  *  Set up the redisTemplate 
  * @param redisTemplate the redisTemplate to set 
  */ 
 public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) { 
  this.redisTemplate = redisTemplate; 
 } 
  
 /** 
  *  To obtain  RedisSerializer 
  * <br>------------------------------<br> 
  */ 
 protected RedisSerializer<String> getRedisSerializer() { 
  return redisTemplate.getStringSerializer(); 
 } 
} 

7, IUserDao implementation class UserDaoImpl


package com.lyz.dao.impl; 
import java.util.ArrayList; 
import java.util.List; 
import org.springframework.dao.DataAccessException; 
import org.springframework.data.redis.connection.RedisConnection; 
import org.springframework.data.redis.core.RedisCallback; 
import org.springframework.data.redis.serializer.RedisSerializer; 
import org.springframework.util.Assert; 
import com.lyz.dao.IUserDao; 
import com.lyz.entity.User;  
/** 
 *  The implementation class of the interface  
 * @author liuyazhuang 
 * 
 */ 
public class UserDaoImpl extends AbstractBaseRedisDao<String, User> implements IUserDao { 
 
 /** 
  *  new  
  * @param user 
  * @return 
  */ 
 public boolean add(final User user) { 
  boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 
   public Boolean doInRedis(RedisConnection connection) 
     throws DataAccessException { 
    RedisSerializer<String> serializer = getRedisSerializer(); 
    byte[] key = serializer.serialize(user.getId()); 
    byte[] name = serializer.serialize(user.getName()); 
    return connection.setNX(key, name); 
   } 
  }); 
  return result; 
 } 
  
 /** 
  *  A batch of new   use pipeline way  
  *@param list 
  *@return 
  */ 
 public boolean add(final List<User> list) { 
  Assert.notEmpty(list); 
  boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 
   public Boolean doInRedis(RedisConnection connection) 
     throws DataAccessException { 
    RedisSerializer<String> serializer = getRedisSerializer(); 
    for (User user : list) { 
     byte[] key = serializer.serialize(user.getId()); 
     byte[] name = serializer.serialize(user.getName()); 
     connection.setNX(key, name); 
    } 
    return true; 
   } 
  }, false, true); 
  return result; 
 } 
  
 /** 
  *  delete  
  * @param key 
  */ 
 public void delete(String key) { 
  List<String> list = new ArrayList<String>(); 
  list.add(key); 
  delete(list); 
 } 
 
 /** 
  *  To delete multiple  
  * @param keys 
  */ 
 public void delete(List<String> keys) { 
  redisTemplate.delete(keys); 
 } 
 
 /** 
  *  Modify the  
  * @param user 
  * @return 
  */ 
 public boolean update(final User user) { 
  String key = user.getId(); 
  if (get(key) == null) { 
   throw new NullPointerException(" The data row does not exist , key = " + key); 
  } 
  boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { 
   public Boolean doInRedis(RedisConnection connection) 
     throws DataAccessException { 
    RedisSerializer<String> serializer = getRedisSerializer(); 
    byte[] key = serializer.serialize(user.getId()); 
    byte[] name = serializer.serialize(user.getName()); 
    connection.set(key, name); 
    return true; 
   } 
  }); 
  return result; 
 } 
 
 /** 
  *  through key To obtain  
  * @param keyId 
  * @return 
  */ 
 public User get(final String keyId) { 
  User result = redisTemplate.execute(new RedisCallback<User>() { 
   public User doInRedis(RedisConnection connection) 
     throws DataAccessException { 
    RedisSerializer<String> serializer = getRedisSerializer(); 
    byte[] key = serializer.serialize(keyId); 
    byte[] value = connection.get(key); 
    if (value == null) { 
     return null; 
    } 
    String name = serializer.deserialize(value); 
    return new User(keyId, name, null); 
   } 
  }); 
  return result; 
 } 
} 

8. Test class RedisTest


package com.lyz.test; 
import java.util.ArrayList; 
import java.util.List; 
import junit.framework.Assert; 
import org.junit.Test; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; 
import com.lyz.dao.IUserDao; 
import com.lyz.entity.User; 
/** 
 * Redis The test class  
 * @author liuyazhuang 
 * 
 */ 
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"}) 
public class RedisTest extends AbstractJUnit4SpringContextTests { 
  
 @Autowired 
 private IUserDao userDao; 
  
 /** 
  *  new  
  */ 
 @Test 
 public void testAddUser() { 
  User user = new User(); 
  user.setId("user1"); 
  user.setName("liuyazhuang"); 
  boolean result = userDao.add(user); 
  Assert.assertTrue(result); 
 } 
  
 /** 
  *  A batch of new   The ordinary way  
  */ 
 @Test 
 public void testAddUsers1() { 
  List<User> list = new ArrayList<User>(); 
  for (int i = 10; i < 50000; i++) { 
   User user = new User(); 
   user.setId("user" + i); 
   user.setName("liuyazhuang" + i); 
   list.add(user); 
  } 
  long begin = System.currentTimeMillis(); 
  for (User user : list) { 
   userDao.add(user); 
  } 
  System.out.println(System.currentTimeMillis() - begin); 
 } 
  
 /** 
  *  A batch of new  pipeline way  
  */ 
 @Test 
 public void testAddUsers2() { 
  List<User> list = new ArrayList<User>(); 
  for (int i = 10; i < 1500000; i++) { 
   User user = new User(); 
   user.setId("user" + i); 
   user.setName("liuyazhuang" + i); 
   list.add(user); 
  } 
  long begin = System.currentTimeMillis(); 
  boolean result = userDao.add(list); 
  System.out.println(System.currentTimeMillis() - begin); 
  Assert.assertTrue(result); 
 } 
  
 /** 
  *  Modify the  
  */ 
 @Test 
 public void testUpdate() { 
  User user = new User(); 
  user.setId("user1"); 
  user.setName("liuyazhuang"); 
  boolean result = userDao.update(user); 
  Assert.assertTrue(result); 
 } 
  
 /** 
  *  through key Removing a single  
  */ 
 @Test 
 public void testDelete() { 
  String key = "user1"; 
  userDao.delete(key); 
 } 
  
 /** 
  *  Batch delete  
  */ 
 @Test 
 public void testDeletes() { 
  List<String> list = new ArrayList<String>(); 
  for (int i = 0; i < 10; i++) { 
   list.add("user" + i); 
  } 
  userDao.delete(list); 
 } 
  
 /** 
  *  To obtain  
  */ 
 @Test 
 public void testGetUser() { 
  String id = "user1"; 
  User user = userDao.get(id); 
  Assert.assertNotNull(user); 
  Assert.assertEquals(user.getName(), "liuyazhuang"); 
 } 
 
 /** 
  *  Set up the userDao 
  * @param userDao the userDao to set 
  */ 
 public void setUserDao(IUserDao userDao) { 
  this.userDao = userDao; 
 } 
} 

9. Warm tips

Project download address: ES56en-Redis_jb51.rar


Related articles: