Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

25.2. Jedis

		
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.2</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>		
		
		
		
package cn.netkiller.redis;

import redis.clients.jedis.Jedis;

public class JedisTest {

	private static Jedis jedis;

	public static void main(String[] args) {
		jedis = new Jedis("192.168.2.1");
		System.out.println("Server is running: "+jedis.ping());
		jedis.set("foo", "bar");
		String value = jedis.get("foo");
		System.out.println(value);
	}

}
		
		

25.2.1. 認證

			
jedis = new Jedis("localhost", 6379, 15000);
jedis.auth("foobared");
			
			
			
			

25.2.2. jedis.keys

			
	public Map<String, String> getCaptcha() {

		Map<String, String> captchas = new HashMap<String, String>();
		this.jedis = new Jedis(this.config.getProperty("captcha.redis"));
		
		System.out.printf("%s: Redis server is running %s\n", this.getClass().getName(), this.jedis.ping());
		
		Set<String> keys = jedis.keys("captcha::phone::*");
		if (!keys.isEmpty()) {
			for(String key:keys){
				if (this.jedis.exists(key)) {
					captchas.put(key.replace("captcha::phone::", ""), this.jedis.get(key)+" ["+ String.valueOf(this.jedis.ttl(key)) + "s]");
				}
			}
		}

		this.jedis.close();

		return captchas;
	}		
			
			
			
import redis.clients.jedis.Jedis;
public class RedisKeys {
   public static void main(String[] args) {
      
      Jedis jedis = new Jedis("localhost");
      System.out.println("Connection to server sucessfully");

      List<String> list = jedis.keys("*");
      for(int i=0; i<list.size(); i++) {
         System.out.println("List of stored keys:: "+list.get(i));
      }
   }
}