国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Java Web】利用Spring整合Redis,配置RedisTemplate

這篇具有很好參考價值的文章主要介紹了【Java Web】利用Spring整合Redis,配置RedisTemplate。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1. 在config中加入RedisConfig配置類

package com.nowcoder.community.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 設(shè)置Key的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // 設(shè)置value的序列化方式
        template.setValueSerializer(RedisSerializer.json());
        // 設(shè)置hash的key的序列化方式
        template.setHashKeySerializer(RedisSerializer.json());
        // 設(shè)置hash的value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());

        template.afterPropertiesSet();
        return template;
    }
}

2. 寫個測試類測試一下

package com.nowcoder.community;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class RedisTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testStrings(){
        String redisKey = "test:count";
        redisTemplate.opsForValue().set(redisKey, 1);

        System.out.println(redisTemplate.opsForValue().get(redisKey));
        System.out.println(redisTemplate.opsForValue().increment(redisKey));
        System.out.println(redisTemplate.opsForValue().decrement(redisKey));
    }

    @Test
    public void testHash(){
        String redisKey = "test:user";
        redisTemplate.opsForHash().put(redisKey,"id",1);
        redisTemplate.opsForHash().put(redisKey,"name","katniss");
        redisTemplate.opsForHash().put(redisKey,"age",24);

        System.out.println(redisTemplate.opsForHash().get(redisKey,"id"));
        System.out.println(redisTemplate.opsForHash().get(redisKey,"name"));
        System.out.println(redisTemplate.opsForHash().get(redisKey,"age"));
    }

    @Test
    public void testLists(){
        String redisKey = "test:ids";
        redisTemplate.opsForList().leftPush(redisKey,101);
        redisTemplate.opsForList().leftPush(redisKey,102);
        redisTemplate.opsForList().leftPush(redisKey,103);

        System.out.println(redisTemplate.opsForList().size(redisKey));
        System.out.println(redisTemplate.opsForList().index(redisKey,0));
        System.out.println(redisTemplate.opsForList().range(redisKey,0,2));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
    }

    @Test
    public void testSets(){
        String redisKey = "test:teachers";
        redisTemplate.opsForSet().add(redisKey,"111","222","333","444","555");

        System.out.println(redisTemplate.opsForSet().size(redisKey));
        System.out.println(redisTemplate.opsForSet().pop(redisKey));
        System.out.println(redisTemplate.opsForSet().members(redisKey));
    }

    @Test
    public void testSortedSets(){
        String redisKey = "test:students";

        redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);
        redisTemplate.opsForZSet().add(redisKey, "悟空", 90);
        redisTemplate.opsForZSet().add(redisKey, "八戒", 70);
        redisTemplate.opsForZSet().add(redisKey, "沙僧", 60);

        System.out.println(redisTemplate.opsForZSet().zCard(redisKey));
        System.out.println(redisTemplate.opsForZSet().score(redisKey,"悟空"));
        System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey,"八戒"));
        System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey,0,2));
    }

    @Test
    public void testKeys(){
        System.out.println(redisTemplate.hasKey("test:user"));
        redisTemplate.delete("test:user");
        System.out.println(redisTemplate.hasKey("test:user"));

        redisTemplate.expire("test:students",10, TimeUnit.SECONDS);

    }

    // 多次訪問同一個Key
    @Test
    public void testBoundOperations(){
        String redisKey = "test:count";
        BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);

        operations.increment();
        operations.increment();
        operations.increment();
        operations.increment();
        operations.increment();
        operations.increment();
        System.out.println(operations.get());
    }

    // 編程式事務(wù)
    @Test
    public void testTransaction(){
        Object obj = redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String redisKey = "test:tx";
                operations.multi();

                operations.opsForSet().add(redisKey,"zhangsan");
                operations.opsForSet().add(redisKey,"lisi");
                operations.opsForSet().add(redisKey,"wangwu");

                System.println.out(operations.opsForSet().members(redisKey));
                return operations.exec();
            }
        });
        System.out.println(obj);
    }
}

3. 注意事項

  • Redis不滿足事務(wù)的原子性,原子性是指事務(wù)要么被全部執(zhí)行,要么都不執(zhí)行。但是Redis不支持回滾,就可能會出現(xiàn)有些語句執(zhí)行成功,有些執(zhí)行失敗,因此具備原子性;
  • Redis事務(wù)的三個階段:
    • 開始事務(wù)
    • 命令入隊
    • 順序執(zhí)行
  • 用Redis管理事務(wù)的時候中間不要做查詢,查詢會被執(zhí)行但無效。例如在testTransaction方法中,operations.opsForSet().members(redisKey)將返回空。

文章來源地址http://www.zghlxwxcb.cn/news/detail-697480.html

