Redis緩衝系統-Java-Jedis操作Redis,基本操作以及 實現對象儲存

來源:互聯網
上載者:User

標籤:

 

原始碼下載:   http://download.csdn.net/detail/jiangtao_st/7623113

1、Maven配置

<dependency>  <groupId>redis.clients</groupId>  <artifactId>jedis</artifactId>  <version>2.5.0</version></dependency><dependency>  <groupId>com.alibaba</groupId>  <artifactId>fastjson</artifactId>  <version>1.1.41</version></dependency></span>

2、Properties 設定檔

redis.pool.maxActive=  100

  redis.pool.maxIdle=  20

  redis.pool.maxWait=  3000

  redis.ip=  localhost

  redis.port=  6379

3、代碼具體實現的Client

/** *  * <p> * Redis用戶端訪問 * </p> *  * @author 卓軒 * @建立時間:2014年7月11日 * @version: V1.0 */public class RedisClient {    public  static  JedisPool jedisPool;     static {            ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");    int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));    int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));    int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));        String ip = resourceBundle.getString("redis.ip");    int port = Integer.parseInt(resourceBundle.getString("redis.port"));        JedisPoolConfig config = new JedisPoolConfig();          config.setMaxTotal(maxActive);        config.setMaxIdle(maxIdle);        config.setMaxWaitMillis(maxWait);            jedisPool = new JedisPool(config, ip, port);   }    /**   * 向緩衝中設定字串內容   * @param key key   * @param value value   * @return   * @throws Exception   */  public static boolean  set(String key,String value) throws Exception{    Jedis jedis = null;    try {      jedis = jedisPool.getResource();      jedis.set(key, value);      return true;    } catch (Exception e) {      e.printStackTrace();      return false;    }finally{      jedisPool.returnResource(jedis);    }  }    /**   * 向緩衝中設定對象   * @param key    * @param value   * @return   */  public static boolean  set(String key,Object value){    Jedis jedis = null;    try {      String objectJson = JSON.toJSONString(value);      jedis = jedisPool.getResource();      jedis.set(key, objectJson);      return true;    } catch (Exception e) {      e.printStackTrace();      return false;    }finally{      jedisPool.returnResource(jedis);    }  }    /**   * 刪除緩衝中得對象,根據key   * @param key   * @return   */  public static boolean del(String key){    Jedis jedis = null;    try {      jedis = jedisPool.getResource();      jedis.del(key);      return true;    } catch (Exception e) {      e.printStackTrace();      return false;    }finally{      jedisPool.returnResource(jedis);    }  }    /**   * 根據key 擷取內容   * @param key   * @return   */  public static Object get(String key){    Jedis jedis = null;    try {      jedis = jedisPool.getResource();      Object value = jedis.get(key);      return value;    } catch (Exception e) {      e.printStackTrace();      return false;    }finally{      jedisPool.returnResource(jedis);    }  }    /**   * 根據key 擷取對象   * @param key   * @return   */  public static <T> T get(String key,Class<T> clazz){    Jedis jedis = null;    try {      jedis = jedisPool.getResource();      String value = jedis.get(key);      return JSON.parseObject(value, clazz);    } catch (Exception e) {      e.printStackTrace();      return null;    }finally{      jedisPool.returnResource(jedis);    }  }}

4、 單元測試、儲存對象、寫入對象

/** *  * <p> *  測試獨立redis 用戶端 * </p> *  * @author 卓軒 * @建立時間:2014年7月11日 * @version: V1.0 */public class SimpleClient {    @Test  public void userCache(){            UserDO zhuoxuan = new UserDO();    zhuoxuan.setUserId(113445);    zhuoxuan.setSex(1);    zhuoxuan.setUname("卓軒");    zhuoxuan.setUnick("zhuoxuan");    zhuoxuan.setEmail("[email protected]");        boolean reusltCache = RedisClient.set("zhuoxuan", zhuoxuan);    if (reusltCache) {      System.out.println("向緩衝中儲存對象成功。");    }else{      System.out.println("向緩衝中儲存對象失敗。");    }  }      @Test  public void getUserInfo(){        UserDO zhuoxuan = RedisClient.get("zhuoxuan",UserDO.class);    if(zhuoxuan != null){      System.out.println("從緩衝中擷取的對象," + zhuoxuan.getUname() + "@" + zhuoxuan.getEmail());    }      }    }

 

Redis緩衝系統-Java-Jedis操作Redis,基本操作以及 實現對象儲存

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.