java操作redis,javaredis
Java操作redis簡單的Jedis執行個體
package com.weixuan.utils;import redis.clients.jedis.Jedis;/** * Create by fengtang * 2015/7/30 * JavaRedis */public final class JedisUtils { /** * 建立一個jedis連結. * * @return 返回當前建立的redis連結化物件 */ public static Jedis getJedisConnection() { Jedis jedis = new Jedis("localhost", 6379); return jedis; }}//測試package com.weixuan.test;import com.weixuan.utils.JedisPoolUtils;import com.weixuan.utils.JedisUtils;import org.junit.Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;/** * Create by fengtang * 2015/7/30 * JavaRedis */public class JedisTest { @Test public void testJedisUtils() { System.out.println("testJedisUtils " + JedisUtils.getJedisConnection()); }}
簡單的JedisPool應用
**注意,版本Jedis版本是2.1 ,common pool 的版本是1.5
高版本中有些參數的設定不一樣,比如setMaxActive**
package com.weixuan.utils;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;import java.util.ResourceBundle;/** * Create by fengtang * 2015/7/30 * JavaRedis */public class JedisPoolUtils { public static JedisPool pool; static { ResourceBundle bundle = ResourceBundle.getBundle("jedis"); JedisPoolConfig config = new JedisPoolConfig(); if (bundle == null) { throw new IllegalArgumentException("[jedis.properties] is not found!"); } config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive"))); config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle"))); config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow"))); config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"))); pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port"))); } public static JedisPool getPool() { return pool; }}//測試package com.weixuan.test;import com.weixuan.utils.JedisPoolUtils;import com.weixuan.utils.JedisUtils;import org.junit.Test;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;/** * Create by fengtang * 2015/7/30 * JavaRedis */public class JedisTest { @Test public void testJedisPoolUtils() throws Exception { JedisPool pool = JedisPoolUtils.getPool(); System.out.println("testJedisPoolUtils ---pool ------------" + pool); Jedis jedis = pool.getResource(); System.out.println("testJedisPoolUtils --- jedis ------------" + jedis); }}
高版本中測試
* Jedis 版本2.7 common pool 版本2.4*
將 config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive"))); 換成 config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxTotal"))); 設定檔也需要改
簡單的使用
package com.weixuan.redis; import com.weixuan.utils.JedisPoolUtils; import redis.clients.jedis.Jedis; /** * Create by fengtang * 2015/7/30 * JavaRedis3 */ public class Main { private Jedis jedis = JedisPoolUtils.getPool().getResource(); public void testInsert() { jedis.set("testKey1", "testValue1"); jedis.set("testKey2", "testValue2"); jedis.set("testKey3", "testValue3"); jedis.set("testKey4", "testValue4"); jedis.set("testKey5", "testValue5"); jedis.set("testKey6", "testValue6"); jedis.set("testKey7", "testValue7"); jedis.set("testKey8", "testValue8"); jedis.set("testKey9", "testValue9"); jedis.set("testKey0", "testValue0"); } public String testGet(String keyName) { return jedis.get(keyName); } public static void main(String[] args) { //new Main().testInsert(); System.out.println(new Main().testGet("testKey0")); }}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。