栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot与SpringCache

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot与SpringCache

SpringBoot与SpringCache

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



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.3
         
    
    com.yl
    cache_redis
    0.0.1-SNAPSHOT
    cache_redis
    Demo project for Spring Boot
    
        11
    
    
        
            org.springframework.boot
            spring-boot-starter-cache
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



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注解时

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

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;

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


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



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.3
         
    
    com.yl
    ehcache
    0.0.1-SNAPSHOT
    ehcache
    Demo project for Spring Boot
    
        11
    
    
        
            org.springframework.boot
            spring-boot-starter-cache
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            net.sf.ehcache
            ehcache
            2.10.6
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



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


    
    
    

6.测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号