1. 程式人生 > >spring 與 redis 整合及使用

spring 與 redis 整合及使用

1、實現目標

  通過redis快取資料。(目的不是加快查詢的速度,而是減少資料庫的負擔)  

2、所需jar包

 

  注意:jdies和commons-pool兩個jar的版本是有對應關係的,注意引入jar包是要配對使用,否則將會報錯。因為commons-pooljar的目錄根據版本的變化,目錄結構會變。前面的版本是org.apache.pool,而後面的版本是org.apache.pool2...

style=" color: white; font-size: 17px; font-weight: bold;"3、redis簡介

  redis是一個key-value儲存系統。和Memcached類似,它支援儲存的value型別相對更多,包括string(字串)、list(連結串列)、set(集合)、zset(sorted set --有序集合)和hash(雜湊型別)。這些資料型別都支援push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎上,redis支援各種不同方式的排序。與memcached一樣,為了保證效率,資料都是快取在記憶體中。區別的是redis會週期性的把更新的資料寫入磁碟或者把修改操作寫入追加的記錄檔案,並且在此基礎上實現了master-slave(主從)

3、編碼實現

1)、配置的檔案(properties)

  將那些經常要變化的引數配置成獨立的propertis,方便以後的修改redis.properties

1

2

3

4

5

6

7

8

9

redis.hostName=127.0.0.1

redis.port=6379

redis.timeout=15000

redis.usePool=true

 

redis.maxIdle=6

redis.minEvictableIdleTimeMillis=300000

redis.numTestsPerEvictionRun=3

redis.timeBetweenEvictionRunsMillis=

60000

2)、spring-redis.xml

  redis的相關引數配置設定。引數的值來自上面的properties檔案

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

<beans xmlns="http://www.springframework.org/schema/beans"

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.xsd" default-autowire="byName">

 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

  <!-- <property name="maxIdle" value="6"></property>

  <property name="minEvictableIdleTimeMillis" value="300000"></property>

  <property name="numTestsPerEvictionRun" value="3"></property>

  <property name="timeBetweenEvictionRunsMillis" value="60000"></property> -->

   

  <property name="maxIdle" value="${redis.maxIdle}"></property>

  <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"></property>

  <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"></property>

  <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"></property>

 </bean>

 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">

  <property name="poolConfig" ref="jedisPoolConfig"></property>

  <property name="hostName" value="${redis.hostName}"></property>

  <property name="port" value="${redis.port}"></property>

  <property name="timeout" value="${redis.timeout}"></property>

  <property name="usePool" value="${redis.usePool}"></property>

 </bean>

 <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

  <property name="connectionFactory" ref="jedisConnectionFactory"></property>

  <property name="keySerializer">

   <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

  </property>

  <property name="valueSerializer">

   <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>

  </property>

 </bean>

</beans>

3)、applicationContext.xml

  spring的總配置檔案,在裡面假如一下的程式碼

1

2

3

4

5

6

7

8

9

10

11

12

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />

  <property name="ignoreResourceNotFound" value="true" />

  <property name="locations">

   <list>

     

    <value>classpath*:/META-INF/config/redis.properties</value>

   </list>

  </property>

 </bean>

 

<import resource="spring-redis.xml" />

4)、web.xml

  設定spring的總配置檔案在專案啟動時載入

1

2

3

4

<context-param>

 <param-name>contextConfigLocation</param-name>

 <param-value>classpath*:/META-INF/applicationContext.xml</param-value><!-- -->

</context-param>

5)、redis快取工具類

ValueOperations  ——基本資料型別和實體類的快取
ListOperations     ——list的快取
SetOperations    ——set的快取

HashOperations  Map的快取

 

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

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

import java.io.Serializable;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.data.redis.core.BoundSetOperations;

import org.springframework.data.redis.core.HashOperations;

import org.springframework.data.redis.core.ListOperations;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.SetOperations;

import org.springframework.data.redis.core.ValueOperations;

import org.springframework.stereotype.Service;

 

@Service

