1. 如何获取Jedis

获取Jar包可以通过Maven库获取:http://mvnrepository.com/artifact/redis.clients/jedis

或者可以通过Maven配置:

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

2. 设置Redis连接认证

2.1 修改配置文件redis.conf,放开requirepass的注释

################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass foobared

修改好后,重起redis:

bash-4.2$ ./redis-server ../redis.conf

2.2 或者直接在redis-cli中修改,无需重起,立即生效

127.0.0.1:6379> config set requirepass 123456

3. Jedis操作代码

package com.rc.jedis.common.util;

import redis.clients.jedis.Jedis;

public class JedisUtil {

    private static Jedis jedis;

    static {
jedis = new Jedis("localhost", 6379);
jedis.auth("123456");
} public static Jedis getJedisConn() {
return jedis;
} public static void main(String[] args) {
System.out.println("==================Test String===============================");
testString();
System.out.println("==================Test List===============================");
testList();
System.out.println("==================Test Set===============================");
testSet();
System.out.println("==================Test Hash===============================");
testHash();
System.out.println("==================Test Zset===============================");
testZset();
} private static void testString() {
//添加数据
jedis.set("skey2", "testskey2");
System.out.println(jedis.get("skey2"));
//在字符串后面添加字符
jedis.append("skey2", " is a test case");
System.out.println(jedis.get("skey2"));
//删除
jedis.del("skey2");
System.out.println(jedis.get("skey2"));
//批量添加
jedis.msetnx("skey1","skey1 value", "skey2", "skey2 value", "skey3", "skey3 value", "skey4", "2");
jedis.incr("skey4");
System.out.println(jedis.get("skey1") + " - " + jedis.get("skey2") + " - " + jedis.get("skey3") + " - " + jedis.get("skey4")); } private static void testList() {
//添加List数据
jedis.lpush("lkey1", "1", "2", "3");
//获取指定位置的数据
System.out.println("list:" + jedis.lindex("lkey1", 1));
//弹出List中所有值
String popString = jedis.lpop("lkey1");
while(popString != null)
{
System.out.println("List:" + popString);
popString = jedis.lpop("lkey1");
}
//同lpush
jedis.rpush("lkey1", "1", "2", "3");
System.out.println("list:" + jedis.lindex("lkey1", 1));
//删除List
jedis.del("lkey1");
System.out.println("list:" + jedis.lindex("lkey1", 1));
} private static void testSet() {
//添加Set数据,同Java的Set,重复数据只存一条
jedis.sadd("sekey1", "1", "2", "3", "1", "4", "5", "4");
System.out.println(jedis.smembers("sekey1"));
//检查元素是否存在
System.out.println(jedis.sismember("sekey1", "3"));
System.out.println(jedis.sismember("sekey1", "9"));
//移除元素
jedis.srem("sekey1", "2");
System.out.println(jedis.sismember("sekey1", "2"));
} private static void testHash() {
//添加数据
jedis.hset("hkey1", "fkey1", "1");
jedis.hset("hkey1", "fkey2", "2");
jedis.hset("hkey1", "fkey3", "3");
System.out.println(jedis.hget("hkey1", "fkey2"));
System.out.println(jedis.hgetAll("hkey1"));
//删除
jedis.hdel("hkey1", "fkey2");
System.out.println(jedis.hget("hkey1", "fkey2"));
System.out.println(jedis.hgetAll("hkey1"));
} private static void testZset() {
//添加数据
jedis.zadd("zkey1", 1, "zfkey1");
jedis.zadd("zkey1", 3, "zfkey3");
jedis.zadd("zkey1", 2, "zfkey2");
jedis.zadd("zkey1", 5, "zfkey5");
jedis.zadd("zkey1", 4, "zfkey4");
System.out.println(jedis.zrange("zkey1", 0, -1));
System.out.println(jedis.zrangeByScore("zkey1", 0, 2));
jedis.zrem("zkey1", "zfkey3");
System.out.println(jedis.zrange("zkey1", 0, -1));
} }
05-08 08:14