首页 > 作文

SpringBoot与SpringCache概念用法大全

更新时间:2023-04-04 23:53:34 阅读: 评论:0

目录
1.springcache的概念2.springcache用法(redis版)2.1 .springcache基本用法2.2 .springcache自定义缓存key2.3 .springcache更新缓存2.4 .springcache清空缓存2.5 .springcache其他用法3.springcache用法(ehcache版)

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="/d/file/titlepic/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>    <gr211院校名单oupid>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.135spring.redis.port=6379spring.redis.password=root123

3.实体类

package com.yl.cache_redis.domain;import java.io.rializable;public class ur implements rializable {    private integer id;    private string urname;    private string password;    public integer getid() {        return id;    }    public void tid(integer id) {        this.id = id;    }    public string geturname() {        return urname;    }    public void turname(string urname) {        this.urname = urname;    }    public string getpassword() {        return password;    }    public void tpassword(string password) {        this.password = password;    }    @override    public string tostring() {        return "ur{" +                "id=" + id +                ", urname='" + urname + '\'' +                ", password='" + password + '\'' +                '}';    }}

4.rvice

package com.yl.cache_redis;import com.yl.cache_redis.domain.ur;import org.springframework.cache.annotation.cacheable;import org.springframework.stereotype.rvice;@rvicepublic class urrvice {    @cacheable(cachenames = "u1") //这个注解作用就是将方法的返回值存到缓存中    public ur geturbyid(integer id) {        system.out.println("geturbyid:" + id);        ur ur = new ur();        ur.tid(id);        ur.turname("root");        ur.tpassword("root");        return ur;    }}

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)urrvice没加@cacheable注解时

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

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

2.2 .springcache自定义缓存key

1.springcache默认使用cachenames和方法中的参数结合组成key的,那么如果有多个参数呢?它又是如何组成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;@书戴嵩画牛文言文翻译componentpublic class mykeygenerator implements keygenerator {    @override    public object generate(object target, method method, object... params) {        return target.tostring() + ":" + method.getname() + ":" + arrays.tostring(params);    }}

2)测试

2.3 .springcache更新缓存

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

2.4 .springcache清空缓存

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

2.测试

2.5 .springcache其他用法

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

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="/d/file/titlepic/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.rializable;public class ur implements rializable {    private integer id;    private string urname;    private string password;    public integer getid() {        return id;    }    public void tid(int打哈欠会传染吗eger id) {        this.id = id;    }    public string geturname() {        return urname;    }    public void turname(string urname) {        this.urname = urname;    }    public string getpassword() {        return password;    }    public void tpassword(string password) {        this.password = password;    }    @override    public string tostring() {        return "ur{" +                "id=" + id +                ", urname='" + urname + '\'' +                ", password='" + password + '\'' +                '}';    }}

3.rvice

package com.yl.ehcache.rvice;import com.yl.ehcache.model.ur;import org.springframework.cache.annotation.cacheevict;import org.springframework.cache.annotation.cacheable;import org.springframework.stereotype.rvice;@rvicepublic class urrvice {    @cacheable(cachenames = "ur")    public ur geturbyid(integer id) {        system.out.println("geturbyid()...");        ur ur = new ur();        ur.tid(id);        ur.turname("root");        ur.tpassword("root");        return ur;    }    @cacheevict(cachenames = "ur")    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@enablecachingpublic 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 = "fal"            timetoidleconds = "120"            timetoliveconds = "120"            overflowtodisk = "fal"            diskpersistent = "fal"            diskexpirythreadintervalconds = "120"/>    <cache name = "ur"           maxelementsinmemory = "1000"           eternal = "fal"           overflowtodisk = "true"           diskpersistent = "true"           diskexpirythreadintervalconds = "600"/></ehcache>

6.测试

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

本文发布于:2023-04-04 23:53:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/d2ee67aae6fb074bd876e0eac629fc87.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:SpringBoot与SpringCache概念用法大全.doc

本文 PDF 下载地址:SpringBoot与SpringCache概念用法大全.pdf

上一篇:高氯酸钠晶体
下一篇:返回列表
标签:缓存   注解   都是   自定义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图