Because Jediscluster does not provide an encapsulation of the keys command, it can only be implemented by itself:
Define the interface first. Returns using TreeSet to make it easy to take advantage of its top () method:
Public interface Iredisoperator {
/**
* To obtain all keys
* @param pattern
* @return
treeset<string> keys (String pattern);
Implementation class:
public class Redisoperator implements Iredisoperator {public
final static Logger Logger = Loggerfactory.getlogger (Re Disoperator.class);
@Autowired
private Jediscluster jediscluster;
@Override public
treeset<string> keys (String pattern) {
logger.debug ("Start getting Keys ...");
treeset<string> keys = new treeset<> ();
map<string, jedispool> clusternodes = Jediscluster.getclusternodes ();
For (String K:clusternodes.keyset ()) {
logger.debug ("Getting keys from: {}", k);
Jedispool JP = Clusternodes.get (k);
Jedis connection = Jp.getresource ();
try {
Keys.addall (Connection.keys (pattern));
} catch (Exception e) {
logger.error ("Getting Keys error: {} ", E);
} finally{
logger.debug ("Connection closed.");
Connection.close ()//////...
}
}
Logger.debug ("Keys gotten!");
return keys;
}
Call:
treeset<string> keys = redisoperator.keys (prefix);
map<string, object> data = new linkedhashmap<> ();
Data.put ("prefix", prefix);
Data.put ("Count", Keys.size ());
String type = "Unknown";
if (Keys.size () >0) {
type = Redisoperator.type (Keys.first ());
}
Data.put ("type", type);