目錄
使用:
首頁 資料庫 Redis 如何使用註解實現Redis快取功能

如何使用註解實現Redis快取功能

May 29, 2023 pm 10:04 PM
redis

c語言編寫的key,value儲存系統(區別於MySQL的二維表格的形式儲存。)

rdb:週期性的持久化

aof:以日誌形式追加

預設rdb開啟,同時開啟使用aof

資料類型:string、list、set、zset、hash、

bitMaps 位元組形式儲存、geospatial 經緯度類型.. .

單一執行緒:採用多路io復用實作高並發

使用:

新增依賴##

<!-- redis -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-pool2</artifactId>
 <version>2.6.0</version>
</dependency>
登入後複製

建立配置類別固定寫法

package com.lzq.yygh.common;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
import java.net.UnknownHostException;
import java.time.Duration;
@Configuration
@EnableCaching  //开启缓存功能
public class RedisConfig {
/**
 * 设置RedisTemplate规则
 * @param redisConnectionFactory
 * @return
 */
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    //解决查询缓存转换异常的问题
    ObjectMapper om = new ObjectMapper();
    // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
     om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等
                om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
     jackson2JsonRedisSerializer.setObjectMapper(om);
    //序列号key value
     redisTemplate.setKeySerializer(new StringRedisSerializer());
     redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
     redisTemplate.setHashKeySerializer(new StringRedisSerializer());
     redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
     redisTemplate.afterPropertiesSet();
     return redisTemplate;
}
    /**
     * 设置CacheManager缓存规则
     * @param factory
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        //解决查询缓存转换异常的问题
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 配置序列化(解决乱码的问题),过期时间600秒
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600)) //缓存过期10分钟 ---- 业务需求。
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))//设置key的序列化方式
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) //设置value的序列化
                .disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }
}
登入後複製

新增設定資訊

spring.redis.host=127.0.0.1
spring.redis.port=6379
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
登入後複製

使用註解實作功能##快取@ Cacheable

根據方法對其返回結果進行緩存,下次請求時,如果緩存存在,則直接讀取緩存資料返回;如果緩存不存在,則執行方法,並將返回的結果存入緩存中。一般用在查詢方法 上。

快取@CachePut

每次執行帶有此註解標記的方法時,結果都會儲存在指定的快取中。可以直接從回應快取中讀取數據,不必再存取資料庫。一般用在新增方法上。

快取@CacheEvict

使用該註解標誌的方法,會清空指定的快取。一般用在更新或刪除方法上

在回傳serviceimpl中標註註解 沒設定key時會自動加上參數作為key

##@Cacheable(value = "dict", key = "'selectIndexList' #id")

以上是如何使用註解實現Redis快取功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Windows11安裝10.0.22000.100跳出0x80242008錯誤解決方法 Windows11安裝10.0.22000.100跳出0x80242008錯誤解決方法 May 08, 2024 pm 03:50 PM

Windows11安裝10.0.22000.100跳出0x80242008錯誤解決方法

剖析 PHP 函數瓶頸,提升執行效率 剖析 PHP 函數瓶頸,提升執行效率 Apr 23, 2024 pm 03:42 PM

剖析 PHP 函數瓶頸,提升執行效率

Golang API快取策略與最佳化 Golang API快取策略與最佳化 May 07, 2024 pm 02:12 PM

Golang API快取策略與最佳化

redis是記憶體快取嗎 redis是記憶體快取嗎 Apr 20, 2024 am 05:26 AM

redis是記憶體快取嗎

redis是非關係型資料庫嗎 redis是非關係型資料庫嗎 Apr 20, 2024 am 05:36 AM

redis是非關係型資料庫嗎

erlang和golang性能哪個好? erlang和golang性能哪個好? Apr 21, 2024 am 03:24 AM

erlang和golang性能哪個好?

PHP開發中的快取機制與應用實戰 PHP開發中的快取機制與應用實戰 May 09, 2024 pm 01:30 PM

PHP開發中的快取機制與應用實戰

PHP數組分頁中如何使用Redis快取? PHP數組分頁中如何使用Redis快取? May 01, 2024 am 10:48 AM

PHP數組分頁中如何使用Redis快取?

See all articles