欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中操作Redis及工具類的封裝詳解

 更新時間:2023年05月06日 10:16:16   作者:JK凱  
在我們項目開發(fā)中總是免不了會使用緩存,Redis現(xiàn)在基本是我們公司中非常常見的緩存方案,包括在用戶token的緩存,熱點信息的緩存等,這篇文章主要講講在SpringBoot項目中如何去操作Redis,及最后工具類的封裝

一、引入依賴及進行配置

1.maven依賴的引入(繼承了SpringBoot的父模塊,所以不需要再聲明版本)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.application.yml配置

spring:
  redis:
    host: localhost
    port: 6379
    password:
    database: 0 # 指定redis的分庫(共16個,0-15)

二、使用

使用示例:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/get")
    public Object getUser() {
        return redisTemplate.opsForValue().get("user");
    }
    @GetMapping("/set")
    public String setUser(String user) {
        redisTemplate.opsForValue().set("user", user);
        return "success";
    }
}

主要是自動裝配好redis依賴提供給我們的RedisTemplate,然后通過redisTemplate來進行getset的操作

RedisTemplate Api(部分展示)

三、Redis 操作工具類的封裝

redis相應的工具類封裝,主要是對各種數(shù)據(jù)類型操作更方便,設置緩存過期時間等。

utils.RedisCache:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public class RedisCache {
    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 緩存基本的對象,Integer、String、實體類等
     *
     * @param key   緩存的鍵值
     * @param value 緩存的值
     */
    public <T> void setCacheObject(final String key, final T value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 緩存基本的對象,Integer、String、實體類等
     *
     * @param key      緩存的鍵值
     * @param value    緩存的值
     * @param timeout  時間
     * @param timeUnit 時間顆粒度
     */
    public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    /**
     * 設置有效時間
     *
     * @param key     Redis鍵
     * @param timeout 超時時間
     * @return true=設置成功;false=設置失敗
     */
    public boolean expire(final String key, final long timeout) {
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 設置有效時間
     *
     * @param key     Redis鍵
     * @param timeout 超時時間
     * @param unit    時間單位
     * @return true=設置成功;false=設置失敗
     */
    public boolean expire(final String key, final long timeout, final TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    /**
     * 獲得緩存的基本對象。
     *
     * @param key 緩存鍵值
     * @return 緩存鍵值對應的數(shù)據(jù)
     */
    public <T> T getCacheObject(final String key) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }

    /**
     * 刪除單個對象
     *
     * @param key
     */
    public boolean deleteObject(final String key) {
        return redisTemplate.delete(key);
    }

    /**
     * 刪除集合對象
     *
     * @param collection 多個對象
     * @return
     */
    public long deleteObject(final Collection collection) {
        return redisTemplate.delete(collection);
    }

    /**
     * 緩存List數(shù)據(jù)
     *
     * @param key      緩存的鍵值
     * @param dataList 待緩存的List數(shù)據(jù)
     * @return 緩存的對象
     */
    public <T> long setCacheList(final String key, final List<T> dataList) {
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }

    /**
     * 獲得緩存的list對象
     *
     * @param key 緩存的鍵值
     * @return 緩存鍵值對應的數(shù)據(jù)
     */
    public <T> List<T> getCacheList(final String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    /**
     * 緩存Set
     *
     * @param key     緩存鍵值
     * @param dataSet 緩存的數(shù)據(jù)
     * @return 緩存數(shù)據(jù)的對象
     */
    public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) {
        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
        Iterator<T> it = dataSet.iterator();
        while (it.hasNext()) {
            setOperation.add(it.next());
        }
        return setOperation;
    }

    /**
     * 獲得緩存的set
     *
     * @param key
     * @return
     */
    public <T> Set<T> getCacheSet(final String key) {
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 緩存Map
     *
     * @param key
     * @param dataMap
     */
    public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key, dataMap);
        }
    }

    /**
     * 獲得緩存的Map
     *
     * @param key
     * @return
     */
    public <T> Map<String, T> getCacheMap(final String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 往Hash中存入數(shù)據(jù)
     *
     * @param key   Redis鍵
     * @param hKey  Hash鍵
     * @param value 值
     */
    public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
        redisTemplate.opsForHash().put(key, hKey, value);
    }

    /**
     * 獲取Hash中的數(shù)據(jù)
     *
     * @param key  Redis鍵
     * @param hKey Hash鍵
     * @return Hash中的對象
     */
    public <T> T getCacheMapValue(final String key, final String hKey) {
        HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
        return opsForHash.get(key, hKey);
    }


    public void incrementCacheMapValue(String key, String hKey, int v) {
        redisTemplate.opsForHash().increment(key, hKey, v);
    }

    /**
     * 刪除Hash中的數(shù)據(jù)
     *
     * @param key
     * @param hkey
     */
    public void delCacheMapValue(final String key, final String hkey) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.delete(key, hkey);
    }

    /**
     * 獲取多個Hash中的數(shù)據(jù)
     *
     * @param key   Redis鍵
     * @param hKeys Hash鍵集合
     * @return Hash對象集合
     */
    public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    /**
     * 獲得緩存的基本對象列表
     *
     * @param pattern 字符串前綴
     * @return 對象列表
     */
    public Collection<String> keys(final String pattern) {
        return redisTemplate.keys(pattern);
    }
}

