独角兽企业重金招聘Python工程师标准>>>
需要jar包:
jedis-2.1.0.jar
commons-pool-1.6.jar
单元测试:
packagecom.wangbingan.db;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;importorg.junit.Before;importorg.junit.Test;importcom.wangbinga.util.RedisUtil;importredis.clients.jedis.Jedis;/***Redis测试**@authorAK**/publicclassRedisTest{privateJedisjedis;//privateStringname="Usher";//privateStringage="44";//privateStringsex="male";/***测试用例启动前运行*/@Beforepublicvoidsetup(){//服务器ipStringip="60.28.29.22";//服务器端口intport=6379;//授权密码Stringpassword="Ytw-#Ehsan";//链接redis服务器jedis=newJedis(ip,port);//授权jedis.auth(password);}/***String测试*/@TestpublicvoidstringTest(){//添加数据jedis.set("name","Usher");System.out.println("添加数据:"+jedis.get("name")+"\n");//拼接数据jedis.append("name","\040isverycool!");System.out.println("拼接数据:"+jedis.get("name")+"\n");//删除数据jedis.del("name");System.out.println("删除数据:"+jedis.get("name")+"\n");//设置多个数据jedis.mset("name","Usher","age","24","sex","male");//某个数据+jedis.incrBy("age",10);System.out.println("姓名:"+jedis.get("name")+"\040年龄:"+jedis.get("age")+"\040性别:"+jedis.get("sex")+"\n");}/***Map测试*/@TestpublicvoidmapTest(){Map<String,String>map=newHashMap<String,String>();//添加数据map.put("name","Usher");map.put("age","24");map.put("sex","male");//添加到redis中jedis.hmset("Information",map);System.out.println("添加Map数据:"+jedis.hmget("Information","name","age","sex")+"\n");//删除数据jedis.hdel("Information","name");System.out.println("删除Map数据:"+jedis.hmget("Information","name")+"\n");//返回key=Information的值的个数System.out.println("返回key=Information的值的个数:"+jedis.hlen("Information")+"\n");//判断是否存在key=Information的对象System.out.println("判断是否存在key=Information的对象:"+jedis.exists("Information")+"\n");//返回map对象中的所有key值System.out.println("返回map对象中的所有key值:"+jedis.hkeys("Information")+"\n");//返回map对象中的所有value值System.out.println("返回map对象中的所有value值:"+jedis.hvals("Information")+"\n");//循环迭代keyIterator<String>it=jedis.hkeys("Information").iterator();//判断是否有元素存在while(it.hasNext()){//获取元素keyStringkey=it.next();System.out.println("返回key为"+key+"的值:"+jedis.hmget("Information",key)+"\n");}}/***List测试*/@TestpublicvoidlistTest(){//先清空Listjedis.del("Information");//查看List是否清空System.out.println("Lis[清空后]t:"+jedis.lrange("Information",0,-1)+"\n");//添加数据jedis.lpush("Information","Usher");jedis.lpush("Information","sex");jedis.lpush("Information","age");//查看List元素System.out.println("List[添加后]:"+jedis.lrange("Information",0,-1)+"\n");//清空Listjedis.del("Information");//添加数据jedis.lpush("Information","Elliot");jedis.lpush("Information","sex");jedis.lpush("Information","age");//查看List元素System.out.println("List[清空+添加]:"+jedis.lrange("Information",0,-1)+"\n");}/***set测试*/@TestpublicvoidsetTest(){//添加元素jedis.sadd("user","Usher");jedis.sadd("user","sex");jedis.sadd("user","age");System.out.println("Key的value值[删除前]:"+jedis.smembers("user")+"\n");//删除元素jedis.srem("user","age");//所有元素的value()System.out.println("Key的value值[删除后]:"+jedis.smembers("user")+"\n");//判断值是否存在System.out.println("判断值是否存在:"+jedis.sismember("user","age")+"\n");//返回集合元素的个数System.out.println("返回集合元素的个数:"+jedis.scard("user")+"\n");//返回随机元素System.out.println("返回随机元素:"+jedis.srandmember("user")+"\n");}/***jedis排序*/@TestpublicvoidsortTest(){//添加元素jedis.rpush("sort","1");jedis.lpush("sort","2");jedis.lpush("sort","3");jedis.lpush("sort","4");System.out.println("元素:"+jedis.lrange("sort",0,-1)+"\n");//排序System.out.println("排序:"+jedis.sort("sort")+"\n");//jedis.sort("sort");//再输出一次//System.out.println("元素:"+jedis.lrange("sort",0,-1)+"\n");}/***测试中文*/@TestpublicvoidchineseTest(){RedisUtil.getJedis().set("name","中文测试");System.out.println(RedisUtil.getJedis().get("name"));}}
redis连接池:
packagecom.wangbinga.util;importredis.clients.jedis.Jedis;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;/***Redis连接池**@authorAK**/publicfinalclassRedisUtil{//Redis服务器IPprivatestaticStringADDR="60.28.29.22";//Redis的端口号privatestaticintPORT=6379;//访问密码privatestaticStringAUTH="Ytw-#Ehsan";//可用连接实例的最大数目,默认值为8;//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。privatestaticintMAX_ACTIVE=1024;//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。privatestaticintMAX_IDLE=200;//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;privatestaticlongMAX_WAIT=10000;//最大延迟时间privatestaticintTIMEOUT=10000;//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;privatestaticbooleanTEST_ON_BORROW=true;privatestaticJedisPooljedisPool=null;/***初始化Redis连接池*/static{try{JedisPoolConfigconfig=newJedisPoolConfig();config.setMaxActive(MAX_ACTIVE);config.setMaxIdle(MAX_IDLE);config.setMaxWait(MAX_WAIT);config.setTestOnBorrow(TEST_ON_BORROW);jedisPool=newJedisPool(config,ADDR,PORT,TIMEOUT,AUTH);}catch(Exceptione){e.printStackTrace();}}/***获取Jedis实例**@return*/publicsynchronizedstaticJedisgetJedis(){try{if(jedisPool!=null){Jedisresource=jedisPool.getResource();returnresource;}else{returnnull;}}catch(Exceptione){e.printStackTrace();returnnull;}}/***释放jedis资源**@paramjedis*/publicstaticvoidreturnResource(finalJedisjedis){if(jedis!=null){jedisPool.returnResource(jedis);}}}
最上面的2个jar包很重要(版本o),因为redis的连接池有些方法在jedis-2.1.0才有,如果别的版本可能没有这个方法。
还有连接池操作的时候需要有commons-pool-1.6.jar这个包,因为第一次用java写连接池,所以不太懂,但是我觉得
只要用到连接池的地方,肯定会有这个包,哈哈,机智的我!