当前位置 : 主页 > 编程语言 > java >

RedisCache工具类

来源:互联网 收集:自由互联 发布时间:2021-06-28
redis工具类 package com.yingjun.ssm.cache;import com.yingjun.ssm.util.ProtoStuffSerializerUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.dao.DataAccessException;import org.springframework.da
redis工具类
package com.yingjun.ssm.cache;

import com.yingjun.ssm.util.ProtoStuffSerializerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Component
public class RedisCache {
	
	public final static String CAHCENAME="cache";//缓存名
	public final static int CAHCETIME=60;//默认缓存时间

	@Autowired
	private RedisTemplate
 
   redisTemplate;

	public 
  
    boolean putCache(String key, T obj) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); boolean result = redisTemplate.execute(new RedisCallback
   
    () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(bkey, bvalue); } }); return result; } public 
    
      void putCacheWithExpireTime(String key, T obj, final long expireTime) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); redisTemplate.execute(new RedisCallback
     
      () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(bkey, expireTime, bvalue); return true; } }); } public 
      
        boolean putListCache(String key, List
       
         objList) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); boolean result = redisTemplate.execute(new RedisCallback
        
         () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(bkey, bvalue); } }); return result; } public 
         
           boolean putListCacheWithExpireTime(String key, List
          
            objList, final long expireTime) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); boolean result = redisTemplate.execute(new RedisCallback
           
            () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(bkey, expireTime, bvalue); return true; } }); return result; } public 
            
              T getCache(final String key, Class
             
               targetClass) { byte[] result = redisTemplate.execute(new RedisCallback
              
               () { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key.getBytes()); } }); if (result == null) { return null; } return ProtoStuffSerializerUtil.deserialize(result, targetClass); } public 
               
                 List
                
                  getListCache(final String key, Class
                 
                   targetClass) { byte[] result = redisTemplate.execute(new RedisCallback
                  
                   () { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key.getBytes()); } }); if (result == null) { return null; } return ProtoStuffSerializerUtil.deserializeList(result, targetClass); } /** * 精确删除key * * @param key */ public void deleteCache(String key) { redisTemplate.delete(key); } /** * 模糊删除key * * @param pattern */ public void deleteCacheWithPattern(String pattern) { Set
                   
                     keys = redisTemplate.keys(pattern); redisTemplate.delete(keys); } /** * 清空所有缓存 */ public void clearCache() { deleteCacheWithPattern(RedisCache.CAHCENAME+"|*"); } }
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
上一篇:网络工具类
下一篇:spring-aop-打印日志
网友评论