Jedis項目搭建
Jedis是Redis官方推薦的Java連接開發工具。要在Java開發中使用好Redis中間件,必須對Jedis熟悉才能
寫成漂亮的代碼
1、導入相關依賴
<dependencies>
<!--導入jedis包-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.0</version>
</dependency>
<!--導入fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
2、application.properties配置redis配置
# 應用名稱
spring.application.name=redis_springboot
#Redis服務器地址
spring.redis.host=192.168.137.4
#Redis服務器連接端口
spring.redis.port=6379
#Redis數據庫索引(默認為0)
spring.redis.database= 0
#連接超時時間(毫秒)
spring.redis.timeout=1800000
#連接池最大連接數(使用負值表示沒有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待時間(負數表示沒限制)
spring.redis.lettuce.pool.max-wait=-1
#連接池中的最大空閑連接
spring.redis.lettuce.pool.max-idle=5
#連接池中的最小空閑連接
spring.redis.lettuce.pool.min-idle=0
3、編寫測試類測試是否ping成功
注意:必須要在配置文件取消本地和安全模式
package com.study;
import redis.clients.jedis.Jedis;
public class TestPing {
public static void main(String[] args) {
//1.new Jedis對象
Jedis jedis = new Jedis("192.168.137.4",6379);
System.out.println(jedis.ping());
}
}
Jedis常用API
一、key操作
package com.study;
import redis.clients.jedis.Jedis;
public class TestKey {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.137.4",6379);
System.out.println("清空數據:"+jedis.flushDB());
System.out.println("判斷某個鍵是否存在:"+jedis.exists("username"));
System.out.println("新增<username,zhangsan>的鍵值對:"+jedis.set("username","zhangsan"));
System.out.println("新增<password,password>的鍵值對:"+jedis.set("password","password"));
System.out.println("查看所有的key:"+jedis.keys("*"));
System.out.println("刪除key鍵password:"+jedis.del("password"));
System.out.println("判斷key鍵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("清空數據庫:"+jedis.flushDB());
System.out.println("返回當前數據庫中key的數目:"+jedis.dbSize());
System.out.println("清空所有數據庫:"+jedis.flushAll());
}
}
二、string操作
package com.study;
import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit;
public class TestString {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.137.4", 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("添加多個鍵值對:"+jedis.mset("key01","value01","key02","value02","key03","value03"));
System.out.println("獲取多個鍵值對:"+jedis.mget("key01","key02","key03"));
System.out.println("刪除多個鍵值對:"+jedis.del("key01","key02"));
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.mget("key1","key2"));
System.out.println("========新增鍵值對並設置有效時間==========");
System.out.println(jedis.setex("key3",2,"value3")); //有效時間為2秒
System.out.println(jedis.get("key3"));
try {
TimeUnit.SECONDS.sleep(3);
}catch (InterruptedException e){
e.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",2,4));
}
}
三、set操作
package com.study;
import redis.clients.jedis.Jedis;
public class TestSet {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.137.4",6379);
jedis.flushDB();
System.out.println("============向集合中添加元素(不重復)============");
System.out.println(jedis.sadd("eleSet", "e1","e2","e4","e3","e0","e8","e7","e5"));
System.out.println(jedis.sadd("eleSet", "e6"));
System.out.println(jedis.sadd("eleSet", "e6"));
System.out.println("eleSet的所有元素為:"+jedis.smembers("eleSet"));
System.out.println("刪除一個元素e0:"+jedis.srem("eleSet", "e0"));
System.out.println("eleSet的所有元素為:"+jedis.smembers("eleSet"));
System.out.println("刪除兩個元素e7和e6:"+jedis.srem("eleSet", "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", "e1","e2","e4","e3","e0","e8","e7","e5"));
System.out.println(jedis.sadd("eleSet2", "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("eleSet1","eleSet2"));
System.out.println("eleSet1和eleSet2的並 集:"+jedis.sunion("eleSet1","eleSet2"));
System.out.println("eleSet1和eleSet2的差 集:"+jedis.sdiff("eleSet1","eleSet2"));//eleSet1中有,eleSet2中沒有
jedis.sinterstore("eleSet4","eleSet1","eleSet2");//求交集並將交集保存到dstkey的集合
System.out.println("eleSet4中的元素:"+jedis.smembers("eleSet4"));
}
}
四、List操作
package com.study;
import redis.clients.jedis.Jedis;
public class TestList {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.137.4", 6379);
jedis.flushDB();
System.out.println("===========添加一個list===========");
jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
jedis.lpush("collections", "HashSet");
jedis.lpush("collections", "TreeSet");
jedis.lpush("collections", "TreeMap");
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));//-1代表倒數第一個元素,-2代表倒數第二個元素,end為-1表示查詢全部
System.out.println("collections區間0-3的元 素:"+jedis.lrange("collections",0,3));
System.out.println("===============================");
// 刪除列表指定的值 ,第二個參數為刪除的個數(有重復時),后add進去的值先被刪,類似於出棧
System.out.println("刪除指定元素個數:"+jedis.lrem("collections", 2, "HashMap"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("刪除下表0-3區間之外的元 素:"+jedis.ltrim("collections", 0, 3));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出棧(左 端):"+jedis.lpop("collections"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections添加元素,從列表右端,與lpush相對 應:"+jedis.rpush("collections", "EnumMap"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出棧(右 端):"+jedis.rpop("collections"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("修改collections指定下標1的內 容:"+jedis.lset("collections", 1, "LinkedArrayList"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("===============================");
System.out.println("collections的長度:"+jedis.llen("collections"));
System.out.println("獲取collections下標為2的元 素:"+jedis.lindex("collections", 2));
System.out.println("===============================");
jedis.lpush("sortedList", "3","6","2","0","7","4");
System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList排序后:"+jedis.lrange("sortedList", 0, -1));
}
}
五、Hash操作
package com.study;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
public class TestHash {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.137.4", 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");
//添加名稱為hash(key)的hash元素
jedis.hmset("hash",map);
//向名稱為hash的hash中添加key為key5,value為value5元素
jedis.hset("hash", "key5", "value5");
System.out.println("散列hash的所有鍵值對 為:"+jedis.hgetAll("hash"));//return Map<String,String>
System.out.println("散列hash的所有鍵為:"+jedis.hkeys("hash"));//return Set<String>
System.out.println("散列hash的所有值為:"+jedis.hvals("hash"));//return List<String>
System.out.println("將key6保存的值加上一個整數,如果key6不存在則添加 key6:"+jedis.hincrBy("hash", "key6", 6));
System.out.println("散列hash的所有鍵值對為:"+jedis.hgetAll("hash"));
System.out.println("將key6保存的值加上一個整數,如果key6不存在則添加 key6:"+jedis.hincrBy("hash", "key6", 3));
System.out.println("散列hash的所有鍵值對為:"+jedis.hgetAll("hash"));
System.out.println("刪除一個或者多個鍵值對:"+jedis.hdel("hash", "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","key3"));
System.out.println("獲取hash中的 值:"+jedis.hmget("hash","key3","key4"));
}
}
Redis整合springboot
1、新建一個springboot web項目
2、導入相關依賴
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--springboot集成redis所需的commnos-pool2依賴-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
3、編寫controller層
package com.study.redis_springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/redisTest")
public class RedisTestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping
public String testRedis(){
//opsForValue 操作字符串
//opsForList 操作list
//opsForSet 操作set
//opsForHash 操作hash
//除了基本的操作,我們常用的方法都可以直接通過redisTemplate操作,比如事務和基本的CRUD
redisTemplate.opsForValue().set("name","lucy");
String name = (String) redisTemplate.opsForValue().get("name");
return name;
}
}