Jar Package Required:
Jedis-2.1.0.jar
Commons-pool-1.6.jar
Unit tests:
package com.wangbingan.db;import java.util.hashmap;import java.util.iterator;import Java.util.map;import org.junit.before;import org.junit.test;import com.wangbinga.util.redisutil ; Import redis.clients.jedis.jedis;/** * redis Test * * @author ak * */public class redistest {private jedis jedis;// private string name = "Usher";// private string age = ";// private " string sex = "Male";/** * test cases run before start */@Beforepublic void setup () {// server ipstring ip = "60.28.29.22";// Server port int port = 6379;// Authorization password string password = "Ytw-2015#ehsan";// link Redis server Jedis = new jedis (IP, port);// authorization Jedis.auth (password);} /** * string Test */@Testpublic void stringtest () {// Add Data JEdis.set ("name", "Usher"); SYSTEM.OUT.PRINTLN ("Add Data:" + jedis.get ("name") + "\ n");// stitching Data jedis.append ("name", "\040is very cool!"); SYSTEM.OUT.PRINTLN ("Stitching Data:" + jedis.get ("name") + "\ n");// delete data Jedis.del ("name"); System.out.println ("Delete data:" + jedis.get ("name") + "\ n");// set multiple data jedis.mset ("name", "Usher", "Age", ", " Sex ", " male ";// some data +jedis.incrby (" age ", 10); System.out.println ("Name:" + jedis.get ("name") + "\040 Ages:" + jedis.get ("Age") + "\040 Gender:" + jedis.get ("Sex") + "\ n"); /** * map Test */@Testpublic void maptest () {Map<String, String> Map = new hashmap<string, string> ();// Add Data map.put ("name", "Usher"); Map.put ("Age", "), Map.put (" Sex ", " male ");// added to Redis jedis.hmset (" Information ", map); System.out.println("Add map data:" + jedis.hmget ("Information", "name", "age", "sex") + "\ n");// Delete Data Jedis.hdel ("Information", "name"); System.out.println ("Delete map data:" + jedis.hmget ("Information", "name") + "\ n");// return key= Information number of values SYSTEM.OUT.PRINTLN ("returns the number of Key=information values:" + jedis.hlen ("Information") + "\ n") ;// determines if there are key=information objects System.out.println ("Determine if there is a Key=information object:" + jedis.exists (" Information ") + " \ n ");// returns all key values in the Map object System.out.println (" Returns all key values in the Map object: " + Jedis.hkeys ("Information") + "\ n");// returns all value values in the Map object System.out.println ("returns all value values in the Map object:" + jedis.hvals ("Information") + "\ n");// loop Iteration keyiterator<string> it = Jedis.hkeys ("Information"). Iterator ();// determine if there are elements present while (It.hasnext ()) {// get element keystring key = it.next (); System.out.println ("Return key to" + key + "value:" + jedis. Hmget ("Information", key) + "\ n");}} /** * list Test */@Testpublic void listtest () {// empty Listjedis.del First (" Information ");// see if the list is emptied System.out.println (" lis[after emptying]t: " + jedis.lrange (" Information ", 0 , -1) + "\ n");// Add Data Jedis.lpush ("Information", "Usher") Jedis.lpush ("Information", " Jedis.lpush ("Information", "age");// view List element System.out.println ("list[after Add]:" + Jedis.lrange ("Information", 0, -1) + "\ n");// emptying Listjedis.del ("information");// Add Data Jedis.lpush ("Information", "Elliot"), Jedis.lpush ("Information", "Sex"), Jedis.lpush ("Information") , "age");// view List element System.out.println ("list[empty + add]:" + jedis.lrange ("Information", 0 , -1) + "\ n");} /** * set Test */@Testpublic void settest () {// add element jedis.sadd ("User", " Usher "), Jedis.sadd (" user ", " sex ") Jedis.sadd (" User ", " age"); System.out.println ("Value of key [before delete]:" + jedis.smembers ("user") + "\ n");// Delete element Jedis.srem ("user", "age");// all elements of Value () System.out.println ("Value of key [after deletion]:" + Jedis.smembers ("user") + "\ n");// determine if the value exists System.out.println ("Determine if the value exists:" + Jedis.sismember ("User", "age") + "\ n");// returns the number of collection elements System.out.println ("returns the number of collection elements:" + jedis.scard ("user") + "\ n");// returns the random element System.out.println ("Return random element:" + Jedis.srandmember ("user") + "\ n");} /** * jedis sort */@Testpublic void sorttest () {// add element Jedis.rpush ("sort ", " 1 "), Jedis.lpush (" Sort ", " 2 "), Jedis.lpush (" Sort ", " 3 "); Jedis.lpush (" Sort ", " 4 "); System.out.println ("element:" + jedis.lrange ("sort", 0, -1) + "\ n");// Sort System.out.println ("Sorting:" + jedis.sort ("sort") + "\ n");// jedis.sort ("sort");// Re-output once// systEm.out.println ("element:" + jedis.lrange ("sort", 0, -1) + "\ n");} /** * Test Chinese */@Testpublic void chinesetest () {redisutil.getjedis (). Set ("Name", "Chinese test"); System.out.println (Redisutil.getjedis (). Get ("name"));}}
Redis connection pooling:
package com.wangbinga.util;import redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import redis.clients.jedis.jedispoolconfig;/** * redis Connection Pool * * @author ak * */public final class redisutil {// redis server ipprivate static string addr = "60.28.29.22";// redis's port number private static int PORT = 6379;// Access Password private static string auth = "Ytw-2015#ehsan";// the maximum number of available connection instances, the default value is 8;// if the assignment is 1, it means no limit, and if the pool has already allocated maxactive Jedis instances, The pool's state is exhausted (exhausted) at this time. private static int max_active = 1024;// controls the maximum number of Jedis instances in a pool that have an idle (idle) state. The default value is also 8. private static int max_idle = 200;// the maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means that it never times out. If the wait time is exceeded, the jedisconnectionexception;private static long max_wait = 10000;// is thrown directly Maximum delay time private static int timeout = 10000;// If the validate operation is performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instance is available;private Static boolean test_on_borrow = true;private static jedispool jedispool = null;/** * initializing Redis Connection pool */static {try {jedispoolconfig config = new jedispoolconfig (); config.setmaxactive (max_active); Config.setmaxidle (MAX_IDLE); config.setMaxWait (max_wait); Config.settestonborrow (Test_on_borrow); Jedispool = new jedispool (Config, ADDR, port, timeout, auth);} catch (exception e) {e.printstacktrace ();}} /** * Get Jedis instances * * @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;}} /** * release Jedis resources * * @param jedis */public static void Returnresource (Final jedis jedis) {if (jedis != null) { Jedispool.returnresource (Jedis);}}}
The top 2 jar packages are important (version O) because some of the methods of Redis connection pooling are available in jedis-2.1.0, and if other versions may not have this method.
There is a connection pool operation need to have commons-pool-1.6.jar this package, because the first time with the Java write connection pool, so do not understand, but I think
As long as the use of the connection pool place, there will certainly be this package, haha, witty me!
Implementing Redis Cache Technology in Java