zhaolu 发表于 2018-11-4 12:59:33

redis缓存

/**  
* RedisUnit
  
* @author dsine
  
*/
  
public class RedisUnit {
  /**
  * 服务地址
  */
  private String host;
  

  /**
  * 端口,默认端口是6379
  */
  private Integer port;
  /**
  * 密码
  */
  private String password = "123456";
  

  /**
  * 默认key前缀
  */
  private String defaultKeyPrefix = "";
  /**
  * 最小空闲连接数
  */
  private Integer minIdle = 5;
  /**
  * 最大空闲连接数
  */
  private Integer maxIdle = 10;
  /**
  * 最大连接数
  */
  private Integer maxTotal = 100;
  /**
  * 连接池
  */
  private JedisPool jedisPool;
  /**
  * 连接池配置,具体参数:
  * blockWhenExhausted = true;//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
  * evictionPolicyClassName = "org.apache.commons.pool2.impl.DefaultEvictionPolicy";//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
  * jmxEnabled = true;// 是否启用pool的jmx管理功能, 默认true
  * jmxNamePrefix = "pool";
  * lifo = true; //是否启用后进先出, 默认true
  * minIdle = 5;//最小空闲连接数, 默认0
  * maxIdle = 10;/最大空闲连接数, 默认8个
  * maxTotal = 100;//最大连接数, 默认8个
  * maxWaitMillis = 3000L;//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,默认-1
  * testOnBorrow = true;//在获取连接的时候检查有效性, 默认false
  * testOnReturn = true;//返回一个jedis实例给连接池时,是否检查连接可用性
  * testWhileIdle = true;//在空闲时检查有效性, 默认false
  * minEvictableIdleTimeMillis = 1800000L;//逐出连接的最小空闲毫秒,默认1800000毫秒(30分钟)
  * softMinEvictableIdleTimeMillis = 1800000L;//对象空闲多久后逐出, 当空闲时间>该值 ,且空闲连接>最大空闲数时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略),默认30m
  * timeBetweenEvictionRunsMillis = 60000L;//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
  * numTestsPerEvictionRun = 5;//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
  */
  private JedisPoolConfig jedisPoolConfig;
  /**
  *初始化启动,创建连接池
  *可以设置密码,防止外部操作
  */
  @PostConstruct
  public void init(){
  jedisPoolConfig = new JedisPoolConfig();
  jedisPoolConfig.setMinIdle(minIdle);
  jedisPoolConfig.setMaxIdle(maxIdle);
  jedisPoolConfig.setMaxTotal(maxTotal);
  if( password != null && !"".equals( password.trim() ) ){
  jedisPool = new JedisPool( jedisPoolConfig, this.host, this.port, Protocol.DEFAULT_TIMEOUT, this.password );
  }else{
  jedisPool = new JedisPool( jedisPoolConfig, this.host, this.port, Protocol.DEFAULT_TIMEOUT );
  }
  }
  @PreDestroy销毁
  public void destroy(){
  jedisPool.destroy();
  }
  private Jedis getJedis(){
  Jedis jedis = jedisPool.getResource();
  return jedis;
  }
  /**
  * 验证key是否为空
  */
  private void checkKeyNotBlank( String key ){
  if( key == null || "".equals( key ) ){
  throw new RuntimeException( "key is blank" );
  }
  }
  /**
  * set设置缓存
  */
  public void set( String keyPrefix, String key, Object value ){
  this.checkKeyNotBlank( key );
  this.getJedis().set( ObjectUtil.toBytes( keyPrefix + key ), ObjectUtil.toBytes( value ) );
  }
  /**
  * set设置缓存
  */
  public void set( String key, Object value ){
  this.set( defaultKeyPrefix, key, value );
  }
  /**
  * setNoPrefixToString
  */
  public void setNoPrefixToString(String key, String flag, String value ){
  this.checkKeyNotBlank( key );
  Logger.info("向缓存中添加标志位"+flag+"的缓存,其中KEY 是"+key+"_"+flag+"VALUE 是"+value);
  this.getJedis().set(key+"_"+flag, value);
  }
  /**
  * setex可以设置超时时间
  */
  public void setex( String keyPrefix, Integer timeOut, String key, Object value ){
  this.checkKeyNotBlank( key );
  this.getJedis().setex( ObjectUtil.toBytes( keyPrefix + key ), timeOut, ObjectUtil.toBytes( value ) );
  }
  /**
  * setex可以设置超时时间
  */
  public void setex( String key, Integer timeOut, Object value ){
  if(defaultKeyPrefix != null && !"".equals(defaultKeyPrefix)){
  this.setex( defaultKeyPrefix, timeOut, key, value );
  }else{
  this.setexObject(key, timeOut,value);
  }
  }
  /**
  * setexObject可以设置超时时间
  */
  public void setexObject( String key, Integer timeOut, Object value ){
  this.checkKeyNotBlank( key );
  String jsonString = JsonUtil.toJson(value);
  this.getJedis().setex(key, timeOut, jsonString);
  }
  /**
  * get单个获取
  */
  publicT get( String keyPrefix, String key, Class clazz ){
  this.checkKeyNotBlank( key );
  Jedis redis = this.getJedis();
  byte[] bs = redis.get( ObjectUtil.toBytes( keyPrefix + key ) );
  if( bs == null || bs.length == 0 ){
  return null;
  }else{
  T value = ObjectUtil.toObject( bs, clazz );
  return value;
  }
  }
  /**
  * get单个获取
  */
  publicT get( String key, Class clazz ){
  return this.get( defaultKeyPrefix, key, clazz);
  }
  /**
  * get单个获取
  */
  public String getNoPrefixToString( String key ){
  checkKeyNotBlank(key);
  return this.getJedis().get(key);
  }
  /**
  * 获取所有的key
  * position 0为前包含,1位后包含, 2为所有*
  * containStr 包含字符串
  */
  public Map getAllKey(Integer position, String containStr){
  Map map = new HashMap();
  String sourceStr = "*";
  try{
  if(position == 0){
  sourceStr = containStr + sourceStr;
  }else if(position == 1){
  sourceStr = sourceStr + containStr;
  }
  Jedis redis = this.getJedis();
  Set set = (Set)redis.keys(sourceStr);
  if(!set.isEmpty()){
  for(String i : set){
  String value=this.getNoPrefixToString(i);
  map.put(i, value);
  Logger.info("后缀为"+containStr+"中的KEY 是"+i+", VALUE 是:"+value);
  }
  }else{
  if(position==1){
  Logger.info("后标志位为:"+containStr+"在缓存中没有数据");
  }else{
  Logger.info("前标志位为:"+containStr+"在缓存中没有数据");
  }
  }
  }catch(Exception e){
  Logger.error("匹配key为" + containStr + "异常", e);
  }
  return map;
  }
  /**
  * del删除
  */
  public void del( String keyPrefix, String key ){
  Jedis redis = this.getJedis();
  redis.del(key);
  }
  /**
  * del删除
  */
  public void del( String key ){
  Jedis redis = this.getJedis();
  redis.del(key);
  }
  

  public void setHost(String host) {
  this.host = host;
  }
  

  public void setPort(Integer port) {
  this.port = port;
  }
  

  public void setPassword(String password) {
  this.password = password;
  }
  

  public void setMinIdle(Integer minIdle) {
  this.minIdle = minIdle;
  }
  

  public void setMaxIdle(Integer maxIdle) {
  this.maxIdle = maxIdle;
  }
  

  public void setMaxTotal(Integer maxTotal) {
  this.maxTotal = maxTotal;
  }
  

  public void setDefaultKeyPrefix(String defaultKeyPrefix) {
  this.defaultKeyPrefix = defaultKeyPrefix;
  }


页: [1]
查看完整版本: redis缓存