¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.redis; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.HashSet; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.TimeUnit; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | 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.ValueOperations; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * spring redis å·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | **/ |
| | | @SuppressWarnings(value = { "unchecked", "rawtypes" }) |
| | | @Component |
| | | public class RedisCache |
| | | { |
| | | @Autowired |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * ç¼ååºæ¬ç对象ï¼IntegerãStringãå®ä½ç±»ç |
| | | * |
| | | * @param key ç¼åçé®å¼ |
| | | * @param value ç¼åçå¼ |
| | | * @param timeout æ¶é´ |
| | | * @param timeUnit æ¶é´é¢ç²åº¦ |
| | | * @return ç¼åç对象 |
| | | */ |
| | | public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) |
| | | { |
| | | ValueOperations<String, T> operation = redisTemplate.opsForValue(); |
| | | operation.set(key, value, timeout, timeUnit); |
| | | return operation; |
| | | } |
| | | |
| | | /** |
| | | * è·å¾ç¼åçåºæ¬å¯¹è±¡ã |
| | | * |
| | | * @param key ç¼åé®å¼ |
| | | * @return ç¼åé®å¼å¯¹åºçæ°æ® |
| | | */ |
| | | public <T> T getCacheObject(String key) |
| | | { |
| | | ValueOperations<String, T> operation = redisTemplate.opsForValue(); |
| | | return operation.get(key); |
| | | } |
| | | |
| | | /** |
| | | * å é¤å个对象 |
| | | * |
| | | * @param key |
| | | */ |
| | | public void deleteObject(String key) |
| | | { |
| | | redisTemplate.delete(key); |
| | | } |
| | | |
| | | /** |
| | | * å é¤éå对象 |
| | | * |
| | | * @param collection |
| | | */ |
| | | public void deleteObject(Collection collection) |
| | | { |
| | | redisTemplate.delete(collection); |
| | | } |
| | | |
| | | /** |
| | | * ç¼å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.leftPush(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(listOperation.index(key, i)); |
| | | } |
| | | 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); |
| | | Iterator<T> it = dataSet.iterator(); |
| | | while (it.hasNext()) |
| | | { |
| | | setOperation.add(it.next()); |
| | | } |
| | | return setOperation; |
| | | } |
| | | |
| | | /** |
| | | * è·å¾ç¼åçset |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public <T> Set<T> getCacheSet(String key) |
| | | { |
| | | Set<T> dataSet = new HashSet<T>(); |
| | | BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key); |
| | | dataSet = operation.members(); |
| | | 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()) |
| | | { |
| | | hashOperations.put(key, entry.getKey(), entry.getValue()); |
| | | } |
| | | } |
| | | return hashOperations; |
| | | } |
| | | |
| | | /** |
| | | * è·å¾ç¼åçMap |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public <T> Map<String, T> getCacheMap(String key) |
| | | { |
| | | Map<String, T> map = redisTemplate.opsForHash().entries(key); |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * è·å¾ç¼åçåºæ¬å¯¹è±¡å表 |
| | | * |
| | | * @param pattern å符串åç¼ |
| | | * @return 对象å表 |
| | | */ |
| | | public Collection<String> keys(String pattern) |
| | | { |
| | | return redisTemplate.keys(pattern); |
| | | } |
| | | } |