Spring集成Redis

1、Redis的安装

安装部分就不过多介绍了,简单写下。

下载地址:https://github.com/MSOpenTech/redis/releases

下载解压运行即可。

2、Spring-redis.xml



	
		
		
		
		
	
	
		
		
		
		
		
	
	
		
		
			
		
		
			
		
	

3、RedisCacheService

@Service("redis")
@SuppressWarnings({"unchecked","hiding","rawtypes"})
public class RedisCacheService {
	@Autowired
	@Qualifier("jedisTemplate")
	public RedisTemplate redisTemplate;
	
	/**
	 * 删除key
	 * @param key
	 */
	public void delete(String key) {
		redisTemplate.delete(key);
	}
	

	/**
	 * 缓存基本的对象,Integer、String、实体类等
	 * 
	 * @param key   缓存的键值
	 * @param value 缓存的值
	 * @return 缓存的对象
	 */
	public  ValueOperations setCacheObject(String key, T value) {
		ValueOperations operation = redisTemplate.opsForValue();
		operation.set(key, value);
		return operation;
	}

	/**
	 * 获得缓存的基本对象。
	 * 
	 * @param key       缓存键值
	 * @param operation
	 * @return 缓存键值对应的数据
	 */
	public  T getCacheObject(String key) {
		ValueOperations operation = redisTemplate.opsForValue();
		return operation.get(key);
	}

	/**
	 * 缓存List数据
	 * 
	 * @param key      缓存的键值
	 * @param dataList 待缓存的List数据
	 * @return 缓存的对象
	 */
	public  ListOperations setCacheList(String key, List 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  List getCacheList(String key) {
		List dataList = new ArrayList();
		ListOperations 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  BoundSetOperations setCacheSet(String key, Set dataSet) {
		BoundSetOperations setOperation = redisTemplate.boundSetOps(key);
		Iterator it = dataSet.iterator();
		while (it.hasNext()) {
			setOperation.add(it.next());
		}
		return setOperation;
	}

	/**
	 * 获得缓存的set
	 * 
	 * @param key
	 * @param operation
	 * @return
	 */
	public Set getCacheSet(String key) {
		Set dataSet = new HashSet();
		BoundSetOperations 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  HashOperations setCacheMap(String key, Map dataMap) {
		HashOperations hashOperations = redisTemplate.opsForHash();
		if (null != dataMap) {
			for (Map.Entry entry : dataMap.entrySet()) {
				hashOperations.put(key, entry.getKey(), entry.getValue());
			}
		}
		return hashOperations;
	}

	/**
	 * 获得缓存的Map
	 * 
	 * @param key
	 * @param hashOperation
	 * @return
	 */
	public  Map getCacheMap(String key) {
		Map map = redisTemplate.opsForHash().entries(key);
		return map;
	}

	/**
	 * 缓存Map
	 * 
	 * @param key
	 * @param dataMap
	 * @return
	 */
	public  HashOperations setCacheIntegerMap(String key, Map dataMap) {
		HashOperations hashOperations = redisTemplate.opsForHash();
		if (null != dataMap) {
			for (Map.Entry entry : dataMap.entrySet()) {
				hashOperations.put(key, entry.getKey(), entry.getValue());
			}

		}

		return hashOperations;
	}

	/**
	 * 获得缓存的Map
	 * 
	 * @param key
	 * @param hashOperation
	 * @return
	 */
	public  Map getCacheIntegerMap(String key) {
		Map map = redisTemplate.opsForHash().entries(key);
		return map;
	}
}

4、使用

通过注解注入使用

@Resource(name = "redis")
private RedisCacheService redis;

你可能感兴趣的:(操作使用)