Java Operation Redis Simple Jedis instance
PackageCom.weixuan.utils;ImportRedis.clients.jedis.Jedis;/** * Create by Fengtang * 2015/7/30 * Javaredis * * Public Final class jedisutils { /** * Create a Jedis link. * * @return Return the currently created Redis link object */ Public StaticJedisgetjedisconnection() {Jedis Jedis =NewJedis ("localhost",6379);returnJedis; }}//Test PackageCom.weixuan.test;ImportCom.weixuan.utils.JedisPoolUtils;ImportCom.weixuan.utils.JedisUtils;ImportOrg.junit.Test;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;/** * Create by Fengtang * 2015/7/30 * Javaredis * * Public class jedistest { @Test Public void testjedisutils() {System.out.println ("Testjedisutils"+ jedisutils.getjedisconnection ()); }}
A simple Jedispool application
* * Note that version Jedis version is 2.1, common pool version is 1.5
Some parameters are set differently in higher versions, such as setmaxactive**
PackageCom.weixuan.utils;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;ImportJava.util.ResourceBundle;/** * Create by Fengtang * 2015/7/30 * Javaredis * * Public class jedispoolutils { Public StaticJedispool Pool;Static{ResourceBundle bundle = Resourcebundle.getbundle ("Jedis"); Jedispoolconfig config =NewJedispoolconfig ();if(Bundle = =NULL) {Throw NewIllegalArgumentException ("[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 =NewJedispool (config, bundle.getstring ("Redis.ip"), Integer.valueof (Bundle.getstring ("Redis.port"))); } Public StaticJedispoolGetpool() {returnPool }}//Test PackageCom.weixuan.test;ImportCom.weixuan.utils.JedisPoolUtils;ImportCom.weixuan.utils.JedisUtils;ImportOrg.junit.Test;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;/** * Create by Fengtang * 2015/7/30 * Javaredis * * Public class jedistest { @Test Public void testjedispoolutils()throwsException {Jedispool pool = Jedispoolutils.getpool (); System.out.println ("Testjedispoolutils---pool------------"+ pool); Jedis Jedis = Pool.getresource (); System.out.println ("Testjedispoolutils---jedis------------"+ Jedis); }}
Test in high version
* Jedis version 2.7 common pool version 2.4*
将 config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive"))); 换成 config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxTotal"))); 配置文件也需要改
Simple to use
PackageCom.weixuan.redis;ImportCom.weixuan.utils.JedisPoolUtils;ImportRedis.clients.jedis.Jedis;/** * Create by Fengtang * 2015/7/30 * JAVAREDIS3 * * Public class Main { PrivateJedis 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"); } PublicStringTestget(String keyName) {returnJedis.get (KeyName); } Public Static void Main(string[] args) {//new Main (). Testinsert ();System.out.println (NewMain (). Testget ("TestKey0")); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Operations Redis