最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot 整合 Lettuce Redis的實(shí)現(xiàn)方法

 更新時間:2019年07月01日 10:27:55   作者:Java_老男孩  
這篇文章主要介紹了SpringBoot 整合 Lettuce Redis的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

SpringBoot 是為了簡化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕易的搭建出一個 WEB 工程

Spring Boot 除了支持常見的ORM框架外,更是對常用的中間件提供了非常好封裝,隨著Spring Boot2.x的到來,支持的組件越來越豐富,也越來越成熟,其中對Redis的支持不僅僅是豐富了它的API,更是替換掉底層Jedis的依賴,取而代之換成了Lettuce(生菜)

Redis介紹

Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。相比Memcached它支持存儲的類型相對更多(字符、哈希、集合、有序集合、列表、GEO),同時Redis是線程安全的。2010年3月15日起,Redis的開發(fā)工作由VMware主持,2013年5月開始,Redis的開發(fā)由Pivotal贊助。

Lettuce

Lettuce 和 Jedis 的都是連接Redis Server的客戶端程序。Jedis在實(shí)現(xiàn)上是直連redis server,多線程環(huán)境下非線程安全,除非使用連接池,為每個Jedis實(shí)例增加物理連接。Lettuce基于Netty的連接實(shí)例(StatefulRedisConnection),可以在多個線程間并發(fā)訪問,且線程安全,滿足多線程環(huán)境下的并發(fā)訪問,同時它是可伸縮的設(shè)計,一個連接實(shí)例不夠的情況也可以按需增加連接實(shí)例。

導(dǎo)入依賴

在 pom.xml 中spring-boot-starter-data-redis的依賴,Spring Boot2.x 后底層不在是Jedis如果做版本升級的朋友需要注意下

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

屬性配置

在 application.properties 文件中配置如下內(nèi)容,由于Spring Boot2.x 的改動,連接池相關(guān)配置需要通過spring.redis.lettuce.pool或者 spring.redis.jedis.pool 進(jìn)行配置了

spring.redis.host=localhost
spring.redis.password=battcn
# 連接超時時間(毫秒)
spring.redis.timeout=10000
# Redis默認(rèn)情況下有16個分片,這里配置具體使用的分片,默認(rèn)是0
spring.redis.database=0
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) 默認(rèn) 8
spring.redis.lettuce.pool.max-active=8
# 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制) 默認(rèn) -1
spring.redis.lettuce.pool.max-wait=-1
# 連接池中的最大空閑連接 默認(rèn) 8
spring.redis.lettuce.pool.max-idle=8
# 連接池中的最小空閑連接 默認(rèn) 0
spring.redis.lettuce.pool.min-idle=0

具體編碼

Spring Boot對Redis的支持已經(jīng)非常完善了,良好的序列化以及豐富的API足夠應(yīng)對日常開發(fā)

實(shí)體類

創(chuàng)建一個User類

package com.battcn.entity;

import java.io.Serializable;

/**
 * @author Levin
 * @since 2018/5/10 0007
 */
public class User implements Serializable {

  private static final long serialVersionUID = 8655851615465363473L;
  private Long id;
  private String username;
  private String password;
  // TODO 省略get set
}

自定義Template

默認(rèn)情況下的模板只能支持RedisTemplate&lt;String, String&gt;,也就是只能存入字符串,這在開發(fā)中是不友好的,所以自定義模板是很有必要的,當(dāng)自定義了模板又想使用String存儲這時候就可以使用StringRedisTemplate的方式,它們并不沖突…

package com.battcn.config;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * TODO 修改database
 *
 * @author Levin
 * @since 2018/5/10 0022
 */
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisCacheAutoConfiguration {

  @Bean
  public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
  }
}

測試

完成準(zhǔn)備事項(xiàng)后,編寫一個junit測試類來檢驗(yàn)代碼的正確性,有很多人質(zhì)疑過Redis線程安全性,故下面也提供了響應(yīng)的測試案例,如有疑問歡迎指正

package com.battcn;

import com.battcn.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.Serializable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * @author Levin
 * @since 2018/5/10 0010
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter8ApplicationTest {

  private static final Logger log = LoggerFactory.getLogger(Chapter8ApplicationTest.class);

  @Autowired
  private StringRedisTemplate stringRedisTemplate;

  @Autowired
  private RedisTemplate<String, Serializable> redisCacheTemplate;

  @Test
  public void get() {
    // TODO 測試線程安全
    ExecutorService executorService = Executors.newFixedThreadPool(1000);
    IntStream.range(0, 1000).forEach(i ->
        executorService.execute(() -> stringRedisTemplate.opsForValue().increment("kk", 1))
    );
    stringRedisTemplate.opsForValue().set("k1", "v1");
    final String k1 = stringRedisTemplate.opsForValue().get("k1");
    log.info("[字符緩存結(jié)果] - [{}]", k1);
    // TODO 以下只演示整合,具體Redis命令可以參考官方文檔,Spring Data Redis 只是改了個名字而已,Redis支持的命令它都支持
    String key = "battcn:user:1";
    redisCacheTemplate.opsForValue().set(key, new User(1L, "u1", "pa"));
    // TODO 對應(yīng) String(字符串)
    final User user = (User) redisCacheTemplate.opsForValue().get(key);
    log.info("[對象緩存結(jié)果] - [{}]", user);
  }
}

