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

SpringBoot與SpringCache概念用法大全

 更新時(shí)間:2022年02月11日 16:31:36   作者:#Hideonbush  
這篇文章主要介紹了SpringBoot與SpringCache的概念及基本用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.SpringCache的概念

首先我們知道jpa,jdbc這些東西都是一些規(guī)范,比如jdbc,要要連接到數(shù)據(jù)庫(kù),都是需要用到數(shù)據(jù)庫(kù)連接,預(yù)處理,結(jié)果集這三個(gè)對(duì)象,無(wú)論是連接到mysql還是oracle都是需要用到這個(gè)三個(gè)對(duì)象的,這是一種規(guī)范,而SpringCache是一種作為緩存的規(guī)范,具體實(shí)現(xiàn)有redis,EhCahe等

2.SpringCache用法(redis版)

2.1 .SpringCache基本用法

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>cache_redis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cache_redis</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <artifactId>spring-boot-starter-web</artifactId>

            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.application.properties

# redis的配置
spring.redis.host=192.168.244.135
spring.redis.port=6379
spring.redis.password=root123

3.實(shí)體類(lèi)

package com.yl.cache_redis.domain;
import java.io.Serializable;
public class User implements Serializable {
    private Integer id;
    private String username;
    private String password;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

4.service

package com.yl.cache_redis;

import com.yl.cache_redis.domain.User;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
    @Cacheable(cacheNames = "u1") //這個(gè)注解作用就是將方法的返回值存到緩存中
    public User getUserById(Integer id) {
        System.out.println("getUserById:" + id);
        User user = new User();
        user.setId(id);
        user.setUsername("root");
        user.setPassword("root");
        return user;
    }
}

5.主程序,加上開(kāi)啟緩存的注解

package com.yl.cache_redis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching //開(kāi)啟緩存功能
public class CacheRedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(CacheRedisApplication.class, args);
    }
}

6.測(cè)試

6.1)userservice沒(méi)加@Cacheable注解時(shí)

6.2)userservice加@Cacheable注解后,發(fā)現(xiàn)sevice中的方法只調(diào)用了一次

6.3)在redis中也可以看到緩存中有數(shù)據(jù),key為定義好的cacheNames+::+方法的參數(shù)

2.2 .SpringCache自定義緩存key

1.SpringCache默認(rèn)使用cacheNames和方法中的參數(shù)結(jié)合組成key的,那么如果有多個(gè)參數(shù)呢?它又是如何組成key的呢?我們可以指定key嗎?

2.如何自定義key呢?

1)自定義key

package com.yl.cache_redis;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Arrays;
@Component
public class MyKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object target, Method method, Object... params) {
        return target.toString() + ":" + method.getName() + ":" + Arrays.toString(params);
    }
}

2)測(cè)試

2.3 .SpringCache更新緩存

1.使用@CachePut注解來(lái)更新,注意:@CachePut中的key要和@Cacheable中的key一樣,否則更新不了!

2.4 .SpringCache清空緩存

1.使用@CacheEvict注解,主要key和要@Cacheable中的key一致

2.測(cè)試

2.5 .SpringCache其他用法

1.@Caching注解,可以組合多個(gè)注解

2.@CacheConfig注解

3.SpringCache用法(EhCache版)

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>ehcache</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ehcache</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
            <artifactId>spring-boot-starter-web</artifactId>

            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.6</version>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.實(shí)體類(lèi)

package com.yl.ehcache.model;
import java.io.Serializable;
public class User implements Serializable {
    private Integer id;
    private String username;
    private String password;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

3.service

package com.yl.ehcache.service;

import com.yl.ehcache.model.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
    @Cacheable(cacheNames = "user")
    public User getUserById(Integer id) {
        System.out.println("getUserById()...");
        User user = new User();
        user.setId(id);
        user.setUsername("root");
        user.setPassword("root");
        return user;
    }
    @CacheEvict(cacheNames = "user")
    public void delete(Integer id) {
        System.out.println("delete");
}

4.主程序

package com.yl.ehcache;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class EhcacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(EhcacheApplication.class, args);
    }
}

5.ehcache.xml

<ehcache>
    <diskStore path="java.io.tmpdir/shiro-spring-sample"/>
    <defaultCache
            maxElementsInMemory = "1000"
            eternal = "false"
            timeToIdleSeconds = "120"
            timeToLiveSeconds = "120"
            overflowToDisk = "false"
            diskPersistent = "false"
            diskExpiryThreadIntervalSeconds = "120"/>
    <cache name = "user"
           maxElementsInMemory = "1000"
           eternal = "false"
           overflowToDisk = "true"
           diskPersistent = "true"
           diskExpiryThreadIntervalSeconds = "600"/>
