spring+redis实现数据的缓存

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

31
4<property name="locations">
5<list>
6
7<value>classpath*:/META-INF/config/redis.properties</value>
8</list>
9</property>
10</bean>
11
12<import resource="spring-redis.xml"/>
4)、web.xml
设置spring的总配置⽂件在项⽬启动时加载
1<context-param>
2<param-name>contextConfigLocation</param-name>
3<param-value>classpath*:/META-INF/applicationContext.xml</param-value><!----> 4</context-param>
5)、redis缓存⼯具类
ValueOperations ——基本数据类型和实体类的缓存
ListOperations ——list的缓存
SetOperations ——set的缓存
HashOperations Map的缓存
1import java.io.Serializable;
2import java.util.ArrayList;
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Iterator;
6import java.util.List;
7import java.util.Map;
8import java.util.Set;
9
10import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.beans.factory.annotation.Qualifier;
12import org.springframework.context.support.ClassPathXmlApplicationContext;
13import org.springframework.data.redis.core.BoundSetOperations;
14import org.springframework.data.redis.core.HashOperations;
15import org.springframework.data.redis.core.ListOperations;
16import org.springframework.data.redis.core.RedisTemplate;
17import org.springframework.data.redis.core.SetOperations;
18import org.springframework.data.redis.core.ValueOperations;
19import org.springframework.stereotype.Service;
20
21@Service
22public class RedisCacheUtil<T>
23{
24
25 @Autowired @Qualifier("jedisTemplate")
26public RedisTemplate redisTemplate;
27
28/**
29 * 缓存基本的对象,Integer、String、实体类等
30 * @param key 缓存的键值
31 * @param value 缓存的值
32 * @return缓存的对象
33*/
34public <T> ValueOperations<String,T> setCacheObject(String key,T value)
35 {
36
37 ValueOperations<String,T> operation = redisTemplate.opsForValue();
38 operation.set(key,value);
39return operation;
40 }
41
42/**
43 * 获得缓存的基本对象。

44 * @param key 缓存键值
45 * @param operation
46 * @return缓存键值对应的数据
47*/
48public <T> T getCacheObject(String key/*,ValueOperations<String,T> operation*/)
48public <T> T getCacheObject(String key/*,ValueOperations<String,T> operation*/)
49 {
50 ValueOperations<String,T> operation = redisTemplate.opsForValue();
51return operation.get(key);
52 }
53
54/**
55 * 缓存List数据
56 * @param key 缓存的键值
57 * @param dataList 待缓存的List数据
58 * @return缓存的对象
59*/
60public <T> ListOperations<String, T> setCacheList(String key,List<T> dataList)
61 {
62 ListOperations listOperation = redisTemplate.opsForList();
63if(null != dataList)
64 {
65int size = dataList.size();
66for(int i = 0; i < size ; i ++)
67 {
68
69 listOperation.rightPush(key,dataList.get(i));
70 }
71 }
72
73return listOperation;
74 }
75
76/**
77 * 获得缓存的list对象
78 * @param key 缓存的键值
79 * @return缓存键值对应的数据
80*/
81public <T> List<T> getCacheList(String key)
82 {
83 List<T> dataList = new ArrayList<T>();
84 ListOperations<String,T> listOperation = redisTemplate.opsForList();
85 Long size = listOperation.size(key);
86
87for(int i = 0 ; i < size ; i ++)
88 {
89 dataList.add((T) listOperation.leftPop(key));
90 }
91
92return dataList;
93 }
94
95/**
96 * 缓存Set
97 * @param key 缓存键值
98 * @param dataSet 缓存的数据
99 * @return缓存数据的对象
100*/
101public <T> BoundSetOperations<String,T> setCacheSet(String key,Set<T> dataSet) 102 {
103 BoundSetOperations<String,T> setOperation = redisTemplate.boundSetOps(key); 104/*T[] t = (T[]) dataSet.toArray();
105 setOperation.add(t);*/
106
107 Iterator<T> it = dataSet.iterator();
108while(it.hasNext())
109 {
110 setOperation.add(it.next());
111 }
112
113return setOperation;
114 }
115
116/**
117 * 获得缓存的set
118 * @param key
119 * @param operation
120 * @return
121*/
122public Set<T> getCacheSet(String key/*,BoundSetOperations<String,T> operation*/) 123 {
124 Set<T> dataSet = new HashSet<T>();
124 Set<T> dataSet = new HashSet<T>();
125 BoundSetOperations<String,T> operation = redisTemplate.boundSetOps(key);
126
127 Long size = operation.size();
128for(int i = 0 ; i < size ; i++)
129 {
130 dataSet.add(operation.pop());
131 }
132return dataSet;
133 }
134
135/**
136 * 缓存Map
137 * @param key
138 * @param dataMap
139 * @return
140*/
141public <T> HashOperations<String,String,T> setCacheMap(String key,Map<String,T> dataMap)
142 {
143
144 HashOperations hashOperations = redisTemplate.opsForHash();
145if(null != dataMap)
146 {
147
148for (Map.Entry<String, T> entry : dataMap.entrySet()) {
149
150/*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
151 hashOperations.put(key,entry.getKey(),entry.getValue());
152 }
153
154 }
155
156return hashOperations;
157 }
158
159/**
160 * 获得缓存的Map
161 * @param key
162 * @param hashOperation
163 * @return
164*/
165public <T> Map<String,T> getCacheMap(String key/*,HashOperations<String,String,T> hashOperation*/) 166 {
167 Map<String, T> map = redisTemplate.opsForHash().entries(key);
168/*Map<String, T> map = hashOperation.entries(key);*/
169return map;
170 }
171
172/**
173 * 缓存Map
174 * @param key
175 * @param dataMap
176 * @return
177*/
178public <T> HashOperations<String,Integer,T> setCacheIntegerMap(String key,Map<Integer,T> dataMap) 179 {
180 HashOperations hashOperations = redisTemplate.opsForHash();
181if(null != dataMap)
182 {
183
184for (Map.Entry<Integer, T> entry : dataMap.entrySet()) {
185
186/*System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); */
187 hashOperations.put(key,entry.getKey(),entry.getValue());
188 }
189
190 }
191
192return hashOperations;
193 }
194
195/**
196 * 获得缓存的Map
197 * @param key
198 * @param hashOperation
199 * @return
7/*Map<String,Country> countryMap = redisCacheUtil1.getCacheMap("country");
8 Map<String,City> cityMap = redisCacheUtil.getCacheMap("city");*/
9 Map<Integer,Country> countryMap = redisCacheUtil1.getCacheIntegerMap("countryMap");
10 Map<Integer,City> cityMap = redisCacheUtil.getCacheIntegerMap("cityMap");
11
12for(int key : countryMap.keySet())
13 {
14 System.out.println("key = " + key + ",value=" + countryMap.get(key));
15 }
16
17 System.out.println("------------city");
18for(int key : cityMap.keySet())
19 {
20 System.out.println("key = " + key + ",value=" + cityMap.get(key));
21 }
22 }
由于Spring在配置⽂件中配置的bean默认是单例的,所以只需要通过Autowired注⼊,即可得到原先的缓存类。

相关文档
最新文档