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

SpringCache緩存自定義配置的實(shí)現(xiàn)

 更新時(shí)間:2022年01月13日 15:22:42   作者:Java小生不才  
本文主要介紹了SpringCache緩存自定義配置的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Cacheable指定自定義屬性

在這里插入圖片描述

詳情請參考spring官網(wǎng)添加鏈接描述

1.key的名字和TTL時(shí)間

在這里插入圖片描述

/**
 * 查詢所有1級分類
 * @Cacheable代表當(dāng)前方法的結(jié)果需要緩存,若緩存中有則方法不會(huì)調(diào)用,若緩存中沒有會(huì)調(diào)用方法并將結(jié)果放入緩存
 * 緩存默認(rèn)行為:
 * a.若緩存中有則方法不會(huì)被調(diào)用
 * b.key默認(rèn)自動(dòng)生成,緩存的名字::SimpleKey []   (自動(dòng)生成的key值)
 * c.緩存的value值,默認(rèn)使用jdk序列化機(jī)制,將序列化后的數(shù)據(jù)存到redis
 * d.默認(rèn)ttl時(shí)間為-1
 * @return
 */
@Cacheable(value = {"category"},key ="'TopCategorys'" )
@Override
public List<CategoryEntity> getTopCategorys() {
    System.out.println(".....getTopCategorys..........");
    long startTime = System.currentTimeMillis();
    List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(
            new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
    System.out.println("消耗時(shí)間:" + (System.currentTimeMillis() - startTime));
    return categoryEntityList;
}

在這里插入圖片描述

 /**
     * 查詢所有1級分類
     * @Cacheable代表當(dāng)前方法的結(jié)果需要緩存,若緩存中有則方法不會(huì)調(diào)用,若緩存中沒有會(huì)調(diào)用方法并將結(jié)果放入緩存
     * 緩存默認(rèn)行為:
     * a.若緩存中有則方法不會(huì)被調(diào)用
     * b.key默認(rèn)自動(dòng)生成,緩存的名字::SimpleKey []   (自動(dòng)生成的key值)
     * c.緩存的value值,默認(rèn)使用jdk序列化機(jī)制,將序列化后的數(shù)據(jù)存到redis
     * d.默認(rèn)ttl時(shí)間為-1
     * @return
     */
   // @Cacheable(value = {"category"},key ="'TopCategorys'" )
    @Cacheable(value = {"category"},key ="#root.method.name" )
    @Override
    public List<CategoryEntity> getTopCategorys() {
        System.out.println(".....getTopCategorys..........");
        long startTime = System.currentTimeMillis();
        List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(
                new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
        System.out.println("消耗時(shí)間:" + (System.currentTimeMillis() - startTime));
        return categoryEntityList;
    }

在這里插入圖片描述

2.緩存數(shù)據(jù)保存為json格式

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

 * 原理:
 *   CacheAutoConfiguration(selectImports方法)--->CacheConfigurations(MAPPINGS)
 *   --->RedisCacheConfiguration-->cacheManager方法--->RedisCacheManager初始化所有的緩存(determineConfiguration方法
 *   每個(gè)緩存決定使用什么配置) --->createConfiguration方法

在config包下新建MyCacheConfig配置類

package com.atguigu.gulimall.product.config;

import org.springframework.boot.autoconfigure.cache.CacheProperties;
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 緩存配置
 * @author zfh
 * @email hst1406959716@163.com
 * @date 2021-12-25 09:40:46
 */
@EnableCaching
@Configuration
public class MyCacheConfig {

    @Bean
    RedisCacheConfiguration redisCacheConfiguration(){
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        // config = config.entryTtl();
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        return config;
    }
}

在這里插入圖片描述

發(fā)現(xiàn)ttl變成了-1,我們的application.properties沒起作用

package com.atguigu.gulimall.product.config;

import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 緩存配置
 * @author zfh
 * @email hst1406959716@163.com
 * @date 2021-12-25 09:40:46
 */
@EnableConfigurationProperties(CacheProperties.class)
@EnableCaching
@Configuration
public class MyCacheConfig {

    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        // config = config.entryTtl();
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        //將配置文件中所有的配置都生效
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }

        return config;
    }
}

在這里插入圖片描述

3.使用緩存前綴

在application.properties文件中

spring.cache.type=redis

#spring.cache.cache-names=qq
#TTL 毫秒為單位
spring.cache.redis.time-to-live=3600000

#如果指定了前綴就用我們指定的前綴,如果沒有就默認(rèn)使用緩存的名字作為前綴
spring.cache.redis.key-prefix=CACHE_
spring.cache.redis.use-key-prefix=true

在這里插入圖片描述

4.緩存null,防止緩存穿透

在application.properties文件中

spring.cache.type=redis

#spring.cache.cache-names=qq
#TTL 毫秒為單位
spring.cache.redis.time-to-live=3600000

#如果指定了前綴就用我們指定的前綴,如果沒有就默認(rèn)使用緩存的名字作為前綴
spring.cache.redis.key-prefix=CACHE_
spring.cache.redis.use-key-prefix=true

#是否緩存空值,防止緩存穿透
spring.cache.redis.cache-null-values=true