使用:

import com.jk.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RedisCache redisCache;
    @GetMapping("/get")
    public Object getUser() {
        return redisCache.getCacheObject("user");
    }
    @GetMapping("/set")
    public String setUser(String user) {
        redisCache.setCacheObject("user", user);
        return "success";
    }
}

以上就是SpringBoot中操作Redis及工具類的封裝詳解的詳細內容,更多關于SpringBoot操作Redis的資料請關注腳本之家其它相關文章!

相關文章

  • IDEA 中使用 Big Data Tools 連接大數(shù)據(jù)組件

    IDEA 中使用 Big Data Tools 連接大數(shù)據(jù)組件

    本文主要介紹了IDEA 中使用 Big Data Tools 連接大數(shù)據(jù)組件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • java基礎之 Arrays.toString()方法詳解

    java基礎之 Arrays.toString()方法詳解

    這篇文章主要介紹了java基礎之 Arrays.toString()方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java使用ES?Client?調用滾動查詢及Elasticsearch滾動查詢Scrolling機制

    Java使用ES?Client?調用滾動查詢及Elasticsearch滾動查詢Scrolling機制

    Elasticsearch提供了一種稱為"滾動查詢"(Scrolling)的機制,用于處理大型數(shù)據(jù)集的分頁查詢,這篇文章給大家介紹滾動查詢的一般步驟及Java使用ESClient調用滾動查詢的方法,感興趣的朋友一起看看吧
    2023-08-08
  • springboot 基于Tomcat容器的自啟動流程分析

    springboot 基于Tomcat容器的自啟動流程分析

    這篇文章主要介紹了springboot 基于Tomcat容器的自啟動流程分析,Spring通過注解導入Bean大體可分為四種方式,我們主要來說Import的兩種實現(xiàn)方法,需要的朋友可以參考下
    2020-02-02
  • Java實現(xiàn)俄羅斯方塊游戲簡單版

    Java實現(xiàn)俄羅斯方塊游戲簡單版

    這篇文章主要為大家詳細介紹了Java實現(xiàn)俄羅斯方塊游戲簡單版,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • SpringBoot 統(tǒng)一請求返回的實現(xiàn)

    SpringBoot 統(tǒng)一請求返回的實現(xiàn)

    這篇文章主要介紹了SpringBoot 統(tǒng)一請求返回的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • java并發(fā)之原子操作類和非阻塞算法

    java并發(fā)之原子操作類和非阻塞算法

    這篇文章主要為大家詳細介紹了java并發(fā)之原子操作類和非阻塞算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • springboot如何讀取自定義屬性

    springboot如何讀取自定義屬性

    大家好,本篇文章主要講的是springboot如何讀取自定義屬性,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • 詳解SpringMVC從基礎到源碼

    詳解SpringMVC從基礎到源碼

    這篇文章主要介紹了詳解SpringMVC從基礎到源碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 一文帶你全面了解Java?Properties類

    一文帶你全面了解Java?Properties類

    Properties是JDK1.0中引入的java類,目前也在項目中大量使用,主要用來讀取外部的配置,那除了這個,你對它其他的一些api也了解嗎??你了解它是怎么實現(xiàn)的嗎??如果不清楚的話,就通過本篇文章帶你一探究竟
    2022-09-09

最新評論