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

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

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

Cacheable指定自定義屬性

在這里插入圖片描述

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

1.key的名字和TTL時間

在這里插入圖片描述

/**
 * 查詢所有1級分類
 * @Cacheable代表當前方法的結(jié)果需要緩存,若緩存中有則方法不會調(diào)用,若緩存中沒有會調(diào)用方法并將結(jié)果放入緩存
 * 緩存默認行為:
 * a.若緩存中有則方法不會被調(diào)用
 * b.key默認自動生成,緩存的名字::SimpleKey []   (自動生成的key值)
 * c.緩存的value值,默認使用jdk序列化機制,將序列化后的數(shù)據(jù)存到redis
 * d.默認ttl時間為-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("消耗時間:" + (System.currentTimeMillis() - startTime));
    return categoryEntityList;
}

在這里插入圖片描述

 /**
     * 查詢所有1級分類
     * @Cacheable代表當前方法的結(jié)果需要緩存,若緩存中有則方法不會調(diào)用,若緩存中沒有會調(diào)用方法并將結(jié)果放入緩存
     * 緩存默認行為:
     * a.若緩存中有則方法不會被調(diào)用
     * b.key默認自動生成,緩存的名字::SimpleKey []   (自動生成的key值)
     * c.緩存的value值,默認使用jdk序列化機制,將序列化后的數(shù)據(jù)存到redis
     * d.默認ttl時間為-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("消耗時間:" + (System.currentTimeMillis() - startTime));
        return categoryEntityList;
    }

在這里插入圖片描述

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

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

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

#如果指定了前綴就用我們指定的前綴,如果沒有就默認使用緩存的名字作為前綴
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

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

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

代碼中直接返回null

  /**
     * 查詢所有1級分類
     * @Cacheable代表當前方法的結(jié)果需要緩存,若緩存中有則方法不會調(diào)用,若緩存中沒有會調(diào)用方法并將結(jié)果放入緩存
     * 緩存默認行為:
     * a.若緩存中有則方法不會被調(diào)用
     * b.key默認自動生成,緩存的名字::SimpleKey []   (自動生成的key值)
     * c.緩存的value值,默認使用jdk序列化機制,將序列化后的數(shù)據(jù)存到redis
     * d.默認ttl時間為-1
     *
     * 原理:
     *   CacheAutoConfiguration(selectImports方法)--->CacheConfigurations(MAPPINGS)
     *   --->RedisCacheConfiguration-->cacheManager方法--->RedisCacheManager初始化所有的緩存(determineConfiguration方法
     *   每個緩存決定使用什么配置) --->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("消耗時間:" + (System.currentTimeMillis() - startTime));
//        return categoryEntityList;
        return null;
    }

在這里插入圖片描述

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

相關(guān)文章

  • springboot集成tkmapper及基本使用教程

    springboot集成tkmapper及基本使用教程

    tk.mybatis可以節(jié)省程序員的大部分時間,對于程序員來說關(guān)于一張表的操作無非就是增刪改查,tk.mybatis提供了一些基本操作的SQL語句,比如說按表的主鍵查詢、刪除等基本操作,我們接下來就來介紹一些springboot集成tkmapper及基本使用
    2022-11-11
  • JAVA初探設(shè)計模式的六大原則

    JAVA初探設(shè)計模式的六大原則

    這篇文章主要介紹了JAVA初探設(shè)計模式的六大原則,對設(shè)計模式感興趣的同學,可以參考下
    2021-05-05
  • nacos配置注冊中心時指定命名空間不起作用的問題

    nacos配置注冊中心時指定命名空間不起作用的問題

    這篇文章主要介紹了nacos配置注冊中心時指定命名空間不起作用的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • springboot集成camunda的實現(xiàn)示例

    springboot集成camunda的實現(xiàn)示例

    本文主要介紹了springboot集成camunda的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • MySQL查詢字段實現(xiàn)字符串分割split功能的示例代碼

    MySQL查詢字段實現(xiàn)字符串分割split功能的示例代碼

    本文主要介紹了MySQL查詢字段實現(xiàn)字符串分割split功能的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java 詳細講解線程安全與同步附實例與注釋

    Java 詳細講解線程安全與同步附實例與注釋

    線程安全是多線程編程時的計算機程序代碼中的一個概念。在擁有共享數(shù)據(jù)的多條線程并行執(zhí)行的程序中,線程安全的代碼會通過同步機制保證各個線程都可以正常且正確的執(zhí)行,不會出現(xiàn)數(shù)據(jù)污染等意外情況
    2022-04-04
  • 使用Enumeration和Iterator遍歷集合類詳解

    使用Enumeration和Iterator遍歷集合類詳解

    Enumeration和Iterator接口功能相似,而且Iterator的功能還比Enumeration多,那么為什么還要使用Enumeration
    2013-09-09
  • 使用@RequestParam 綁定List參數(shù)

    使用@RequestParam 綁定List參數(shù)

    這篇文章主要介紹了使用@RequestParam 綁定List參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot中@Autowired爆紅原理分析及解決

    SpringBoot中@Autowired爆紅原理分析及解決

    這篇文章主要介紹了SpringBoot中@Autowired爆紅原理分析及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • 解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法

    解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法

    這篇文章主要給大家介紹了關(guān)于解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法,文中給出了三種解決的方法,大家可以根據(jù)需要選擇對應的方法,需要的朋友們下面來一起看看吧。
    2017-02-02

最新評論