什么是Jedis
是 Redis 官方推荐的 java连接开发工具! 使用Java 操作Redis 中间件!
导入依赖
<!--导入jedis的包-->
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
2、编码测试:
- 连接数据库
- 操作命令
- 断开连接!
import redis.clients.jedis.Jedis;
public class TestPing {
public static void main(String[] args) {
// 1、 new Jedis 对象即可
Jedis jedis = new Jedis("127.0.0.1",6379);
// jedis 所有的命令就是我们之前学习的所有指令!所以之前的指令学习很重要!
System.out.println(jedis.ping());
}
}
输出:
连接Redis时要修改配置文件
- daemonize 设置yes (后台运行)
- protected-mode no 关闭保护模式(即开启远程访问)
- 把 bine 127.0.0.1 注释掉,去掉请求访问ip限制
- 关闭防火墙等
常用API
public class JedisType {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
System.out.println("=================== String =========================");
System.out.println(jedis.set("name", "zs"));
System.out.println(jedis.get("name"));
System.out.println(jedis.append("name", "+value"));
System.out.println(jedis.get("name"));
System.out.println(jedis.strlen("name"));
System.out.println("=================== List =========================");
System.out.println(jedis.lpush("listKey", "l1", "l2", "l3"));
System.out.println(jedis.lrange("listKey", 0, -1)); // [l3, l2, l1]
System.out.println(jedis.llen("listKey"));
System.out.println("=================== Hash =========================");
System.out.println(jedis.hset("hashKey", "k1", "v1"));
System.out.println(jedis.hset("hashKey", "k2", "v2"));
System.out.println(jedis.hset("hashKey", "k3", "v3"));
System.out.println(jedis.hmget("hashKey", "k1", "k2", "k3")); // [v1, v2, v3]
System.out.println(jedis.hgetAll("hashKey")); // {k3=v3, k2=v2, k1=v1}
System.out.println("=================== Set =========================");
System.out.println(jedis.sadd("setKey", "s1", "s2", "s3", "s4"));
System.out.println(jedis.smembers("setKey")); // [s2, s1, s4, s3]
System.out.println(jedis.scard("setKey"));
System.out.println("=================== Zset =========================");
System.out.println(jedis.zadd("ZKey", 90, "z1"));
System.out.println(jedis.zadd("ZKey", 80, "z2"));
System.out.println(jedis.zadd("ZKey", 85, "z3"));
System.out.println(jedis.zrange("ZKey", 0, -1)); // [z2, z3, z1]
}
}
1、通用API
public class TestKey {
public TestKey() {
}
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
System.out.println("清空数据:" + jedis.flushDB());
System.out.println("判断某个键是否存在:" + jedis.exists("username"));
System.out.println("新增<'username','kuangshen'>的键值对:" + jedis.set("username", "kuangshen"));
System.out.println("新增<'password','password'>的键值对:" + jedis.set("password", "password"));
System.out.print("系统中所有的键如下:");
Set<String> keys = jedis.keys("*");
System.out.println(keys);
System.out.println("删除键password:" + jedis.del("password"));
System.out.println("判断键password是否存在:" + jedis.exists("password"));
System.out.println("查看键username所存储的值的类型:" + jedis.type("username"));
System.out.println("随机返回key空间的一个:" + jedis.randomKey());
System.out.println("重命名key:" + jedis.rename("username", "name"));
System.out.println("取出改后的name:" + jedis.get("name"));
System.out.println("按索引查询:" + jedis.select(0));
System.out.println("删除当前选择数据库中的所有key:" + jedis.flushDB());
System.out.println("返回当前数据库中key的数目:" + jedis.dbSize());
System.out.println("删除所有数据库中的所有key:" + jedis.flushAll());
jedis.connect();
jedis.disconnect();
jedis.flushAll();
}
}
2、String(字符串)
public class TestString {
public TestString() {
}
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("===========增加数据===========");
System.out.println(jedis.set("key1", "value1"));
System.out.println(jedis.set("key2", "value2"));
System.out.println(jedis.set("key3", "value3"));
System.out.println("删除键key2:" + jedis.del("key2"));
System.out.println("获取键key2:" + jedis.get("key2"));
System.out.println("修改key1:" + jedis.set("key1", "value1Changed"));
System.out.println("获取key1的值:" + jedis.get("key1"));
System.out.println("在key3后面加入值:" + jedis.append("key3", "End"));
System.out.println("key3的值:" + jedis.get("key3"));
System.out.println("增加多个键值对:" + jedis.mset(new String[]{"key01", "value01", "key02", "value02", "key03", "value03"}));
System.out.println("获取多个键值对:" + jedis.mget(new String[]{"key01", "key02", "key03"}));
System.out.println("获取多个键值对:" + jedis.mget(new String[]{"key01", "key02", "key03", "key04"}));
System.out.println("删除多个键值对:" + jedis.del(new String[]{"key01", "key02"}));
System.out.println("获取多个键值对:" + jedis.mget(new String[]{"key01", "key02", "key03"}));
jedis.flushDB();
System.out.println("===========新增键值对防止覆盖原先值==============");
System.out.println(jedis.setnx("key1", "value1"));
System.out.println(jedis.setnx("key2", "value2"));
System.out.println(jedis.setnx("key2", "value2-new"));
System.out.println(jedis.get("key1"));
System.out.println(jedis.get("key2"));
System.out.println("===========新增键值对并设置有效时间=============");
System.out.println(jedis.setex("key3", 2, "value3"));
System.out.println(jedis.get("key3"));
try {
TimeUnit.SECONDS.sleep(3L);
} catch (InterruptedException var3) {
var3.printStackTrace();
}
System.out.println(jedis.get("key3"));
System.out.println("===========获取原值,更新为新值==========");
System.out.println(jedis.getSet("key2", "key2GetSet"));
System.out.println(jedis.get("key2"));
System.out.println("获得key2的值的字串:" + jedis.getrange("key2", 2L, 4L));
}
}
3、List(列表)
public class TestList {
public TestList() {
}
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("===========添加一个list===========");
jedis.lpush("collections", new String[]{"ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap"});
jedis.lpush("collections", new String[]{"HashSet"});
jedis.lpush("collections", new String[]{"TreeSet"});
jedis.lpush("collections", new String[]{"TreeMap"});
System.out.println("collections的内容:" + jedis.lrange("collections", 0L, -1L));
System.out.println("collections区间0-3的元素:" + jedis.lrange("collections", 0L, 3L));
System.out.println("===============================");
System.out.println("删除指定元素个数:" + jedis.lrem("collections", 2L, "HashMap"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0L, -1L));
System.out.println("删除下表0-3区间之外的元素:" + jedis.ltrim("collections", 0L, 3L));
System.out.println("collections的内容:" + jedis.lrange("collections", 0L, -1L));
System.out.println("collections列表出栈(左端):" + jedis.lpop("collections"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0L, -1L));
System.out.println("collections添加元素,从列表右端,与lpush相对应:" + jedis.rpush("collections", new String[]{"EnumMap"}));
System.out.println("collections的内容:" + jedis.lrange("collections", 0L, -1L));
System.out.println("collections列表出栈(右端):" + jedis.rpop("collections"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0L, -1L));
System.out.println("修改collections指定下标1的内容:" + jedis.lset("collections", 1L, "LinkedArrayList"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0L, -1L));
System.out.println("===============================");
System.out.println("collections的长度:" + jedis.llen("collections"));
System.out.println("获取collections下标为2的元素:" + jedis.lindex("collections", 2L));
System.out.println("===============================");
jedis.lpush("sortedList", new String[]{"3", "6", "2", "0", "7", "4"});
System.out.println("sortedList排序前:" + jedis.lrange("sortedList", 0L, -1L));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList排序后:" + jedis.lrange("sortedList", 0L, -1L));
}
}
4、set(集合)
public class TestSet {
public TestSet() {
}
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("============向集合中添加元素(不重复)============");
System.out.println(jedis.sadd("eleSet", new String[]{"e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"}));
System.out.println(jedis.sadd("eleSet", new String[]{"e6"}));
System.out.println(jedis.sadd("eleSet", new String[]{"e6"}));
System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
System.out.println("删除一个元素e0:" + jedis.srem("eleSet", new String[]{"e0"}));
System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
System.out.println("删除两个元素e7和e6:" + jedis.srem("eleSet", new String[]{"e7", "e6"}));
System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));
System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));
System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
System.out.println("eleSet中包含元素的个数:" + jedis.scard("eleSet"));
System.out.println("e3是否在eleSet中:" + jedis.sismember("eleSet", "e3"));
System.out.println("e1是否在eleSet中:" + jedis.sismember("eleSet", "e1"));
System.out.println("e1是否在eleSet中:" + jedis.sismember("eleSet", "e5"));
System.out.println("=================================");
System.out.println(jedis.sadd("eleSet1", new String[]{"e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"}));
System.out.println(jedis.sadd("eleSet2", new String[]{"e1", "e2", "e4", "e3", "e0", "e8"}));
System.out.println("将eleSet1中删除e1并存入eleSet3中:" + jedis.smove("eleSet1", "eleSet3", "e1"));
System.out.println("将eleSet1中删除e2并存入eleSet3中:" + jedis.smove("eleSet1", "eleSet3", "e2"));
System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));
System.out.println("eleSet3中的元素:" + jedis.smembers("eleSet3"));
System.out.println("============集合运算=================");
System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));
System.out.println("eleSet2中的元素:" + jedis.smembers("eleSet2"));
System.out.println("eleSet1和eleSet2的交集:" + jedis.sinter(new String[]{"eleSet1", "eleSet2"}));
System.out.println("eleSet1和eleSet2的并集:" + jedis.sunion(new String[]{"eleSet1", "eleSet2"}));
System.out.println("eleSet1和eleSet2的差集:" + jedis.sdiff(new String[]{"eleSet1", "eleSet2"}));
jedis.sinterstore("eleSet4", new String[]{"eleSet1", "eleSet2"});
System.out.println("eleSet4中的元素:" + jedis.smembers("eleSet4"));
}
}
5、Hash(哈希)
public class TestHash {
public TestHash() {
}
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
Map<String, String> map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
jedis.hmset("hash", map);
jedis.hset("hash", "key5", "value5");
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("散列hash的所有键为:" + jedis.hkeys("hash"));
System.out.println("散列hash的所有值为:" + jedis.hvals("hash"));
System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:" + jedis.hincrBy("hash", "key6", 6L));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:" + jedis.hincrBy("hash", "key6", 3L));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("删除一个或者多个键值对:" + jedis.hdel("hash", new String[]{"key2"}));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("散列hash中键值对的个数:" + jedis.hlen("hash"));
System.out.println("判断hash中是否存在key2:" + jedis.hexists("hash", "key2"));
System.out.println("判断hash中是否存在key3:" + jedis.hexists("hash", "key3"));
System.out.println("获取hash中的值:" + jedis.hmget("hash", new String[]{"key3"}));
System.out.println("获取hash中的值:" + jedis.hmget("hash", new String[]{"key3", "key4"}));
}
}
测试事务
public class TestTX {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "张三");
jsonObject.put("age", "21");
jsonObject.put("sex", "boy");
// 开启事务
Transaction multi = jedis.multi();
String user = jsonObject.toJSONString();
try {
multi.set("user1", user);
multi.set("user2", user);
//执行事务
multi.exec();
} catch (Exception e) {
multi.discard(); // 出现问题,放弃事务
e.printStackTrace();
} finally {
System.out.println(jedis.mget("user1", "user2"));
jedis.close(); // 关闭连接
}
}
}
输出
[{"sex":"boy","name":"张三","age":"21"}, {"sex":"boy","name":"张三","age":"21"}]