Redis 經常使用客戶端: Redission、Jedisjava
redis的Java客戶端,可訪問、操做Redisredis
Jedis工具類:工具
package com.zhiwei.redis_jedis; import java.util.ResourceBundle; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Redis的基礎工具類 * @author Yang Zhiwei * */ public final class RedisUtil { private static JedisPool jedisPool = null; //初始化JedisPool鏈接池 static { try { //從classpath路徑下獲取redis.perpoties文件,獲取jedis鏈接池參數(後綴名能夠省略) ResourceBundle rb=ResourceBundle.getBundle("com/zhiwei/redis_jedis/redis"); int maxActive=Integer.parseInt(rb.getString("redis.pool.maxActive")); int maxIdle=Integer.parseInt(rb.getString("redis.pool.maxIdle")); int minIdle=Integer.parseInt(rb.getString("redis.pool.minIdle")); int maxWait=Integer.parseInt(rb.getString("redis.pool.maxWait")); //下面亮相兩項配置會影響性能(通常在測試環境中使用) boolean testOnBorrow=Boolean.parseBoolean(rb.getString("redis.pool.testOnBorrow")); boolean testOnReturn=Boolean.parseBoolean(rb.getString("redis.pool.testOnReturn")); String host=rb.getString("redis.ip"); int port=Integer.parseInt(rb.getString("redis.port")); //封裝jedis鏈接池參數 JedisPoolConfig jedisPoolConfig=new JedisPoolConfig(); jedisPoolConfig.setMaxActive(maxActive); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMinIdle(minIdle); jedisPoolConfig.setMaxWait(maxWait); jedisPoolConfig.setTestOnBorrow(testOnBorrow); jedisPoolConfig.setTestOnReturn(testOnReturn); //建立redis鏈接池 jedisPool=new JedisPool(jedisPoolConfig,host,port); } catch (Exception e) { e.printStackTrace(); } } /** * 經過Jedis鏈接池獲取Jedis:原子性 * @return */ public synchronized static Jedis getJedis() { try { if (jedisPool != null) { Jedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } /** * jedis鏈接返回給鏈接池,釋放資源 * @param jedis */ public static void releaseJedis(final Jedis jedis) { if (jedis != null) { jedisPool.returnResource(jedis); } } /** * 根據key值去查找value值 * @param key * @return */ public static String find(String key){ Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.get(key); } catch (Exception e) { e.printStackTrace(); return null; }finally{ RedisUtil.releaseJedis(jedis); //釋放Jedis鏈接 } } /** * 獲取key對應的value值的子串: * set key "this is example!" * getrange key 0 3 * --->"this" */ public static String findSubStr(String key,Integer startOffset,Integer endOffset){ Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.getrange(key, startOffset, endOffset); } catch (Exception e) { e.printStackTrace(); return null; }finally{ jedisPool.returnResource(jedis); } } /** * redis添加k-v對:finally中代碼塊經過try--catch以後必定會觸發 * @param key * @param value * @return * @throws Exception */ public static boolean add(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{ RedisUtil.releaseJedis(jedis); } } /** * 刪除redis中的key對應的k-v對 * @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{ RedisUtil.releaseJedis(jedis); } } }
Redis 配置性能
redis.pool.maxActive=200 #最大鏈接數:可以同時創建的「最大連接個數」 redis.pool.maxIdle=100 #最大空閒數:空閒連接數大於maxIdle將進行回收 redis.pool.minIdle=5 #最小空閒數:低於minIdle時,將建立新的連接 redis.pool.maxWait=100 #最大等待時間:單位ms redis.pool.testOnBorrow=true #使用鏈接時,檢測鏈接是否成功 redis.pool.testOnReturn=true #返回鏈接時,檢測鏈接是否成功 redis.ip=127.0.0.1 redis.port=6379
基礎測試類:測試
package com.zhiwei.redis_jedis; /* *jedis是java訪問redis的一個客戶端,相似FTPClient HttpClient */ import redis.clients.jedis.Jedis; public class MainTest { public static void main(String[] args) throws Exception { if(RedisUtil.add("squirrel", "xiaoyang")){ System.err.println("key:squirrel,value:"+RedisUtil.find("squirrel")); } } //常規鏈接:類比FTPClient @SuppressWarnings("unused") private static void basic() { Jedis jedis = new Jedis("127.0.0.1"); //創建jedis與redis鏈接 System.out.println("Connection to server sucessfully"); System.out.println("Server is running: "+jedis.ping()); jedis.set("name", "xiaoyang"); //添加k-v對 System.out.println(jedis.get("name")); //獲取key爲name的元素的值 jedis.del("name"); //刪除Key爲name的元素 System.out.println(jedis.get("name")); jedis.flushDB(); //清空當前訪問DB的數據 } /** * 基礎測試類:通常開發 */ private static void poolJedis(){ //獲取Redis的客戶端Jedis:通常開發使用Redis進行集羣開發,jedis鏈接經過鏈接池獲取 Jedis jedis=RedisUtil.getJedis(); jedis.set("name", "sqirrel"); //redis中添加數據:k-v形式 System.out.println("redis--->"+jedis.get("name"));//取值 String result=RedisUtil.find("name"); System.out.println("result--->"+result); } }