其它類型

下列的就是Redis其它類型所對應(yīng)的操作方式

  • opsForValue: 對應(yīng) String(字符串)
  • opsForZSet: 對應(yīng) ZSet(有序集合)
  • opsForHash: 對應(yīng) Hash(哈希)
  • opsForList: 對應(yīng) List(列表)
  • opsForSet: 對應(yīng) Set(集合)
  • opsForGeo: 對應(yīng) GEO(地理位置)

總結(jié)

spring-data-redis文檔: https://docs.spring.io/spring-data/redis/docs/2.0.1.RELEASE/reference/html/#new-in-2.0.0

Redis 文檔: https://redis.io/documentation

Redis 中文文檔: http://www.redis.cn/commands.html

目前很多大佬都寫過關(guān)于 SpringBoot 的教程了,如有雷同,請多多包涵,本教程基于最新的 spring-boot-starter-parent:2.0.1.RELEASE編寫,包括新版本的特性都會一起介紹

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java反射如何修改private final成員變量值

    Java反射如何修改private final成員變量值

    這篇文章主要介紹了Java反射如何修改private final成員變量值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Spring Cloud下實(shí)現(xiàn)用戶鑒權(quán)的方案

    Spring Cloud下實(shí)現(xiàn)用戶鑒權(quán)的方案

    Java下常用的安全框架主要有Spring Security和shiro,都可提供非常強(qiáng)大的功能,但學(xué)習(xí)成本較高。但在微服務(wù)下鑒權(quán)又會對服務(wù)有一定的入侵性。 因此,本文將介紹Spring Cloud下實(shí)現(xiàn)用戶鑒權(quán)的方案,感興趣的同學(xué)可以關(guān)注一下
    2021-11-11
  • SpringBoot中的異常處理與參數(shù)校驗(yàn)的方法實(shí)現(xiàn)

    SpringBoot中的異常處理與參數(shù)校驗(yàn)的方法實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot中的異常處理與參數(shù)校驗(yàn)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Java如何獲取字符串單詞個數(shù)

    Java如何獲取字符串單詞個數(shù)

    這篇文章主要介紹了Java如何獲取字符串單詞個數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • springboot filter實(shí)現(xiàn)請求響應(yīng)全鏈路攔截

    springboot filter實(shí)現(xiàn)請求響應(yīng)全鏈路攔截

    這篇文章主要為大家詳細(xì)介紹了SpringBoot如何結(jié)合Filter同時攔截請求和響應(yīng),從而實(shí)現(xiàn)??日志采集自動化,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-04-04
  • JavaWeb 實(shí)現(xiàn)驗(yàn)證碼功能(demo)

    JavaWeb 實(shí)現(xiàn)驗(yàn)證碼功能(demo)

    在 WEB-APP 中一般應(yīng)用于:登錄、注冊、買某票、秒殺等場景,大家都接觸過這個驗(yàn)證碼操作,今天小編通過實(shí)例代碼給大家講解javaweb實(shí)現(xiàn)驗(yàn)證碼功能,需要的朋友參考下
    2017-02-02
  • 淺談一下Java中的訪問修飾符以及作用

    淺談一下Java中的訪問修飾符以及作用

    這篇文章主要介紹了淺談一下Java中的訪問修飾符以及作用,修飾符修飾的是“被訪問”的權(quán)限,所有修飾符都可以修飾成員變量,方法,構(gòu)造方法,需要的朋友可以參考下
    2023-05-05
  • 使用dynamic datasource springboot starter實(shí)現(xiàn)多數(shù)據(jù)源及源碼分析

    使用dynamic datasource springboot starter實(shí)現(xiàn)多數(shù)據(jù)源及源碼分析

    這篇文章主要介紹了使用dynamic-datasource-spring-boot-starter做多數(shù)據(jù)源及源碼分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • SpringBoot Security安裝配置及Thymeleaf整合

    SpringBoot Security安裝配置及Thymeleaf整合

    這篇文章主要介紹了SpringBoot Security安裝配置及Thymeleaf整合,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • 淺談Arrays.asList()方法的使用

    淺談Arrays.asList()方法的使用

    本文主要介紹了Arrays.asList()方法的使用。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02

最新評論

新野县| 临颍县| 虞城县| 闵行区| 洪江市| 井研县| 瓮安县| 喀什市| 鄂尔多斯市| 万年县| 南充市| 沐川县| 东莞市| 瓮安县| 上林县| 江川县| 仁怀市| 商南县| 苍梧县| 济源市| 凤台县| 吉安县| 莱西市| 凤城市| 徐水县| 榆林市| 罗江县| 光泽县| 铁岭市| 金塔县| 襄樊市| 静乐县| 色达县| 石台县| 香格里拉县| 昭觉县| 富裕县| 平武县| 天长市| 吴旗县| 抚顺县|