public class RedisCacheUtil<T>

{

 

  

 @Autowired @Qualifier("jedisTemplate")

 public RedisTemplate redisTemplate;

  

 

  

 /**

  * 快取基本的物件,Integer、String、實體類等

  * @param key 快取的鍵值

  * @param value 快取的值

  * @return  快取的物件

  */

 public <T> ValueOperations<String,T> setCacheObject(String key,T value)

 {

   

  ValueOperations<String,T> operation = redisTemplate.opsForValue();

  operation.set(key,value);

  return operation;

 }

  

 /**

  * 獲得快取的基本物件。

  * @param key  快取鍵值

  * @param operation

  * @return   快取鍵值對應的資料

  */

 public <T> T getCacheObject(String key/*,ValueOperations<String,T> operation*/)

 {

  ValueOperations<String,T> operation = redisTemplate.opsForValue();

  return operation.get(key);

 }

  

 /**

  * 快取List資料

  * @param key  快取的鍵值

  * @param dataList 待快取的List資料

  * @return   快取的物件

  */

 public <T> ListOperations<String, T> setCacheList(String key,List<T> dataList)

 {

  ListOperations listOperation = redisTemplate.opsForList();

  if(null != dataList)

  {

   int size = dataList.size();

   for(int i = 0; i < size ; i ++)

   {

     

    listOperation.rightPush(key,dataList.get(i));

   }

  }

   

  return listOperation;

 }

  

 /**

  * 獲得快取的list物件

  * @param key 快取的鍵值

  * @return  快取鍵值對應的資料

  */

 public <T> List<T> getCacheList(String key)

 {

  List<T> dataList = new ArrayList<T>();

  ListOperations<String,T> listOperation = redisTemplate.opsForList();

  Long size = listOperation.size(key);

   

  for(int i = 0 ; i < size ; i ++)

  {

   dataList.add((T) listOperation.leftPop(key));

  }

   

  return dataList;

 }

  

 /**

  * 快取Set

  * @param key  快取鍵值

  * @param dataSet 快取的資料

  * @return   快取資料的物件

  */

 public <T> BoundSetOperations<String,T> setCacheSet(String key,Set<T> dataSet)

 {

  BoundSetOperations<String,T> setOperation = redisTemplate.boundSetOps(key);

  /*T[] t = (T[]) dataSet.toArray();

    setOperation.add(t);*/

   

   

  Iterator<T> it = dataSet.iterator();

  while(it.hasNext())

  {

   setOperation.add(it.next());

  }

   

  return setOperation;

 }

  

 /**

  * 獲得快取的set

  * @param key

  * @param operation

  * @return

  */

 public Set<T> getCacheSet(String key/*,BoundSetOperations<String,T> operation*/)

 {

  Set<T> dataSet = new HashSet<T>();

  BoundSetOperations<String,T> operation = redisTemplate.boundSetOps(key);

   

  Long size = operation.size();

  for(int i = 0 ; i < size ; i++)

  {

   dataSet.add(operation.pop());

  }

  return dataSet;

 }

  

 /**

  * 快取Map

  * @param key

  * @param dataMap

  * @return

  */

 public <T> HashOperations<String,String,T> setCacheMap(String key,Map<String,T> dataMap)

 {

   

  HashOperations hashOperations = redisTemplate.opsForHash();

  if(null != dataMap)

  {

    

   for (Map.Entry<String, T> entry : dataMap.entrySet()) {

      

    /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */

    hashOperations.put(key,entry.getKey(),entry.getValue());

   }

    

  }

   

  return hashOperations;

 }

  

 /**

  * 獲得快取的Map

  * @param key

  * @param hashOperation

  * @return

  */

 public <T> Map<String,T> getCacheMap(String key/*,HashOperations<String,String,T> hashOperation*/)

 {

  Map<String, T> map = redisTemplate.opsForHash().entries(key);

  /*Map<String, T> map = hashOperation.entries(key);*/

  return map;

 }

  

  

  

  

  

  

  

