Jediscluster the way to implement the Redis keys command _redis

Source: Internet
Author: User

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);


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.