team470 发表于 2018-11-4 08:16:33

spring + redis 实现数据的缓存

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
  
{
  

  
    @Autowired @Qualifier("jedisTemplate")
  
    public RedisTemplate redisTemplate;
  

  
    /**
  
   * 缓存基本的对象,Integer、String、实体类等
  
   * @param key    缓存的键值
  
   * @param value    缓存的值
  
   * @return      缓存的对象
  
   */
  
    publicValueOperations setCacheObject(String key,T value)
  
    {
  

  
      ValueOperations operation = redisTemplate.opsForValue();
  
      operation.set(key,value);
  
      return operation;
  
    }
  

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

  
    /**
  
   * 缓存List数据
  
   * @param key      缓存的键值
  
   * @param dataList    待缓存的List数据
  
   * @return            缓存的对象
  
   */
  
    publicListOperations 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      缓存键值对应的数据
  
   */
  
    publicList 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            缓存数据的对象
  
   */
  
    publicBoundSetOperations setCacheSet(String key,Set dataSet)
  
    {
  
      BoundSetOperations setOperation = redisTemplate.boundSetOps(key);
  
      /*T[] t = (T[]) dataSet.toArray();
  
             setOperation.add(t);*/
  

  
      Iterator it = dataSet.iterator();
  
      while(it.hasNext())
  
      {
  
            setOperation.add(it.next());
  
      }
  

  
      return setOperation;
  
    }
  

  
    /**
  
   * 获得缓存的set
  
   * @param key
  
   * @param operation
  
   * @return
  
   */
  
    public Set getCacheSet(String key/*,BoundSetOperations operation*/)
  
    {
  
      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
  
   */
  
    publicHashOperations setCacheMap(String key,Map dataMap)
  
    {
  

  
      HashOperations hashOperations = redisTemplate.opsForHash();
  
      if(null != dataMap)
  
      {
  

  
            for (Map.Entry 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
  
   */
  
    publicMap getCacheMap(String key/*,HashOperations hashOperation*/)
  
    {
  
      Map map = redisTemplate.opsForHash().entries(key);
  
      /*Map map = hashOperation.entries(key);*/
  
      return map;
  
    }
  

  
    /**
  
   * 缓存Map
  
   * @param key
  
   * @param dataMap
  
   * @return
  
   */
  
    publicHashOperations setCacheIntegerMap(String key,Map dataMap)
  
    {
  
      HashOperations hashOperations = redisTemplate.opsForHash();
  
      if(null != dataMap)
  
      {
  

  
            for (Map.Entry 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
  
   */
  
    publicMap getCacheIntegerMap(String key/*,HashOperations hashOperation*/)
  
    {
  
      Map map = redisTemplate.opsForHash().entries(key);
  
      /*Map map = hashOperation.entries(key);*/
  
      return map;
  
    }
  
}


页: [1]
查看完整版本: spring + redis 实现数据的缓存