</ehcache>

6.測(cè)試

到此這篇關(guān)于SpringBoot與SpringCache的文章就介紹到這了,更多相關(guān)SpringBoot與SpringCache內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java ZooKeeper分布式鎖實(shí)現(xiàn)圖解

    Java ZooKeeper分布式鎖實(shí)現(xiàn)圖解

    ZooKeeper是一個(gè)分布式的,開(kāi)放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個(gè)開(kāi)源的實(shí)現(xiàn),是Hadoop和Hbase的重要組件。它是一個(gè)為分布式應(yīng)用提供一致性服務(wù)的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等
    2022-03-03
  • java文件下載設(shè)置中文名稱(chēng)的實(shí)例(response.addHeader)

    java文件下載設(shè)置中文名稱(chēng)的實(shí)例(response.addHeader)

    下面小編就為大家分享一篇java文件下載設(shè)置中文名稱(chēng)的實(shí)例(response.addHeader),具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2017-12-12
  • Scala數(shù)據(jù)庫(kù)連接池的簡(jiǎn)單實(shí)現(xiàn)

    Scala數(shù)據(jù)庫(kù)連接池的簡(jiǎn)單實(shí)現(xiàn)

    本文主要介紹了Scala數(shù)據(jù)庫(kù)連接池的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Java判斷多個(gè)時(shí)間段是否重合的方法小結(jié)

    Java判斷多個(gè)時(shí)間段是否重合的方法小結(jié)

    這篇文章主要為大家詳細(xì)介紹了Java中判斷多個(gè)時(shí)間段是否重合的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-02-02
  • 用Java輕松讀取Word文檔內(nèi)容的常用方法

    用Java輕松讀取Word文檔內(nèi)容的常用方法

    這篇文章主要介紹了用Java輕松讀取Word文檔內(nèi)容的常用方法,對(duì)于doc格式使用Apache?POI庫(kù)中的HWPFDocument和WordExtractor類(lèi),對(duì)于docx格式使用XWPFDocument類(lèi),并通過(guò)遍歷段落和文本運(yùn)行對(duì)象來(lái)提取文本內(nèi)容,需要的朋友可以參考下
    2025-03-03
  • Java spring定時(shí)任務(wù)詳解

    Java spring定時(shí)任務(wù)詳解

    這篇文章主要為大家詳細(xì)介紹了Spring定時(shí)任務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-10-10
  • Spring MVC 404 Not Found無(wú)錯(cuò)誤日志的解決方法

    Spring MVC 404 Not Found無(wú)錯(cuò)誤日志的解決方法

    這篇文章主要為大家詳細(xì)介紹了Spring MVC 404 Not Found無(wú)錯(cuò)誤日志的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • SpringBoot連接Nacos集群報(bào)400問(wèn)題及完美解決方法

    SpringBoot連接Nacos集群報(bào)400問(wèn)題及完美解決方法

    這篇文章主要介紹了解決SpringBoot連接Nacos集群報(bào)400問(wèn)題?,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Java文件批量重命名批量提取特定類(lèi)型文件

    Java文件批量重命名批量提取特定類(lèi)型文件

    這篇文章主要介紹了Java文件批量重命名批量提取特定類(lèi)型文件的相關(guān)資料
    2016-08-08
  • Springboot自動(dòng)加載配置的原理解析

    Springboot自動(dòng)加載配置的原理解析

    Springboot遵循“約定優(yōu)于配置”的原則,使用注解對(duì)一些常規(guī)的配置項(xiàng)做默認(rèn)配置,減少或不使用xml配置,讓你的項(xiàng)目快速運(yùn)行起來(lái),這篇文章主要給大家介紹了關(guān)于Springboot自動(dòng)加載配置原理的相關(guān)資料,需要的朋友可以參考下
    2021-10-10

最新評(píng)論

宜阳县| 祥云县| 宁陕县| 讷河市| 麟游县| 内丘县| 来凤县| 乾安县| 连城县| 扎兰屯市| 卢湾区| 天长市| 霞浦县| 茌平县| 双峰县| 泰兴市| 莒南县| 民权县| 凤凰县| 磐石市| 新竹市| 固安县| 塘沽区| 黄陵县| 五指山市| 全南县| 洛南县| 惠安县| 荃湾区| 延长县| 仙居县| 洛浦县| 加查县| 卫辉市| 姚安县| 罗江县| 河津市| 长阳| 西昌市| 绥滨县| 喜德县|