Package com.wanhua.util;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.logging.Logger;
Import play. Play;
Import Redis.clients.jedis.Jedis;
Import Redis.clients.jedis.JedisPool;
Import Redis.clients.jedis.JedisPoolConfig;
/**
* Redis Tool class for obtaining redispool. Refer to the official website as follows: You shouldn ' t use the same instance from different threads because you'll have strange errors. And sometimes creating lots of Jedis instances is not good enough because it means lots of sockets and connections, which Leads to strange errors as well. A single Jedis instance are not threadsafe! To avoid these problems, your should use Jedispool, which is a threadsafe pool of network connections. This can overcome those strange errors and achieve great performance. To use it, init a pool:jedispool pool = new Jedispool (new Jedispoolconfig (), "localhost"); You can store the pool somewhere statically, it's thread-safe. Jedispoolconfig includes a number of helpful redis-specific connection pooling defaults. For example, Jedis with Jedispoolconfig would close a connection after seconds if it had not been returned.
*
* @author Bqwang
*/
public class Jedisutil {
private static Logger Logger = Logger.getlogger (JedisUtil.class.getName ()); Logger
/**
* Private constructor.
*/
Private Jedisutil () {
}
private static map<string, jedispool> maps = new hashmap<string, jedispool> ();
/**
* Get connection pool.
*
* @return Connection Pool Instance
*/
private static Jedispool getpool (String IP, int port) {
String key = IP + ":" + port;
Jedispool pool = null;
if (!maps.containskey (key)) {
Jedispoolconfig config = new Jedispoolconfig ();
Config.setmaxidle (Integer.parseint (Play.configuration.getProperty ("Redis.pool.maxIdle"));
Config.setmaxtotal (Integer.parseint (Play.configuration.getProperty ("Redis.pool.maxTotal"));
Config.settestonborrow (Play.configuration.getProperty ("redis.pool.testOnBorrow") = = "true"? True:false);
Config.settestonreturn (Play.configuration.getProperty ("redis.pool.testOnReturn") = = "true"? True:false);
try {
/**
* If you encounter Java.net.SocketTimeoutException:Read timed out exception, try setting your own timeout value when constructing jedispool. Jedispool default Time-out is 2 seconds (in milliseconds)
*/
Pool = new Jedispool (config, IP, port, Integer.parseint (Play.configuration.getProperty ("redis.pool.timeout"));
Maps.put (key, pool);
} catch (Exception e) {
E.printstacktrace ();
}
} else {
Pool = Maps.get (key);
}
return pool;
}
/**
* Class-level internal class, that is, a static member-style inner class, an instance of the inner class has no binding relationship to an instance of an external class, and is loaded only when it is called, enabling lazy loading.
*/
private Static Class Redisutilholder {
/**
* Static initializers, which are guaranteed by the JVM for thread safety
*/
private static Jedisutil instance = new Jedisutil ();
}
/**
* When the GetInstance method is first called, it reads the redisutilholder.instance for the first time, causing the Redisutilholder class to be initialized, and the class initializes its static domain when it is loaded and initialized. This creates an instance of Redisutil, because it is a static domain, so it is initialized only once when the class is loaded by the virtual machine and is guaranteed to be thread-safe by the virtual machine. The advantage of this pattern is that the GetInstance method is not synchronized and only performs a domain access, so deferred initialization does not add any access costs.
*/
public static Jedisutil getinstance () {
return redisutilholder.instance;
}
/**
* Get Redis instances.
*
* @return Redis Tool class instances
*/
Public Jedis Getjedis () {
Logger.info ("Get getjedis------");
Jedis Jedis = null;
int count = 0;
do {
String IP = Play.configuration.getProperty ("Redis.server.ip");
Logger.info ("Get IP------" +IP);
int port = integer.parseint (Play.configuration.getProperty ("Redis.server.port"));
Logger.info ("Get port------" +port);
try {
Jedis = Getpool (IP, Port). GetResource ();
} catch (Exception e) {
Logger.info ("Get Redis master1 failed!" + e);
Destroying objects
Getpool (IP, Port). Returnbrokenresource (Jedis);
}
count++;
} while (Jedis = = null && count < Integer.parseint (Play.configuration.getProperty ("Redis.pool.retryTimes")) );
Logger.info ("Get Jedis success------");
return Jedis;
}
/**
* Release the Redis instance to the connection pool.
*
* @param Jedis
* Redis Instance
*/
public static void Closejedis (Jedis Jedis) {
String IP = Play.configuration.getProperty ("Redis.server.ip");
int port = integer.parseint (Play.configuration.getProperty ("Redis.server.port"));
if (Jedis! = null) {
Getpool (IP, Port). Returnresource (Jedis);
}
}
public static void Main (String args[]) {
Jedis JDS = Jedisutil.getinstance (). Getjedis ();
Adding data to Redis
Jds.sadd ("MB", "test");
Fetching data from Redis
set<string> ResultSet = jds.smembers ("MB");
System.out.println ("--get--result--from--redis---" + resultset.tostring ());
Emptying the message for the specified key in Redis
Jds.del ("MB");
Clear all Redis messages
Jds.flushdb ();
}
}
Use of Redis: A tool class to get Redis instances