欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot与SpringCache概念用法大全

程序员文章站 2022-06-15 13:56:37
目录1.springcache的概念2.springcache用法(redis版)2.1 .springcache基本用法2.2 .springcache自定义缓存key2.3 .springcach...

1.springcache的概念

首先我们知道jpa,jdbc这些东西都是一些规范,比如jdbc,要要连接到数据库,都是需要用到数据库连接,预处理,结果集这三个对象,无论是连接到mysql还是oracle都是需要用到这个三个对象的,这是一种规范,而springcache是一种作为缓存的规范,具体实现有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.实体类

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") //这个注解作用就是将方法的返回值存到缓存中
    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.主程序,加上开启缓存的注解

package com.yl.cache_redis;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cache.annotation.enablecaching;
@springbootapplication
@enablecaching //开启缓存功能
public class cacheredisapplication {
    public static void main(string[] args) {
        springapplication.run(cacheredisapplication.class, args);
    }
}

6.测试

6.1)userservice没加@cacheable注解时

SpringBoot与SpringCache概念用法大全

6.2)userservice加@cacheable注解后,发现sevice中的方法只调用了一次

SpringBoot与SpringCache概念用法大全

6.3)在redis中也可以看到缓存中有数据,key为定义好的cachenames+::+方法的参数

SpringBoot与SpringCache概念用法大全

2.2 .springcache自定义缓存key

1.springcache默认使用cachenames和方法中的参数结合组成key的,那么如果有多个参数呢?它又是如何组成key的呢?我们可以指定key吗?

SpringBoot与SpringCache概念用法大全

SpringBoot与SpringCache概念用法大全

SpringBoot与SpringCache概念用法大全

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)测试

SpringBoot与SpringCache概念用法大全

SpringBoot与SpringCache概念用法大全

SpringBoot与SpringCache概念用法大全

2.3 .springcache更新缓存

1.使用@cacheput注解来更新,注意:@cacheput中的key要和@cacheable中的key一样,否则更新不了!

SpringBoot与SpringCache概念用法大全

SpringBoot与SpringCache概念用法大全

2.4 .springcache清空缓存

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

SpringBoot与SpringCache概念用法大全

2.测试

SpringBoot与SpringCache概念用法大全

2.5 .springcache其他用法

1.@caching注解,可以组合多个注解

SpringBoot与SpringCache概念用法大全

2.@cacheconfig注解

SpringBoot与SpringCache概念用法大全

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.实体类

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.测试

SpringBoot与SpringCache概念用法大全

到此这篇关于springboot与springcache的文章就介绍到这了,更多相关springboot与springcache内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!