redisデータ構造キャッシュ運用


以前redisはredisの基本的な役割と用途を説明したが,この1編は主にredisの運用シーンとスライス,spring統合について述べた.redisはデータ構造を約5種類格納し,Stringは通常キー値対で,比較的多く用いられる.HASHはkeyに対してhashmapキー値対運用も比較的多くlist setはもちろん集合運用sortedSetソート集合で使用する.
まずredisとspringの構成統合を見てみましょう
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="2" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:demo-provider-module.properties</value>
            </list>
        </property>
    </bean>
    <!-- redis       -->
     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
       <property name="maxTotal" value="2048" />  
       <property name="maxIdle" value="200" />  
       <property name="numTestsPerEvictionRun" value="1024"/>  
       <property name="timeBetweenEvictionRunsMillis" value="30000" />  
       <property name="minEvictableIdleTimeMillis" value="-1" />  
       <property name="softMinEvictableIdleTimeMillis" value="10000" />  
       <property name="maxWaitMillis" value="1500"/>  
       <property name="testOnBorrow" value="true" />  
       <property name="testWhileIdle" value="true"/>  
       <property name="testOnReturn" value="false"/>  
       <property name="jmxEnabled" value="true"/>  
       <property name="jmxNamePrefix" value="youyuan"/>  
       <property name="blockWhenExhausted" value="false"/>  
   </bean>  
     <!-- redis    pool,     :timeout/password  -->
    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">  
       <constructor-arg index="0" ref="jedisPoolConfig"/>  
       <constructor-arg index="1">  
           <list>  
                <bean name="master" class="redis.clients.jedis.JedisShardInfo">  
                   <constructor-arg index="0" value="${redis1.server.host}"/>  
                   <constructor-arg index="1" value="${redis1.server.port}" type="int"/><!--               -->
                   <constructor-arg index="2" value="${redis1.server.timeout}" type="int"/>  
               </bean>  
           </list>  
       </constructor-arg>  
   </bean>  
</beans>

開発中にredisサーバがlistの下に注入されるのは一般的に1台だけではないかもしれません.
多くの場合、私たちがコードを書くときにデータ構造の運用が適切であることを信じて、多くのことを審査して、多くの問題を解決して、redisも同じです.よく使うredis使用ツールクラスを自分でカプセル化しました
package demo.dcn.cache;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.protobuf.Internal.ProtobufList;
/**
 * Redis           
 * @author [email protected]
 */
public class JedisUtil {

	/**
	 *       json   
	 * @param object
	 * @return
	 */
	public static String toJson(Object object){
		return JSON.toJSONString(object);
	}
	/**
	 *    T          Class<T>           
	 *   JSON  parse JavaBean 
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> T toObject(String json,Class<T> clazz){
		return JSON.parseObject(json, clazz);
	}
	
	/**
	 *       
     *
	 * @param json
	 * @param typeReference
	 * @return
	 */
	public static <T> T toObject(String json,TypeReference<T> typeReference){
		return JSON.parseObject(json, typeReference);
	}	
	/**
	 *  json            
	 * @param json
	 * @param T
	 * @return
	 */
	public static <T> List<T> parseArray(String json,Class<T> T){
		return JSON.parseArray(json, T);
	}
	/**
	 *     json          
	 * @param jsons
	 * @param clazz
	 * @return
	 */
	public static <T> List<T> toObjects(List<String> jsons, Class<T> clazz){
		List<T> list = new ArrayList<T>();
		for (String json : jsons) {
			T obj = toObject(json, clazz);
            if(null != obj) {
            	list.add(obj);
            }
		}
		return list;
	}
	
	/**
	 *   JSON  parse JSONObject javamap  JSONArray Javalist
	 * @param json
	 * @return
	 */
	public static Object parseObject(String json){
		return JSON.parse(json);
	}
	 /**
	  *  JSON  parse JSONObject    
	  * @param json
	  * @return
	  */
	public static JSONObject parJsonObject(String json){
		return JSONObject.parseObject(json);
	}
	
	/**
	 *  JSON  parse JSONArray 
	 * @param text
	 * @return
	 */
	public static final JSONArray parseArray(String text){
		return JSONArray.parseArray(text);
	}
	 /**
	  *   JavaBean        JSON   
	  * @param object
	  * @param prettyFormat
	  * @return
	  */
	public static final String toJSONString(Object object, boolean prettyFormat){
		return JSON.toJSONString(object, prettyFormat);
	}
	
	/**
	 *   JavaBean   JSONObject  JSONArray
	 * @param javaObject
	 * @return
	 */
	public static final Object toJSON(Object javaObject){
		return JSON.toJSON(javaObject);
	}

}
package demo.dcn.cache;

import redis.clients.jedis.ShardedJedis;

/**
 *      jedis
 * @author [email protected]
 */
public class SelfShardedJedis {

	private ShardedJedis shardedJedis;//  
	
	private Boolean isReturn;//       
	
	public SelfShardedJedis(ShardedJedis shardedJedis, Boolean isReturn) {
		super();
		this.shardedJedis = shardedJedis;
		this.isReturn = isReturn;
	}