代碼中直接返回null

  /**
     * 查詢所有1級分類
     * @Cacheable代表當(dāng)前方法的結(jié)果需要緩存,若緩存中有則方法不會(huì)調(diào)用,若緩存中沒有會(huì)調(diào)用方法并將結(jié)果放入緩存
     * 緩存默認(rèn)行為:
     * a.若緩存中有則方法不會(huì)被調(diào)用
     * b.key默認(rèn)自動(dòng)生成,緩存的名字::SimpleKey []   (自動(dòng)生成的key值)
     * c.緩存的value值,默認(rèn)使用jdk序列化機(jī)制,將序列化后的數(shù)據(jù)存到redis
     * d.默認(rèn)ttl時(shí)間為-1
     *
     * 原理:
     *   CacheAutoConfiguration(selectImports方法)--->CacheConfigurations(MAPPINGS)
     *   --->RedisCacheConfiguration-->cacheManager方法--->RedisCacheManager初始化所有的緩存(determineConfiguration方法
     *   每個(gè)緩存決定使用什么配置) --->createConfiguration方法
     * @return
     */
   // @Cacheable(value = {"category"},key ="'TopCategorys'" )
    @Cacheable(value = {"category"},key ="#root.method.name" )
    @Override
    public List<CategoryEntity> getTopCategorys() {
        System.out.println(".....getTopCategorys..........");
        long startTime = System.currentTimeMillis();
        List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(
                new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
        System.out.println("消耗時(shí)間:" + (System.currentTimeMillis() - startTime));
//        return categoryEntityList;
        return null;
    }

在這里插入圖片描述

 到此這篇關(guān)于SpringCache緩存自定義配置的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringCache緩存自定義配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot連接redis并動(dòng)態(tài)切換database的實(shí)現(xiàn)方法

    springboot連接redis并動(dòng)態(tài)切換database的實(shí)現(xiàn)方法

    這篇文章主要介紹了springboot連接redis并動(dòng)態(tài)切換database,本文主為通過修改ConnectionFactory從而達(dá)到動(dòng)態(tài)切換database的效果,結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • 使用log4j2關(guān)閉debug日志

    使用log4j2關(guān)閉debug日志

    這篇文章主要介紹了使用log4j2關(guān)閉debug日志方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java  Swing基礎(chǔ)教程之圖形化實(shí)例代碼

    java Swing基礎(chǔ)教程之圖形化實(shí)例代碼

    這篇文章主要介紹了java Swing基礎(chǔ)教程之圖形化實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • SpringMVC中RequestParam注解的簡單理解

    SpringMVC中RequestParam注解的簡單理解

    @RequestMapping RequestMapping是一個(gè)用來處理請求地址映射的注解,可用于類或方法上,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中RequestParam注解的簡單理解,需要的朋友可以參考下
    2022-03-03
  • Netty分布式ByteBuf的分類方式源碼解析

    Netty分布式ByteBuf的分類方式源碼解析

    這篇文章主要為大家介紹了Netty分布式ByteBuf的分類方式源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • java實(shí)現(xiàn)圖片上加文字水印(SpringMVC + Jsp)

    java實(shí)現(xiàn)圖片上加文字水印(SpringMVC + Jsp)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)在圖片上加文字水印的方法,水印可以是圖片或者文字,操作方便,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Debian 7 和 Debian 8 用戶安裝 Java 8的方法

    Debian 7 和 Debian 8 用戶安裝 Java 8的方法

    Oracle Java 8 穩(wěn)定版本近期已發(fā)布,有很多新的特征變化。其中,有功能的程序支持通過“Lambda項(xiàng)目 ”,收到了一些安全更新和界面改進(jìn)上的bug修復(fù),使得開發(fā)人員的工作更容易。
    2014-03-03
  • java生成隨機(jī)數(shù)的常用方法分析

    java生成隨機(jī)數(shù)的常用方法分析

    這篇文章主要介紹了java生成隨機(jī)數(shù)的常用方法,結(jié)合實(shí)例形式分析了java生成隨機(jī)數(shù)常用的方法功能與相關(guān)使用技巧,需要的朋友可以參考下
    2017-10-10
  • java中的equals()和toString()方法實(shí)例詳解

    java中的equals()和toString()方法實(shí)例詳解

    這篇文章主要介紹了java中的equals()和toString()方法實(shí)例詳解的相關(guān)資料,這里舉例說明,并附實(shí)例代碼,和實(shí)現(xiàn)效果圖,需要的朋友可以參考下
    2016-11-11
  • Java實(shí)戰(zhàn)之校園外賣點(diǎn)餐系統(tǒng)的實(shí)現(xiàn)

    Java實(shí)戰(zhàn)之校園外賣點(diǎn)餐系統(tǒng)的實(shí)現(xiàn)

    這篇文章主要介紹了如何利用Java實(shí)現(xiàn)簡易的校園外賣點(diǎn)餐系統(tǒng),文中采用的技術(shù)有:JSP、Spring、SpringMVC、MyBatis 等,感興趣的可以了解一下
    2022-03-03

最新評論

壤塘县| 广水市| 云浮市| 新野县| 莱阳市| 顺昌县| 闽清县| 邹城市| 云阳县| 昌黎县| 商河县| 高密市| 广宁县| 孟村| 德江县| 永福县| 镇雄县| 长葛市| 纳雍县| 和顺县| 山东| 松潘县| 田林县| 罗田县| 罗山县| 郁南县| 新干县| 和平区| 南投市| 渭南市| 肃北| 文成县| 周宁县| 翁源县| 桐庐县| 临潭县| 金平| 通许县| 依兰县| 台湾省| 正宁县|