激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring整合Redis完整實例代碼

Spring整合Redis完整實例代碼

2020-09-14 15:21liuyazhuang Java教程

這篇文章主要介紹了Spring整合Redis完整實例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

做過大型軟件系統的同學都知道,隨著系統數據越來越龐大,越來越復雜,隨之帶來的問題就是系統性能越來越差,尤其是頻繁操作數據庫帶來的性能損耗更為嚴重。很多業績大牛為此提出了眾多的解決方案和開發了很多框架以優化這種頻繁操作數據庫所帶來的性能損耗,其中,尤為突出的兩個緩存服務器是Memcached和Redis。今天,我們不講Memcached和Redis本身,這里主要為大家介紹如何使spring與Redis整合。

1、pom構建

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<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配置文件(applicationContext.xml)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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

?
1
2
3
4
5
6
7
8
9
10
# 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實體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.lyz.entity;
import java.io.Serializable;
 
/**
 * user實體類
 * @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;
 }
 
 /**
  * 獲得id
  * @return the id
  */
 public String getId() {
  return id;
 }
 
 /**
  * 設置id
  * @param id the id to set
  */
 public void setId(String id) {
  this.id = id;
 }
 
 /**
  * 獲得name
  * @return the name
  */
 public String getName() {
  return name;
 }
 
 /**
  * 設置name
  * @param name the name to set
  */
 public void setName(String name) {
  this.name = name;
 }
 
 /**
  * 獲得password
  * @return the password
  */
 public String getPassword() {
  return password;
 }
 
 /**
  * 設置password
  * @param password the password to set
  */
 public void setPassword(String password) {
  this.password = password;
 }
}

5、User操作的接口IUserDao

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.lyz.dao;
import java.util.List;
import com.lyz.entity.User; 
/**
 * user操作接口
 * @author liuyazhuang
 *
 */
public interface IUserDao {
  
 /**
  * 新增
  * @param user
  * @return
  */
 boolean add(User user);
  
 /**
  * 批量新增 使用pipeline方式
  * @param list
  * @return
  */
 boolean add(List<User> list);
  
 /**
  * 刪除
  * @param key
  */
 void delete(String key);
  
 /**
  * 刪除多個
  * @param keys
  */
 void delete(List<String> keys);
  
 /**
  * 修改
  * @param user
  * @return
  */
 boolean update(User user);
 
 /**
  * 通過key獲取
  * @param keyId
  * @return
  */
 User get(String keyId);
}

6、基本的抽象類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
 
 
/**
 * 基本的抽象類
 * @author liuyazhuang
 *
 * @param <K>
 * @param <V>
 */
public abstract class AbstractBaseRedisDao<K, V> {
  
 @Autowired
 protected RedisTemplate<K, V> redisTemplate;
 
 /**
  * 設置redisTemplate
  * @param redisTemplate the redisTemplate to set
  */
 public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
  this.redisTemplate = redisTemplate;
 }
  
 /**
  * 獲取 RedisSerializer
  * <br>------------------------------<br>
  */
 protected RedisSerializer<String> getRedisSerializer() {
  return redisTemplate.getStringSerializer();
 }
}

7、IUserDao的實現類UserDaoImpl

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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; 
/**
 * 接口的實現類
 * @author liuyazhuang
 *
 */
public class UserDaoImpl extends AbstractBaseRedisDao<String, User> implements IUserDao {
 
 /**
  * 新增
  * @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;
 }
  
 /**
  * 批量新增 使用pipeline方式
  *@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;
 }
  
 /**
  * 刪除
  * @param key
  */
 public void delete(String key) {
  List<String> list = new ArrayList<String>();
  list.add(key);
  delete(list);
 }
 
 /**
  * 刪除多個
  * @param keys
  */
 public void delete(List<String> keys) {
  redisTemplate.delete(keys);
 }
 
 /**
  * 修改
  * @param user
  * @return
  */
 public boolean update(final User user) {
  String key = user.getId();
  if (get(key) == null) {
   throw new NullPointerException("數據行不存在, 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;
 }
 
 /**
  * 通過key獲取
  * @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、測試類RedisTest

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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測試類
 * @author liuyazhuang
 *
 */
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
public class RedisTest extends AbstractJUnit4SpringContextTests {
  
 @Autowired
 private IUserDao userDao;
  
 /**
  * 新增
  */
 @Test
 public void testAddUser() {
  User user = new User();
  user.setId("user1");
  user.setName("liuyazhuang");
  boolean result = userDao.add(user);
  Assert.assertTrue(result);
 }
  
 /**
  * 批量新增 普通方式
  */
 @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);
 }
  
 /**
  * 批量新增 pipeline方式
  */
 @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);
 }
  
 /**
  * 修改
  */
 @Test
 public void testUpdate() {
  User user = new User();
  user.setId("user1");
  user.setName("liuyazhuang");
  boolean result = userDao.update(user);
  Assert.assertTrue(result);
 }
  
 /**
  * 通過key刪除單個
  */
 @Test
 public void testDelete() {
  String key = "user1";
  userDao.delete(key);
 }
  
 /**
  * 批量刪除
  */
 @Test
 public void testDeletes() {
  List<String> list = new ArrayList<String>();
  for (int i = 0; i < 10; i++) {
   list.add("user" + i);
  }
  userDao.delete(list);
 }
  
 /**
  * 獲取
  */
 @Test
 public void testGetUser() {
  String id = "user1";
  User user = userDao.get(id);
  Assert.assertNotNull(user);
  Assert.assertEquals(user.getName(), "liuyazhuang");
 }
 
 /**
  * 設置userDao
  * @param userDao the userDao to set
  */
 public void setUserDao(IUserDao userDao) {
  this.userDao = userDao;
 }
}

9、溫馨提示

項目下載地址:Spring-Redis.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/l1028386804/article/details/52108758

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 老司机一级毛片 | 久久久久久艹 | 国产寡妇xxxxxxxx性开放 | 午夜视频在线免费播放 | 看个毛片 | 亚洲欧美日韩一区二区三区在线观看 | av大全在线播放 | 国产人成精品一区二区三 | 综合99 | 最新在线黄色网址 | 国产91极品 | 成人免费午夜视频 | xfplay噜噜av| 国产精品欧美日韩一区二区 | av电影免费播放 | 国产在线精品一区二区三区 | 国产成人强伦免费视频网站 | 国产1区2 | 国产伊人色 | 国产一级在线看 | 国产1区2区3区中文字幕 | 成年人免费视频大全 | 有色视频在线观看 | 九一免费在线观看 | 国产一区二区三区视频观看 | 欧美黄色小视频 | 国产在线观看 | 欧美成人一区免费视频 | 精品一区二区三区中文字幕 | 亚洲综合色视频在线观看 | 久久久久久久久久网 | 黄在线免费 | 91九色视频在线观看 | 久久国产精品久久精品国产演员表 | 小情侣嗯啊哦视频www | 国产精品久久久久久久久久10秀 | 久草热久 | 美国黄色毛片女人性生活片 | 亚洲第一色片 | h视频免费在线观看 | 久久免费视频8 |