	public SelfShardedJedis() {
		super();
	}
	/**
	 * @return the shardedJedis
	 */
	public ShardedJedis getShardedJedis() {
		return shardedJedis;
	}

	/**
	 * @param shardedJedis the shardedJedis to set
	 */
	public void setShardedJedis(ShardedJedis shardedJedis) {
		this.shardedJedis = shardedJedis;
	}

	/**
	 * @return the isReturn
	 */
	public Boolean getIsReturn() {
		return isReturn;
	}

	/**
	 * @param isReturn the isReturn to set
	 */
	public void setIsReturn(Boolean isReturn) {
		this.isReturn = isReturn;
	}
}
package demo.dcn.cache;

import java.util.Set;

import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class RedisClientBase implements RedisClient {

	private final Logger logger = LoggerFactory.getLogger(RedisClientBase.class);
	private static final String NX ="NX";//redis   key        
	private static final String XX = "XX";//redis   key   ,        
	private static final String EX ="EX";//redis         
	private static final String PX ="PX";//redis          
	
	@Resource
	private ShardedJedisPool shardedJedisPool;//   
	
	 public static final int MAX_SCAN_COUNT = 200;
	
	 private static RedisClient	redisclient;

	public RedisClientBase(ShardedJedisPool shardedJedisPool) {
		super();
		this.shardedJedisPool = shardedJedisPool;
	}
	public RedisClientBase() {
		redisclient = this;
	}
	public static RedisClient getInstance() {
        return redisclient;
    }
	 /**
     *        jedis    
     * 
     * @return
     */
	public SelfShardedJedis getShardedJedis(){
		ShardedJedis shards = shardedJedisPool.getResource();
		SelfShardedJedis selfShardedJedis = new SelfShardedJedis(shards, false);
		return selfShardedJedis;
	}
	
	/**
	 *    jedis    
	 * @param jedis
	 * @param broken
	 */
	@SuppressWarnings({ "null", "deprecation" })
	public void closeRedisThread(SelfShardedJedis jedis, boolean broken){
		try{
			if(jedis!=null||jedis.getIsReturn()){
				return;
			}
			if(broken){
				shardedJedisPool.returnBrokenResource(jedis.getShardedJedis());
				jedis.setIsReturn(true);
			}else{
				shardedJedisPool.returnResource(jedis.getShardedJedis());
				jedis.setIsReturn(true);
			}
		}catch(Exception e){
			logger.error(e.getMessage(),e);
		}
		
	}
	
	/**setnx      value ,     key    。      key     ,  setNotExists       。
	 *   
	 * @param key
	 * @param obj
	 * @return
	 *     ,   1 ;    ,   0 。
	 */
	public Long setNotExists(String key,byte[] obj){
		SelfShardedJedis shardedJedis = null;
		boolean broken = false;
		try{
			shardedJedis = getShardedJedis();
			return shardedJedis.getShardedJedis().setnx(key.getBytes(), obj);//      
		}catch(Exception e){
			  logger.error(e.getMessage(), e);
	          broken = true;
		}finally{
			closeRedisThread(shardedJedis, broken);
		}
		return 0L;
	}
	/**
	 *   key      value ,       ,     key    。      key     ,  setNotExists       。 * Set the string value as value of the key. The string
     * can't be longer than 1073741824 bytes (1 GB).
	 * @param key
	 * @param value
	 * @param nxxx
	 * @param expx
	 * @param time
	 * @return
	 */
	public String set(String key,String value, String nxxx, String expx, long time){
		SelfShardedJedis shardedJedis = null;
		boolean broken = false;
		try{
			shardedJedis = getShardedJedis();
			return shardedJedis.getShardedJedis().set(key, value, nxxx, expx, time);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
            broken = true;
        } finally {
            closeRedisThread(shardedJedis, broken);
        }
        return null;
	}
	
	/**
	 *   key        
	 * @param key
	 * @return
	 */
	public String getString(String key){
		 SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try{
	        	shardedJedis = getShardedJedis();
	        	return shardedJedis.getShardedJedis().get(key);
	        }catch(Exception e){
	        	logger.error(e.getMessage(), e);
	            broken = true;
	     }finally {
	            closeRedisThread(shardedJedis, broken);
	        }
			return null;
	}
	
	/**
	 *   key    
	 * @param key
	 * @param obj
	 * @param expireTime
	 * @return
	 */
	 public String setExpire(String key, byte[] obj, int expireTime) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            return shardedJedis.getShardedJedis().setex(key.getBytes(), expireTime, obj);
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	/**
	 *   key    
	 * @param key
	 * @param expireTime
	 * @return
	 */
	  public Long expire(String key, int expireTime) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            return shardedJedis.getShardedJedis().expire(key, expireTime);
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	  
	  /**
	     *       key  ,    (rank)        。
	     * <p/>
	     *           start   stop   ,   start   stop   。      start   stop    0   ,    ,  0           ,  1           ,    。           ,  -1
	     *         , -2          ,    
	     * 
	     * @param key
	     * @param start
	     * @param stop
	     * @return
	     */
	    public Long zremrangeByRank(String key, long start, long stop) {
	        long result = 0L;
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            result = shardedJedis.getShardedJedis().zremrangeByRank(key, start, start);
	        } catch (Exception e) {
	            broken = true;
	            logger.error(e.getMessage(), e);
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return result;
	    }
	    
	    /**
	     *       key  ,                  score    (    )   
	     * 
	     * @param key
	     * @param start
	     * @param stop
	     * @return
	     */
	    public Set<String> zrevrange(String key, long start, long stop) {
	        Set<String> result = null;
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            result = shardedJedis.getShardedJedis().zrevrange(key, start, start);
	        } catch (Exception e) {
	            broken = true;
	            logger.error(e.getMessage(), e);
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return result;
	    }
	

	    /**
	     *   key
	     * @param key
	     * @return
	     */
	    public Long del(String key) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            return shardedJedis.getShardedJedis().del(key);
	        } catch (Exception e) {
	            broken = true;
	            logger.error(e.getMessage(), e);
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	
	    /**
	     *    
	     * 
	     * @param key
	     * @param seconds       ,  : ;  0        。
	     * @param roll       
	     */
	    public Long incr(String key, int seconds, boolean roll) {
	        long b = 0;
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            b = shardedJedis.getShardedJedis().incr(key.getBytes());
	            if (seconds > 0) {
	                if (b == 1) {//      ,      
	                    shardedJedis.getShardedJedis().expire(key.getBytes(), seconds);
	                }
	                if (roll && b > 1) {//     
	                    shardedJedis.getShardedJedis().expire(key.getBytes(), seconds);
	                }
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            if (broken && shardedJedis != null) {
	                shardedJedis.getShardedJedis().del(key);
	            }
	            closeRedisThread(shardedJedis, broken);
	        }
	        return b;
	    }
	/**
	 *   key     
	 * @param key
	 * @return  
	 */
	   public Boolean exists(String key) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            return shardedJedis.getShardedJedis().exists(key.getBytes());
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return false;
	    }
	   
	   
	   /**
	    *   key    jedis
	    * @param key
	    * @return
	    */
	   
	   public Jedis getShard(String key) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().getShard(key);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    
	   /**
	     *   JedisShardInfo
	     * 
	     * @param key
	     * @return JedisShareInfo
	     */
	    public JedisShardInfo getShardInfo(String key) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().getShardInfo(key);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    /**
	     *         
	     * @param key
	     * @param value
	     * @return
	     */
	    public String set(String key, String value) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().set(key, value);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    /**
	     *   key   
	     * @param key
	     * @return
	     */
	    public String type(String key) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().type(key);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }

	    /**
	     *   key         key     ,   -2 。
		 *   key               ,   -1 。
		 *   ,     ,   key        。
	     * @param key
	     * @return
	     */
	    public Long ttl(String key) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().ttl(key);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    
	    /**
	     *  key       ,     
	     *      key    。
	     *	  key      ,   , key     ,   nil 。            
	     * @param key
	     * @param value
	     * @return
	     */
	    public String getSet(String key, String value) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().getSet(key, value);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    /**
	     *  key         key String             0
	     * @param key
	     * @param value
	     * @return 
	     */
	    public Long setnx(String key, String value) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().setnx(key, value);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    /**
	     *   key     ,     
	     * @param key
	     * @param seconds
	     * @param value
	     * @return
	     */
	    public String setex(String key, int seconds, String value) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().setex(key, seconds, value);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    
	    /**
	     *     key        key      key      0     
	     * @param key
	     * @param integer
	     * @return
	     */
	    public Long decrBy(String key, long integer) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().decrBy(key, integer);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    /**
	     *   key         key       key     0 incr    1
	     * @param key
	     * @param integer
	     * @return
	     */
	   public Long incrBy(String key, long integer) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().incrBy(key, integer);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	  /**
	   *       key    ,        key    ,    key value
	   * @param key
	   * @param value
	   * @return
	   */
	   public Long append(String key, String value) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().append(key, value);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
	    
	   /**
	    *      
	    * @param key
	    * @param start
	    * @param end
	    * @return
	    */
	   public String substr(String key, int start, int end) {
	        SelfShardedJedis shardedJedis = null;
	        boolean broken = false;
	        try {
	            shardedJedis = getShardedJedis();
	            if (shardedJedis != null) {
	                return shardedJedis.getShardedJedis().substr(key, start, end);
	            }
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	            broken = true;
	        } finally {
	            closeRedisThread(shardedJedis, broken);
	        }
	        return null;
	    }
}

ここでは主にStringタイプのパッケージに対する方法であり,他のタイプはほぼ常に存在する.サイトと照らし合わせて理解できますが、
ここのjson出力は,シーケンス化に用いるアリババfastjsonであり,Google gsonでもよいが,protobufはシーケンス化が比較的速い.時間を見つけてやってみる.
http://redisdoc.com/index.html
既存
0人がメッセージを発表し、猛撃->
ここで<<-ディスカッションに参加
ITeye推奨
  • —ソフトウェア人材免言語低保証渡米有給大学院!—