Redis的Java客戶端-Jedis
在Redis官網(wǎng)中提供了各種語言的客戶端,地址:https://redis.io/docs/clients/
其中Java客戶端也包含很多但在開發(fā)中用的最多的還是Jedis,接下來就讓我們以Jedis開始我們的快速實戰(zhàn)。
Jedis快速入門
入門案例詳細(xì)步驟
案例分析:
創(chuàng)建工程:
創(chuàng)建一個maven管理的java項目
引入依賴:
在pom.xml文件下添加以下依賴
<dependencies>
<!--jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
建立連接
新建一個單元測試類,內(nèi)容如下:
private Jedis jedis;
@BeforeEach
void setUp() {
// 1.建立連接
jedis = new Jedis("192.168.218.134", 6379);
// jedis = JedisConnectionFactory.getJedis();
// 2.設(shè)置密碼
jedis.auth("123456");
// 3.選擇庫
jedis.select(0);
}
測試:
@Test
void testString() {
// 存入數(shù)據(jù)
String result = jedis.set("name", "onenewcode");
System.out.println("result = " + result);
// 獲取數(shù)據(jù)
String name = jedis.get("name");
System.out.println("name = " + name);
}
@Test
void testHash() {
// 插入hash數(shù)據(jù)
jedis.hset("user:1", "name", "Jack");
jedis.hset("user:1", "age", "21");
// 獲取
Map<String, String> map = jedis.hgetAll("user:1");
System.out.println(map);
}
釋放資源
@AfterEach
void tearDown() {
if (jedis != null) {
jedis.close();
}
}
Jedis連接池
Jedis本身是線程不安全的,并且頻繁的創(chuàng)建和銷毀連接會有性能損耗,因此我們推薦大家使用Jedis連接池代替Jedis的直連方式
有關(guān)池化思想,并不僅僅是這里會使用,很多地方都有,比如說我們的數(shù)據(jù)庫連接池,比如我們tomcat中的線程池,這些都是池化思想的體現(xiàn)。
創(chuàng)建Jedis的連接池
public class JedisConnectionFacotry {
private static final JedisPool jedisPool;
static {
//配置連接池
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(8);
poolConfig.setMaxIdle(8);
poolConfig.setMinIdle(0);
poolConfig.setMaxWaitMillis(1000);
//創(chuàng)建連接池對象
jedisPool = new JedisPool(poolConfig,
"192.168.218.134",6379,1000,"123456");
}
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
代碼說明:
-
1) JedisConnectionFacotry:工廠設(shè)計模式是實際開發(fā)中非常常用的一種設(shè)計模式,我們可以使用工廠,去降低代的耦合,比如Spring中的Bean的創(chuàng)建,就用到了工廠設(shè)計模式
-
2)靜態(tài)代碼塊:隨著類的加載而加載,確保只能執(zhí)行一次,我們在加載當(dāng)前工廠類的時候,就可以執(zhí)行static的操作完成對 連接池的初始化
-
3)最后提供返回連接池中連接的方法.
改造原始代碼
代碼說明:
1.在我們完成了使用工廠設(shè)計模式來完成代碼的編寫之后,我們在獲得連接時,就可以通過工廠來獲得。
,而不用直接去new對象,降低耦合,并且使用的還是連接池對象。
2.當(dāng)我們使用了連接池后,當(dāng)我們關(guān)閉連接其實并不是關(guān)閉,而是將Jedis還回連接池的。
@BeforeEach
void setUp(){
//建立連接
/*jedis = new Jedis("127.0.0.1",6379);*/
jedis = JedisConnectionFacotry.getJedis();
//選擇庫
jedis.select(0);
}
@AfterEach
void tearDown() {
if (jedis != null) {
jedis.close();
}
}
Redis的Java客戶端-SpringDataRedis
SpringData是Spring中數(shù)據(jù)操作的模塊,包含對各種數(shù)據(jù)庫的集成,其中對Redis的集成模塊就叫做SpringDataRedis,官網(wǎng)地址:https://spring.io/projects/spring-data-redis
- 提供了對不同Redis客戶端的整合(Lettuce和Jedis)
- 提供了RedisTemplate統(tǒng)一API來操作Redis
- 支持Redis的發(fā)布訂閱模型
- 支持Redis哨兵和Redis集群
- 支持基于Lettuce的響應(yīng)式編程
- 支持基于JDK.JSON.字符串.Spring對象的數(shù)據(jù)序列化及反序列化
- 支持基于Redis的JDKCollection實現(xiàn)
SpringDataRedis中提供了RedisTemplate工具類,其中封裝了各種對Redis的操作。并且將不同數(shù)據(jù)類型的操作API封裝到了不同的類型中:
快速入門
SpringBoot已經(jīng)提供了對SpringDataRedis的支持,使用非常簡,首先創(chuàng)建一個spring boot項目
導(dǎo)入pom坐標(biāo)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.onenewcode</groupId>
<artifactId>MyRedis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyRedis</name>
<description>MyRedis</description>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.0.2</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--redis依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--common-pool-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!--Jackson依賴-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.onenewcode.myredis.MyRedisApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
配置文件
在application.yml文件下添加以下內(nèi)容
spring:
data:
redis:
host: 192.168.218.134
port: 6379
password: 123456
lettuce:
pool:
max-active: 8 #最大連接
max-idle: 8 #最大空閑連接
min-idle: 0 #最小空閑連接
max-wait: 100ms #連接等待時間
測試代碼
@SpringBootTest
class MyRedisApplicationTests {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Test
void testString() {
// 寫入一條String數(shù)據(jù)
redisTemplate.opsForValue().set("name", "onenewcode");
// 獲取string數(shù)據(jù)
Object name = redisTemplate.opsForValue().get("name");
System.out.println("name = " + name);
}
}
貼心小提示:SpringDataJpa使用起來非常簡單,記住如下幾個步驟即可
SpringDataRedis的使用步驟:
- 引入spring-boot-starter-data-redis依賴
- 在application.yml配置Redis信息
- 注入RedisTemplate
目錄結(jié)構(gòu)
數(shù)據(jù)序列化器
RedisTemplate可以接收任意Object作為值寫入Redis,只不過寫入前會把Object序列化為字節(jié)形式,默認(rèn)是采用JDK序列化,得到的結(jié)果是這樣的:
缺點:
- 可讀性差
- 內(nèi)存占用較大
我們可以自定義RedisTemplate的序列化方式,代碼如下:
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){
// 創(chuàng)建RedisTemplate對象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 設(shè)置連接工廠
template.setConnectionFactory(connectionFactory);
// 創(chuàng)建JSON序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer =
new GenericJackson2JsonRedisSerializer();
// 設(shè)置Key的序列化
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
// 設(shè)置Value的序列化
template.setValueSerializer(jsonRedisSerializer);
template.setHashValueSerializer(jsonRedisSerializer);
// 返回
return template;
}
}
這里采用了JSON序列化來代替默認(rèn)的JDK序列化方式。最終結(jié)果如圖:
整體可讀性有了很大提升,并且能將Java對象自動的序列化為JSON字符串,并且查詢時能自動把JSON反序列化為Java對象。不過,其中記錄了序列化時對應(yīng)的class名稱,目的是為了查詢時實現(xiàn)自動反序列化。這會帶來額外的內(nèi)存開銷。
StringRedisTemplate
盡管JSON的序列化方式可以滿足我們的需求,但依然存在一些問題,自動進(jìn)行序列化時會負(fù)載多余的信息。
為了在反序列化時知道對象的類型,JSON序列化器會將類的class類型寫入json結(jié)果中,存入Redis,會帶來額外的內(nèi)存開銷。
為了減少內(nèi)存的消耗,我們可以采用手動序列化的方式,換句話說,就是不借助默認(rèn)的序列化器,而是我們自己來控制序列化的動作,同時,我們只采用String的序列化器,這樣,在存儲value時,我們就不需要在內(nèi)存中就不用多存儲數(shù)據(jù),從而節(jié)約我們的內(nèi)存空間
這種用法比較普遍,因此SpringDataRedis就提供了RedisTemplate的子類:StringRedisTemplate,它的key和value的序列化方式默認(rèn)就是String方式。
省去了我們自定義RedisTemplate的序列化方式的步驟,而是直接使用:
@SpringBootTest
class MyRedisApplicationTests {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
// JSON工具
private static final ObjectMapper mapper = new ObjectMapper();
@Test
void testString() {
// 寫入一條String數(shù)據(jù)
redisTemplate.opsForValue().set("1", "onenewcode");
// 獲取string數(shù)據(jù)
Object name = redisTemplate.opsForValue().get("name");
System.out.println("name = " + name);
}
@Test
void testSaveUser() throws JsonProcessingException {
// 創(chuàng)建對象
User user = new User("onenewcode", 21);
// 手動序列化
String json = mapper.writeValueAsString(user);
// 寫入數(shù)據(jù)
stringRedisTemplate.opsForValue().set("user:200", json);
// 獲取數(shù)據(jù)
String jsonUser = stringRedisTemplate.opsForValue().get("user:200");
// 手動反序列化
User user1 = mapper.readValue(jsonUser, User.class);
System.out.println("user1 = " + user1);
}
}
此時我們再來看一看存儲的數(shù)據(jù),小伙伴們就會發(fā)現(xiàn)那個class數(shù)據(jù)已經(jīng)不在了,節(jié)約了我們的空間~
最后小總結(jié):
RedisTemplate的兩種序列化實踐方案:
-
方案一:
- 自定義RedisTemplate
- 修改RedisTemplate的序列化器為GenericJackson2JsonRedisSerializer
-
方案二:
- 使用StringRedisTemplate
- 寫入Redis時,手動把對象序列化為JSON
- 讀取Redis時,手動把讀取到的JSON反序列化為對象
Hash結(jié)構(gòu)操作
在基礎(chǔ)篇的最后,咱們對Hash結(jié)構(gòu)操作一下,收一個小尾巴,這個代碼咱們就不再解釋啦
馬上就開始新的篇章~~~進(jìn)入到我們的Redis實戰(zhàn)篇文章來源:http://www.zghlxwxcb.cn/news/detail-745245.html
@SpringBootTest
class RedisStringTests {
···
@Test
void testHash() {
stringRedisTemplate.opsForHash().put("user:400", "name", "onenewcode");
stringRedisTemplate.opsForHash().put("user:400", "age", "21");
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("user:400");
System.out.println("entries = " + entries);
}
}
項目倉庫
https://github.com/onenewcode/MyRedis.git
文章來源地址http://www.zghlxwxcb.cn/news/detail-745245.html
到了這里,關(guān)于redis教程 二 redis客戶端Jedis使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!