java封装redis工具类

1.spring整合redis中的配置

     
     
     
         
             
                 
             
         
         
       
           
             
                 
                 
             
           
         
     
         
maxTotal=50
redis.note1.host=127.0.01
redis.note1.port=6379


2.java中的redis类工具类

package com.cib.occs.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cib.occs.base.Function;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Service
public class RedisService {
	@Autowired
	private ShardedJedisPool  shardedJedisPool;
	
	public T excute(Function fun){
		ShardedJedis jedisInfo=null;
		try{
			jedisInfo=shardedJedisPool.getResource();
			return fun.callBack(jedisInfo);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(jedisInfo!=null){
				jedisInfo.close();
			}
			
		}
		return null;
	}
	/**
	 * 获取值
	 * @param key
	 * @return
	 */
	public String get(final String key){
		return this.excute(new Function(){
			@Override
			public String callBack(ShardedJedis e) {
			    return e.get(key);
				
			}
			
		});
	}
	/**
         * 获取值
         * @param key
         * @return
         */
        public Set hvals(){
                return this.excute(new Function,ShardedJedis>(){
                        @Override
                        public  Set callBack(ShardedJedis e) {
                            return e.hkeys("1*");
                        }
                        
                });
        }
	
	/**
	 * 设置值
	 * @param key
	 * @param value
	 * @return
	 */
	public String set(final String key,final String value){
		return this.excute(new Function(){
			@Override
			public String callBack(ShardedJedis e) {
				return e.set(key, value);
			}
			
		});
	}
	
	
	public String set(final String key,final Maphash){
            return this.excute(new Function(){
                    @Override
                    public String callBack(ShardedJedis e) {
                            return e.hmset(key, hash);
                    }
                    
            });
    }
    	       public List get(final String key,final String fields){
    	            return this.excute(new Function ,ShardedJedis>(){
    	                    @Override
    	                    public List  callBack(ShardedJedis e) {
    	                            return e.hmget(key, fields);
    	                    }
    	                    
    	            });
    	    }
	    
    
	/**
         * 设置值
         * @param key
         * @param value
         * @return
         */
        public String set(final byte[] key,final byte[]value){
                return this.excute(new Function(){
                        @Override
                        public String callBack(ShardedJedis e) {
                                return e.set(key, value);
                        }
                        
                });
        }
        
        /**
         * 通过字节获取key
         * @param key
         * @param value
         * @return
         */
        public byte[] get(final byte[] key){
                return this.excute(new Function(){
                        @Override
                        public byte[] callBack(ShardedJedis e) {
                                return e.get(key);
                        }
                        
                });
        }
	/**
	 * 删除
	 * @param key
	 * @return
	 */
	public Long decr(final String key){
		return this.excute(new Function(){
			@Override
			public Long callBack(ShardedJedis e) {
				return e.decr(key);
			}
			
		});
	}
	/**
         * 删除
         * @param key
         * @return
         */
        public Long del(final String key){
                return this.excute(new Function(){
                        @Override
                        public Long callBack(ShardedJedis e) {
                                return e.del(key);
                        }
                        
                });
        }
	/**
	 * 设置时间
	 * @param key
	 * @param seconds
	 * @return
	 */
   public Long expir(final String key,final Integer seconds){
	   return this.excute(new Function(){
		@Override
		public Long callBack(ShardedJedis e) {
			return e.expire(key, seconds);
		}
	   });
   }
   
	/**
	 * 插入的同时设置时间
	 * @param key
	 * @param seconds
	 * @return
	 */
   public String  setAndExpir(final String key,final String value,final Integer seconds){
	   return this.excute(new Function(){
		@Override
		public String callBack(ShardedJedis e) {
			String str=e.set(key, value);
			e.expire(key, seconds);
			return str;
		}
	   });
   }
   
   public static byte[]serialize(Object object){
       ObjectOutputStream oos=null;
       ByteArrayOutputStream baos=null;
       try{
           baos=new ByteArrayOutputStream();
           oos=new ObjectOutputStream(baos);
           oos.writeObject(object);
           byte[]bytes=baos.toByteArray();
           return bytes;
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           try {  
               if(oos!=null){
                   oos.close();
               }
               if(baos!=null){
                   baos.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return null;
   }
   
   public static Object unserialize(byte[]bytes){
       ByteArrayInputStream bais=null;
       ObjectInputStream ois =null;
       try{
           bais=new ByteArrayInputStream(bytes);
            ois =new ObjectInputStream(bais);
           return ois.readObject();
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           try {
               if(bais!=null){
                   bais.close();
               }
               if(ois!=null){
                   ois.close();
               }
           
        } catch (IOException e) {
            e.printStackTrace();
        }
       }
       return null;
   }
   
   
}

3.Function类

package com.cib.occs.base;


public interface Function {

	  public T callBack(E e);
}











你可能感兴趣的:(java中的中间件)