Spring 4.0.3 spring-data-redis jedisの統合

10369 ワード

最近、プロジェクトはキャッシュシステムとしてredisを必要とし、多くの穴を登った.
まずpom.xml



    org.springframework.data
    spring-data-redis
    1.4.2.RELEASE



    redis.clients
    jedis
    2.6.2

spring-redis.xml


    
    

    
    
    
    
        
        
        
        
    
    
    
        
        
        
        
    
    
    
        
        
        
            
        
        
            
        
    

config.properties

#redis       
redis.host=127.0.0.1
#redis    ,  6379
redis.port=6379
#redis    
redis.pass=
#     ,       maxIdle ,     
redis.maxIdle=100
#     ,       “      ”
redis.maxActive=300
#      :  ms
redis.maxWait=1000
#     ,        
redis.testOnBorrow=true
#               ,     0,       
redis.timeout=10000

RedisUtil.java
簡単な方法stringとlistしかできませんでした
public class RedisUtil {
    //https://blog.csdn.net/javaxiaibai0414/article/details/88666453
    private static RedisUtil redisUtil = new RedisUtil();
    private static RedisTemplate client = null;			//       ,          “RedisTemplate” 
    private static synchronized RedisTemplate getRedisUtil() {
        if(client == null){
            client = SpringContextHolder.getBean("redisTemplate");
        }
        return client;
    }

    private RedisUtil() {}
    /**
     *       .
     * @return
     */
    public static RedisUtil getInstance() {
        if(redisUtil == null){
            synchronized (RedisUtil.class) {
                redisUtil = new RedisUtil();
            }
        }
        return redisUtil;
    }


    //-------------------------------------------------
    //------------------        ----------------
    /**
     *         
     * @param key
     * @param time     
     * @return
     */
    public boolean expire(String key,long time){
        try{
            if(time>0){
                getRedisUtil().expire(key,time,TimeUnit.SECONDS);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     *   key      
     * @param key    null
     * @return       0      
     */
    public long getExpire(String key){
        return getRedisUtil().getExpire(key);
    }

    /**
     *   key    
     * @param key
     * @return
     */
    public boolean hasKey(String key){
        try{
            return getRedisUtil().hasKey(key);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     *     
     * @param key          
     */
    @SuppressWarnings("unchecked")
    public void del(String ... key){
        if(key!=null&&key.length>0){
            if (key.length ==1) {
                getRedisUtil().delete(key[0]);
            }else {
                getRedisUtil().delete(CollectionUtils.arrayToList(key));
            }
        }
    }


    //-------------------------------------------------
    //------------------String        ----------


    /**
     *     
     * @param key
     * @return
     */
    public Object get(String key) {
        return key==null?null:getRedisUtil().opsForValue().get(key);
    }

    /**
     *              (      )
     * @param key
     * @param value
     * @return T   F  
     */
    public boolean set(String key ,String value) {
        try{
            getRedisUtil().opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     *             --    token
     * @param key
     * @param value
     * @param time time   0          
     * @return
     */
    public boolean set(String key,String value,long time){
        try {
            if (time>0){
                getRedisUtil().opsForValue().set(key,value,time,TimeUnit.SECONDS);
            }else {
                set(key,value);
            }

            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     *   
     * @param key
     * @param delta
     * @return
     */
    public long incr(String key,long delta){
        if(delta<0){
            throw new RuntimeException("        0");
        }
        return getRedisUtil().opsForValue().increment(key,delta);
    }

    /**
     *   
     * @param key
     * @param delta
     * @return
     */
    public long decr(String key,long delta){
        if(delta<0){
            throw new RuntimeException("        0");
        }
        return getRedisUtil().opsForValue().increment(key,-delta);
    }

    //-------------------------------------------------
    //------------------LIST        ------------


    /**
     *   list     
     * @param key
     * @param start   
     * @param end    0 -1      
     * @return
     */
    public List lGet(String key,long start,long end){
        try{
            return getRedisUtil().opsForList().range(key,start,end);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     *   list     
     * @param key
     * @return
     */
    public long lGetListSize(String key){
        try {
            return getRedisUtil().opsForList().size(key);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    /**
     *        list   
     * @param key  
     * @param index     index>=0 , 0   ,1      ,    ;index<0 ,-1,  ,-2       ,    
     * @return
     */
    public Object lGetIndex(String key,long index){
        try {
            return getRedisUtil().opsForList().index(key,index);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     *  list          
     * @param key
     * @param value
     * @return
     */
    public boolean lSet(String key, Object value){
        try {
            getRedisUtil().opsForList().rightPush(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

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


    /**
     *  list          
     * @param key
     * @param value
     * @return
     */
    public boolean lSet(String key, List value){
        try {
            getRedisUtil().opsForList().rightPushAll(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

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



}

後記:spring-data-redisとjedisのバージョン(ここで使用しているspring-data-redis 1.4.2とjedis 2.62)に注意してください.この2つの商品のバージョンの要求は厳しすぎます.私のプロジェクトはspring 4を使用しています.5をアップグレードしたいと思っていましたが、エラーが多すぎて諦めました.spring 5をアップグレードした後、このバージョンの問題はずっとよくなります.
感謝:https://www.cnblogs.com/hjwublog/p/5749929.html