 /**

  * 快取Map

  * @param key

  * @param dataMap

  * @return

  */

 public <T> HashOperations<String,Integer,T> setCacheIntegerMap(String key,Map<Integer,T> dataMap)

 {

  HashOperations hashOperations = redisTemplate.opsForHash();

  if(null != dataMap)

  {

    

   for (Map.Entry<Integer, T> entry : dataMap.entrySet()) {

      

    /*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */

    hashOperations.put(key,entry.getKey(),entry.getValue());

   }

    

  }

   

  return hashOperations;

 }

  

 /**

  * 獲得快取的Map

  * @param key

  * @param hashOperation

  * @return

  */

 public <T> Map<Integer,T> getCacheIntegerMap(String key/*,HashOperations<String,String,T> hashOperation*/)

 {

  Map<Integer, T> map = redisTemplate.opsForHash().entries(key);

  /*Map<String, T> map = hashOperation.entries(key);*/

  return map;

 }

}

6)、測試

  這裡測試我是在專案啟動的時候到資料庫中查找出國家和城市的資料,進行快取,之後將資料去除。

6.1  專案啟動時快取資料

 

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

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationListener;

import org.springframework.context.event.ContextRefreshedEvent;

import org.springframework.stereotype.Service;

 

import com.test.model.City;

import com.test.model.Country;

import com.zcr.test.User;

 

/*

 * 監聽器,用於專案啟動的時候初始化資訊

 */

@Service

public class StartAddCacheListener implements ApplicationListener<ContextRefreshedEvent>

{

 //日誌

 private final Logger log= Logger.getLogger(StartAddCacheListener.class);

  

 @Autowired

 private RedisCacheUtil<Object> redisCache;

  

 @Autowired

 private BrandStoreService brandStoreService;

  

 @Override

 public void onApplicationEvent(ContextRefreshedEvent event)

 {

  //spring 啟動的時候快取城市和國家等資訊

  if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext"))

  {

   System.out.println("\n\n\n_________\n\n快取資料 \n\n ________\n\n\n\n");

   List<City> cityList = brandStoreService.selectAllCityMessage();

   List<Country> countryList = brandStoreService.selectAllCountryMessage();

    

   Map<Integer,City> cityMap = new HashMap<Integer,City>();

    

   Map<Integer,Country> countryMap = new HashMap<Integer, Country>();

    

   int cityListSize = cityList.size();

   int countryListSize = countryList.size();

    

   for(int i = 0 ; i < cityListSize ; i ++ )

   {

    cityMap.put(cityList.get(i).getCity_id(), cityList.get(i));

   }

    

   for(int i = 0 ; i < countryListSize ; i ++ )

   {

    countryMap.put(countryList.get(i).getCountry_id(), countryList.get(i));

   }

    

   redisCache.setCacheIntegerMap("cityMap", cityMap);

   redisCache.setCacheIntegerMap("countryMap", countryMap);

  }

 }

  

}

6.2  獲取快取資料

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@Autowired

private RedisCacheUtil<User> redisCache;

 

@RequestMapping("testGetCache")

public void testGetCache()

{

 /*Map<String,Country> countryMap = redisCacheUtil1.getCacheMap("country");

 Map<String,City> cityMap = redisCacheUtil.getCacheMap("city");*/

 Map<Integer,Country> countryMap = redisCacheUtil1.getCacheIntegerMap("countryMap");

 Map<Integer,City> cityMap = redisCacheUtil.getCacheIntegerMap("cityMap");

  

 for(int key : countryMap.keySet())

 {

  System.out.println("key = " + key + ",value=" + countryMap.get(key));

 }

  

 System.out.println("------------city");

 for(int key : cityMap.keySet())

 {

  System.out.println("key = " + key + ",value=" + cityMap.get(key));

 }

}

由於Spring在配置檔案中配置的bean預設是單例的,所以只需要通過Autowired注入,即可得到原先的快取類。

以上就是spring+redis實現資料快取的方法,希望對大家的學習有所幫助。

分類: redis