Spring統合redis


1.pom.xml
 
        
            org.springframework.data
            spring-data-redis
            1.8.17.RELEASE
        
        
        
            redis.clients
            jedis
            2.9.1
        

2.resources.properties
#redis     IP
redis.host=xxx.xx.x.xxx
#redis      
redis.port=6379
#     
redis.maxIdle=300
#     
redis.maxActive=600
#      
redis.maxWait=100000
#               ,      ,                ,           
redis.testOnBorrow=true
#   return Object   ,         
redis.testOnReturn=true
#         , false   ,ture      ,   true
redis.blockWhenExhausted=false
#         (  )
redis.timeBetweenEvictionRunsMillis=300000
#           ,  3
redis.numTestsPerEvictionRun=1024
#        
redis.minEvictableIdleTimeMillis=1800000
#         ,   false
redis.testWhileIdle=true
#         ,      >         >             
redis.softMinEvictableIdleTimeMillis=10000
#    
redis.timeout=100000

#redis    RedisClusterConfiguration  
#                   
redis.maxRedirects=5
#      
#redis.host1=192.168.200.128
#redis.port1=7000
#redis.host2=192.168.200.128
#redis.port2=7001
#redis.host3=192.168.200.128
#redis.port3=7002
#redis.host4=192.168.200.128
#redis.port4=7003
#redis.host5=192.168.200.128
#redis.port5=7004
#redis.host6=192.168.200.128
#redis.port6=7005


3.applicationContext.xml



    
    

    
    
    
    
    
    
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
        
        
        
        
        
        
        
    

    
    

    
    

    
    
        
        
        
        
        
        
            
        
        
            
        
        
        
    





4.cacheモジュール、redisインタフェース

import java.util.*;
import java.util.concurrent.TimeUnit;


public interface CacheService {

    /**
     *     
     * @param key  
     * @param value  
     * @return      
     */
    boolean add(String key, Object value);

    /**
     *            
     * @param key  
     * @param value  
     * @param minute    (  )
     * @return      
     */
    boolean add(String key, Object value, int minute);

    /**
     *            
     * @param key  
     * @param value  
     * @param second    ( )
     * @return      
     */
    boolean addBySecond(String key, Object value, int second);

    /**
     *     (      )
     * @param key  
     * @param value  
     * @return      
     */
    boolean set(String key, Object value);

    /**
     *     (      )
     * @param key  
     * @param value  
     * @param minute    (  )
     * @return      
     */
    boolean set(String key, Object value, int minute);

    /**
     *       
     * @param key  
     * @param value  
     * @return      
     */
    boolean replace(String key, Object value);

    /**
     *       
     * @param key  
     * @param value  
     * @param minute    (  )
     * @return      
     */
    boolean replace(String key, Object value, int minute);

    /**
     *       
     * @param key  
     * @return  
     */
    Object get(String key);

    /**
     *       
     * @param key  
     * @return      
     */
    boolean delete(String key);

    /**
     *     key
     *
     * @param keys
     */
    void delete(String... keys);

    /**
     *   Key   
     *
     * @param keys
     */
    void delete(Collection keys);

    /**
     *        
     * @param key  
     * @return     
     */
    boolean containsKey(String key);

    /**
     *   key     
     *
     * @param key
     * @param time    
     */
    Boolean expire(String key, long time);

    /**
     *   key        
     *
     * @param key
     * @param date
     */
    Boolean expireAt(String key, Date date);

    /**
     *   key     
     *
     * @param key
     * @param timeUnit
     * @return
     */
    long getExpire(String key, TimeUnit timeUnit);

    /**
     *  key       
     *
     * @param key
     */
    Boolean persistKey(String key);

    /**
     *   
     *
     * @param key  
     * @param delta      (  0)
     * @return
     */
    Long increment(String key, long delta);

    /**
     *   
     *
     * @param key    
     * @param delta     (  0)
     * @return
     */
    Long decrement(String key, long delta);

    // ================================Map=================================