到了這里,關(guān)于【Java Web】利用Spring整合Redis,配置RedisTemplate的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • spring boot java項目整合Scala&Spark,接口api調(diào)用方式調(diào)用scala代碼,配置分享

    spring boot java項目整合Scala&Spark,接口api調(diào)用方式調(diào)用scala代碼,配置分享

    版本說明: spring boot: 2.5.9 jdk:1.8 spark:2.4.5 sclala:2.11.12 首先你需要有一個完美的spring boot項目(java版本)能成功運行,這就不贅述了,按照網(wǎng)上的自己搭建吧,然后重要的來了,我搗鼓了兩天時間,各樣的報錯見過了,網(wǎng)上的處理方法要嘛是不全,要嘛是沒有用,各種辦

    2024年02月10日
    瀏覽(29)
  • springboot(spring)整合redis(集群)、細(xì)節(jié)、底層配置講解

    springboot(spring)整合redis(集群)、細(xì)節(jié)、底層配置講解

    1.引入依賴. ? ? ?其實springboot整合其他主流框架直接在后面加上名稱即可,比如spring-boot-starter-redis,然后就直接可以用,可以直接注入了. ? ? ?主要原因大概就是springboot框架已經(jīng)包含了自動注入的功能,對于每一個引入的主要jar包(包含starter),都有一個factory的配置文件,里面配置

    2024年02月09日
    瀏覽(19)
  • Spring Boot 整合 Redis 全面教程:從配置到使用

    Redis 是一種高性能的鍵值存儲數(shù)據(jù)庫,而 Spring Boot 是一個簡化了開發(fā)過程的 Java 框架。將兩者結(jié)合,可以輕松地在 Spring Boot 項目中使用 Redis 來實現(xiàn)數(shù)據(jù)緩存、會話管理和分布式鎖等功能。 在 pom.xml 文件中添加 Redis 相關(guān)依賴 在 application.properties 或 application.yml 配置文件中添

    2024年02月13日
    瀏覽(23)
  • Redis哨兵集群搭建及RedisTemplate的哨兵模式配置詳解

    Redis哨兵集群搭建及RedisTemplate的哨兵模式配置詳解

    本文詳細(xì)介紹了Redis哨兵集群的原理、架構(gòu)和工作流程,包括哨兵的功能作用、故障恢復(fù)機制、選舉新的master等內(nèi)容。同時,提供了哨兵集群架構(gòu)示意圖和實例準(zhǔn)備、配置、啟動、測試的步驟。此外,還介紹了如何在Spring的RedisTemplate中配置哨兵模式,實現(xiàn)Redis主從集群的自動切換和節(jié)點感知。

    2024年02月14日
    瀏覽(27)
  • 【Java】SpringBoot快速整合Redis

    【Java】SpringBoot快速整合Redis

    ??????? 文末有源碼gitee地址 ????????【面試】淺學(xué)Redis_redis 廣播-CSDN博客 ????????Redis是一種 高性能開源的基于內(nèi)存的,采用鍵值對存儲的非關(guān)系型數(shù)據(jù)庫 ,它支持多種數(shù)據(jù)結(jié)構(gòu),包括字符串、哈希表、列表、集合、有序集合等。Redis的特點之一是 數(shù)據(jù)存儲在內(nèi)存

    2024年01月19日
    瀏覽(23)
  • 《Java Web輕量級整合開發(fā)入門》學(xué)習(xí)筆記

    《Java Web輕量級整合開發(fā)入門》學(xué)習(xí)筆記

    輕量級Java Web整合開發(fā) 第一章 輕量級Java Web開發(fā)概述 1.2? java web 開發(fā)概述 1.JSP是一種編譯執(zhí)行的前臺頁面技術(shù)。對于每個JSP頁面,Web服務(wù)器都會生成一個相應(yīng)的Java文件,然后再編譯該Java文件,生成相應(yīng)的Class類型文件。在客戶端訪問到的JSP頁面,就是相應(yīng)Class文件執(zhí)行的結(jié)果

    2024年02月08日
    瀏覽(93)
  • java阻塞隊列/kafka/spring整合kafka

    java阻塞隊列/kafka/spring整合kafka

    queue增加刪除元素 增加元素 add方法在添加元素的時候,若超出了度列的長度會直接拋出異常: put方法,若向隊尾添加元素的時候發(fā)現(xiàn)隊列已經(jīng)滿了會發(fā)生阻塞一直等待空間,以加入元素 offer方法在添加元素時,如果發(fā)現(xiàn)隊列已滿無法添加的話,會直接返回false 刪除元素 pol

    2024年02月12日
    瀏覽(22)
  • java使用redistemplate 刪除hash表

    使用 RedisTemplate 刪除 Hash 表中的數(shù)據(jù)可以使用 delete(H key, Object... hashKeys) 方法。 示例: 其中 \\\"myhash\\\" 是 Hash 表的名稱,\\\"field1\\\" 和 \\\"field2\\\" 是要刪除的字段。 也可以使用 redisTemplate.opsForHash().entries(key).clear() 清除一個 Hash 表所有的數(shù)據(jù). 需要注意的是, 如果 Hash 表不

    2024年02月16日
    瀏覽(20)
  • 【Java Web】Redis入門

    一、 Redis入門 Redis是一款基于鍵值對的NoSQL數(shù)據(jù)庫,它的值支持多種數(shù)據(jù)結(jié)構(gòu): 字符串strings,哈希hashes,列表lists,集合sets,有序列表sorted sets等。 Redis將 所有的數(shù)據(jù)都放在 內(nèi)存 中,讀寫速度非常驚人;同時Redis 還可以將內(nèi)存中的數(shù)據(jù)以快照(RDB, 整體拷貝,定時備份)或日

    2024年02月09日
    瀏覽(44)
  • RedisTemplate使用zadd報錯java.lang.StackOverflowError

    RedisTemplate使用zadd報錯java.lang.StackOverflowError

    代碼當(dāng)中使用RedisTemplate操作String、List都是正常的,但是操作zadd就會報錯,有人說是這兩個依賴的版本不一致的問題,但是項目中還有其他地方要用到,所以改版本號行不通, 下面是我操作的核心代碼 起初我認(rèn)為是版本號不一致的問題,因為線上服務(wù)器是7.0,本地是5.0,但

    2024年01月16日
    瀏覽(17)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包