您当前的位置: 首页 >  缓存

梁云亮

暂无认证

  • 3浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot中Cache缓存的使用

梁云亮 发布时间:2020-07-22 12:52:43 ,浏览量:3

第一步:创建SpringBoot项目 Maven依赖

    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-cache
    2.3.0.RELEASE


    mysql
    mysql-connector-java
    runtime



    org.springframework.boot
    spring-boot-starter-jdbc


    org.mybatis.spring.boot
    mybatis-spring-boot-autoconfigure
    2.1.2


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.1.2


    org.mybatis
    mybatis
    3.5.4


    org.mybatis
    mybatis-spring
    2.0.4


    javax.cache
    cache-api
    1.1.1


    org.projectlombok
    lombok
    1.18.12


    com.alibaba
    druid
    1.1.22


    org.springframework.boot
    spring-boot-starter-test
    test
    
        
            org.junit.vintage
            junit-vintage-engine
        
    

Yml配置
server:
    port: 80
    servlet:
        context-path: /ed
mybatis:
    #Entity扫描的model包
    type-aliases-package: com.hc.domain
    #显示日志
    configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
spring:
    application:
        name: EhcacheDemo
    datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true
        username: root
        password: root
第二步:新建ehcache.xml 文件


    
    
    
    

配置信息说明:

  • name:缓存名称。
  • maxElementsInMemory:缓存最大个数
  • eternal:对象是否永久有效,一但设置timeout将不起作用
  • timeToIdleSeconds:对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
  • timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0,也就是对象存活时间无穷大
  • overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache是否将对象写到磁盘中
  • diskSpoolBufferSizeMB:设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
  • maxElementsOnDisk:硬盘最大缓存个数
  • diskPersistent:是否缓存虚拟机重启期数据,默认不缓存
  • diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
  • memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)
  • clearOnFlush:内存数量最大时是否清除
第三步:具体代码: 实体类
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
    private Integer id;
    private String username;
    private String password;
}
Mapper类
@Mapper
@CacheConfig(cacheNames = "userCache")
public interface UserMapper {
    @Cacheable
    @Select("select * from tb_user where id = #{id}")
    User selectByPrimaryKey(@Param("id") Integer id);
    @Select("select * from tb_user where username = #{username}")
    User selectByUsername(@Param("username") String username);
}
Controller类
@RestController
public class UserController {
    @Resource
    private UserMapper userMapper;
    @GetMapping("selectByPrimaryKey")
    public User selectByPrimaryKey(){
        User user = userMapper.selectByPrimaryKey(1001);
        return user;
    }
    @GetMapping("selectByUsername")
    public void selectByUsername(){
        userMapper.selectByUsername("lisi");
    }

	// 可以采用如下代码清除缓存:
	@Resource
	private CacheManager cacheManager;
		@RequestMapping("/rmCache")
		public void remoKey() {
    		cacheManager.getCache("userCache").clear();
		}
	}

关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0438s