    /**
     * HashGet
     *
     * @param key       null
     * @param item      null
     * @return  
     */
    Object hget(String key, String item);

    /**
     *   hashKey       
     *
     * @param key  
     * @return        
     */
    Map hmget(String key);

    /**
     * HashSet
     *
     * @param key  
     * @param map       
     * @return true    false   
     */
    boolean hmset(String key, Map map);

    /**
     * HashSet      
     *
     * @param key   
     * @param map        
     * @param time   ( )
     * @return true   false  
     */
    boolean hmset(String key, Map map, long time);

    /**
     *    hash      ,        
     *
     * @param key    
     * @param item   
     * @param value  
     * @return true    false  
     */
    boolean hset(String key, String item, Object value);

    /**
     *    hash      ,        
     *
     * @param key    
     * @param item   
     * @param value  
     * @param time    ( )   :      hash    ,           
     * @return true    false  
     */
    boolean hset(String key, String item, Object value, long time);

    /**
     *   hash    
     *
     * @param key       null
     * @param item            null
     */
    void hdel(String key, Object... item);

    /**
     *   hash         
     *
     * @param key       null
     * @param item      null
     * @return true    false   
     */
    boolean hHasItemByKey(String key, String item);

    /**
     * hash        ,                
     *
     * @param key   
     * @param item  
     * @param by       (  0)
     * @return
     */
    Double hincrement(String key, String item, double by);

    /**
     * hash  
     *
     * @param key   
     * @param item  
     * @param by       (  0)
     * @return
     */
    Double hdecrement(String key, String item, double by);

    // ============================set=============================

    /**
     *   key  Set     
     *
     * @param key  
     * @return
     */
    Set sGet(String key);

    /**
     *   value   set   ,    
     *
     * @param key    
     * @param value  
     * @return true    false   
     */
    Boolean sHasKey(String key, Object value);

    /**
     *      set  
     *
     * @param key     
     * @param values        
     * @return     
     */
    Long sSet(String key, Object... values);

    /**
     *  set      
     *
     * @param key     
     * @param time     ( )
     * @param values        
     * @return     
     */
    Long sSetAndTime(String key, long time, Object... values);

    /**
     *   set     
     *
     * @param key  
     * @return
     */
    Long sGetSetSize(String key);

    /**
     *     value 
     *
     * @param key     
     * @param values        
     * @return      
     */
    Long setRemove(String key, Object... values);

    // ===============================list=================================

    /**
     *   list     
     *
     * @param key    
     * @param start   
     * @param end      0   -1     
     * @return
     */
    List lGet(String key, long start, long end);

    /**
     *   list     
     *
     * @param key  
     * @return
     */
    Long lGetListSize(String key);

    /**
     *        list   
     *
     * @param key    
     * @param index    index>=0 , 0   ,1      ,    ;index<0 ,-1,  ,-2       ,    
     * @return
     */
    Object lGetIndex(String key, long index);

    /**
     *  list    
     *
     * @param key    
     * @param value  
     * @return
     */
    boolean lSet(String key, Object value);

    /**
     *  list    
     *
     * @param key    
     * @param value  
     * @param time    ( )
     * @return
     */
    boolean lSet(String key, Object value, long time);

    /**
     *  list    
     *
     * @param key    
     * @param value  
     * @return
     */
    boolean lSet(String key, List value);

    /**
     *  list    
     *
     * @param key    
     * @param value  
     * @param time    ( )
     * @return
     */
    boolean lSet(String key, List value, long time);

    /**
     *       list      
     *
     * @param key    
     * @param index   
     * @param value  
     * @return
     */
    boolean lUpdateIndex(String key, long index, Object value);

    /**
     *   N   value
     *
     * @param key    
     * @param count      
     * @param value  
     * @return      
     */
    Long lRemove(String key, long count, Object value);

}


5.cacheモジュール、redis操作実装クラス

import com.alibaba.dubbo.config.annotation.Service;
import com.summersoft.ts.cache.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;


