您的位置:首页 > 数据库 > Redis

使用Jedis操作redis

2014-10-09 15:01 148 查看
原文地址:<a target=_blank href="http://javacrazyer.iteye.com/blog/1840161">参考</a>    <a target=_blank href="http://www.tuicool.com/articles/vYVvQby">参考1</a>
Jedis工具类
package demo.hw.redis;

import java.util.HashMap;
import java.util.Map;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* redis客户端
* @author pchzhang
*
*/
public class JedisUtil {
private static Map<String,JedisPool>maps=new HashMap<String,JedisPool>();

private static JedisUtil jedisUtil=new JedisUtil();
private JedisUtil()
{
}

/**
* 或的实例
* @author pchzhang
* @return
*/
public static JedisUtil getInstance()
{
return jedisUtil;
}
private static JedisPool getPool(String ip,int port)
{
String key=ip+":"+port;
JedisPool pool=null;
if(!maps.containsKey(key)) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(100);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
try{
/**
*如果你遇到 java.net.SocketTimeoutException: Read timed out exception的异常信息

4000
*请尝试在构造JedisPool的时候设置自己的超时值. JedisPool默认的超时时间是2秒(单位毫秒)
*/
pool = new JedisPool(config, ip, port,2000);
maps.put(key, pool);
} catch(Exception e) {
e.printStackTrace();
}
}else{
pool = maps.get(key);
}
return pool;
}

/**
* 获取Redis实例.
* @return Redis工具类实例
*/
public Jedis getJedis(String ip,int port) {
Jedis jedis  = null;
int count =0;
do{
try{
jedis = getPool(ip,port).getResource();
//log.info("get redis master1!");
} catch (Exception e) {
// 销毁对象
getPool(ip,port).returnBrokenResource(jedis);
}
count++;
}while(jedis==null&&count<5);
return jedis;
}

/**
* 释放redis实例到连接池.
* @param jedis redis实例
*/
public void closeJedis(Jedis jedis,String ip,int port) {
if(jedis != null) {
getPool(ip,port).returnResource(jedis);
}
}
}
Jedis测试类:
<pre name="code" class="java">package demo.hw.redis;import redis.clients.jedis.Jedis;import redis.clients.jedis.SortingParams;public class RedisClientTest {public static void main(String[] args) {try {test1();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 测试基本操作* @author pchzhang*/public static void test0(){Jedis jedis = new Jedis("192.168.70.154");//Jedis jedis=JedisUtil.getInstance().getJedis("192.168.70.154", 6379);jedis.set("score","90");String value = jedis.get("score");System.out.println(value);jedis.append("score", " my god");System.out.println("append之后:"+jedis.get("score"));jedis.del("score");System.out.println("删除之后"+jedis.get("score"));}public static void test1() throws InterruptedException{//Jedis jedis = new Jedis("192.168.70.154");Jedis jedis=JedisUtil.getInstance().getJedis("192.168.70.154", 6379);jedis.setnx("sex", "男");// 若key不存在,则存储jedis.setex("what",3, "123");//设置key的存在时间System.out.println(jedis.get("what"));Thread.sleep(2000);System.out.println("查看what的剩余生存时间:"+jedis.ttl("what"));//查看what剩余时间Thread.sleep(1100);System.out.println("休眠两秒之后:"+jedis.get("what"));}/*** 测试操作列表* @author pchzhang*/public static void testList(){Jedis jedis=JedisUtil.getInstance().getJedis("192.168.70.154", 6379);// 添加数据jedis.lpush("lists", "vector");jedis.lpush("lists", "ArrayList");jedis.lpush("lists", "LinkedList");// 数组长度System.out.println(jedis.llen("lists"));// 排序/** list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"),* 会出现"ERR One or more scores can't be converted into double"*/SortingParams sortingParameters = new SortingParams();sortingParameters.alpha();System.out.println(jedis.sort("lists",sortingParameters));// 字串System.out.println(jedis.lrange("lists", 0, 3));// 修改列表中单个值jedis.lset("lists", 0, "hello list!");// 获取列表指定下标的值System.out.println(jedis.lindex("lists", 1));// 删除列表指定下标的值System.out.println(jedis.lrem("lists", 1, "vector"));// 删除区间以外的数据System.out.println(jedis.ltrim("lists", 0, 1));// 列表出栈System.out.println(jedis.lpop("lists"));// 整个列表值}}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: