/** * Alipay.com Inc. * Copyright (c) 2004-2018 All Rights Reserved. */ package com.weigu.serviceimpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.*; import org.springframework.stereotype.Service; import java.util.*; /** * @author 886644 * @version $Id: RedisUtil.java, v 0.1 2018/9/5 16:35 886644 Exp $ * */ @Service public class RedisUtil<T> { @Autowired private RedisTemplate redisTemplate; /** * 缓存基本的对象,Integer String,实体类对象等 * * @param key 缓存的键值 * @param value 缓存的值 * @param <T> * @return 缓存的对象 */ public <T> ValueOperations<String, T> setCatcheObject(String key, T value) { System.out.println(key + "*****" + value.toString()); ValueOperations<String, T> operation = redisTemplate.opsForValue(); operation.set(key, value); return operation; } /** * 获得缓存的基本的对象 * * @param key 缓存的键值; * @param <T> * @return 缓存的键值对应的值 */ public <T> T getCacheObject(String key) { return (T) redisTemplate.opsForValue().get(key); } /** * 缓存list数据 * * @param key 缓存的键值 * @param dataList 缓存的键值对应的list * @param <T> * @return 返回list值 */ public <T> ListOperations<String, T> setCatcheList(String key, List<T> dataList) { ListOperations<String, T> listOperations = redisTemplate.opsForList(); if (null != listOperations) { int size = dataList.size(); for (int i = 0; i < size; i++) { listOperations.rightPush(key, dataList.get(i)); } } return listOperations; } /** * 获取缓存的list对象 * * @param key key缓存的键值 * @param <T> 缓存键值对应的数据 * @return */ public <T> List<T> getCatcheList(String key) { List<T> dataList = new ArrayList<>(); ListOperations listOperations = redisTemplate.opsForList(); Long size = listOperations.size(key); for (int i = 0; i < size; i++) { dataList.add((T) listOperations.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 * @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 * @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 * @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; } }
监听器
/** * Alipay.com Inc. * Copyright (c) 2004-2018 All Rights Reserved. */ package com.weigu.serviceimpl; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Service; import org.apache.log4j.Logger; import java.util.ArrayList; import java.util.List; /** * @author 886644 * @version $Id: StartAddCacheListener.java, v 0.1 2018/9/5 16:31 886644 Exp $ */ /** * 监听器,用于项目启动时初始化信息 */ @Service public class StartAddCacheListener implements ApplicationListener<ContextRefreshedEvent> { //日志 private final Logger log = Logger.getLogger(StartAddCacheListener.class); @Autowired private RedisUtil<Object> redisCache; @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { redisCache.setCatcheObject("dataList", "Hello_world"); // test1(); } public void test() { String dataList = redisCache.getCacheObject("dataList"); log.info(dataList + "****************************************"); } public void test1() { ArrayList<String> a = new ArrayList<>(); a.add("1111"); a.add("222"); a.add("333"); redisCache.setCatcheObject("datalist", a); List<String> datalist = redisCache.getCatcheList("datalist"); if (datalist == null && datalist.size() < 0) { } System.out.println(datalist + "redis中的数据为"); } }