@Service
public class CacheServiceImpl implements CacheService {

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public boolean add(String key, Object value) {
        try {
            if (!this.containsKey(key)) {
                redisTemplate.opsForValue().set(key, value);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean add(String key, Object value, int minute) {
        try {
            if (!this.containsKey(key)) {
                if (minute > 0) {
                    redisTemplate.opsForValue().set(key, value, minute, TimeUnit.MINUTES);
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean addBySecond(String key, Object value, int second) {
        try {
            if (!this.containsKey(key)) {
                if (second > 0) {
                    redisTemplate.opsForValue().set(key, value, second, TimeUnit.SECONDS);
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean set(String key, Object value, int minute) {
        try {
            if (minute > 0) {
                redisTemplate.opsForValue().set(key, value, minute, TimeUnit.MINUTES);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean replace(String key, Object value) {
        try {
            if (this.containsKey(key)) {
                redisTemplate.opsForValue().set(key, value);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean replace(String key, Object value, int minute) {
        try {
            if (this.containsKey(key)) {
                if (minute > 0) {
                    redisTemplate.opsForValue().set(key, value, minute, TimeUnit.MINUTES);
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Object get(String key) {
        try {
            return key == null ? null : redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean delete(String key) {
        try {
            if (this.containsKey(key)) {
                 redisTemplate.delete(key);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void delete(String... keys) {
        try {
            redisTemplate.delete(CollectionUtils.arrayToList(keys));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Collection keys) {
        try {
            redisTemplate.delete(keys);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean containsKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean expire(String key, long time) {
        try {
            return redisTemplate.expire(key, time, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean expireAt(String key, Date date) {
        try {
            return redisTemplate.expireAt(key, date);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public long getExpire(String key, TimeUnit timeUnit) {
        try {
            return redisTemplate.getExpire(key, timeUnit);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Boolean persistKey(String key) {
        try {
            return redisTemplate.persist(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Long increment(String key, long delta) {
        try {
            if (delta < 0) {
                throw new RuntimeException("        0");
            }
            return redisTemplate.opsForValue().increment(key, delta);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Long decrement(String key, long delta) {
        try {
            if (delta < 0) {
                throw new RuntimeException("        0");
            }
            return redisTemplate.opsForValue().increment(key, -delta);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return 0L;
        }
    }

    // ================================Map=================================

    @Override
    public Object hget(String key, String item) {
        try {
            return redisTemplate.opsForHash().get(key, item);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Map hmget(String key) {
        try {
            return redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean hmset(String key, Map map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean hmset(String key, Map map, long time) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            if (time > 0) {
                this.expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean hset(String key, String item, Object value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean hset(String key, String item, Object value, long time) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void hdel(String key, Object... item) {
        try {
            redisTemplate.opsForHash().delete(key, item);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean hHasItemByKey(String key, String item) {
        try {
            return redisTemplate.opsForHash().hasKey(key, item);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Double hincrement(String key, String item, double by) {
        try {
            return redisTemplate.opsForHash().increment(key, item, by);
        } catch (Exception e) {
            e.printStackTrace();
            return 0D;
        }
    }

    @Override
    public Double hdecrement(String key, String item, double by) {
        try {
            return redisTemplate.opsForHash().increment(key, item, -by);
        } catch (Exception e) {
            e.printStackTrace();
            return 0D;
        }
    }

    // ============================set=============================

    @Override
    public Set sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean sHasKey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Long sSet(String key, Object... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Long sSetAndTime(String key, long time, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().add(key, values);
            if (time > 0) {
                expire(key, time);
            }
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Long sGetSetSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Long setRemove(String key, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    // ===============================list=================================

    @Override
    public List lGet(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Long lGetListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public Object lGetIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean lSet(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean lSet(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean lSet(String key, List value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean lSet(String key, List value, long time) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0){
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean lUpdateIndex(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Long lRemove(String key, long count, Object value) {
        try {
            return redisTemplate.opsForList().remove(key, count, value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }
}