spring boot使用guava缓存

1.pom中插入依赖:

<!--guava缓存cache-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.5-jre</version>
        </dependency>

2.在com.example.mapper.mybatisMap建立一个包cache,在cache下建立一个类LocalCache:


import com.google.common.cache.Cache;import com.google.common.cache.CacheBuilder;import java.util.concurrent.TimeUnit;
public class LocalCache {
    private static Cache<String, Integer> cache = CacheBuilder.newBuilder()
            .expireAfterWrite(10L, TimeUnit.MINUTES)  //写入10分钟后过期
            .maximumSize(50000L)
            .build();

    public Integer getCache(String key){
        return (Integer)cache.getIfPresent(key);
    }

    public Cache<String, Integer> getCache(){
        return cache;
    }

    public void setCache(String key, Integer obj){
        cache.put(key, obj);
    }

    public void removeCache(String key){
        cache.invalidate(key);
    }

    public void removeAll(){
        cache.invalidateAll();
    }

    public long getSize(){
        return cache.size();
    }

}

原文地址:https://www.cnblogs.com/heqiyoujing/p/9459981.html

时间: 2024-08-10 10:46:01

spring boot使用guava缓存的相关文章

Spring Boot 入门之缓存和 NoSQL 篇(四)

原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多数请求都是在重复的获取相同的数据,如果使用缓存,将结果数据放入其中可以很大程度上减轻数据库的负担,提升系统的响应速度. 本篇将介绍 Spring Boot 中缓存和 NoSQL 的使用.上篇文章<Spring Boot 入门之持久层篇(三)>. 二.整合缓存 Spring Boot 针对不同的缓存

spring boot 使用 redis 缓存

spring boot redis 使用 1 Redis:Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库.Redis 与其他 key - value 缓存产品有以下三个特点:Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用.Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储.Redis支持数据的备份,即master-slave模式的数据备份. spr

Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 package springboot01cache.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; im

Spring Boot 集成 Ehcache 缓存,三步搞定!

本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能.在Spring Boot应用程序中,我们可以通过Spring Caching来快速搞定数据缓存. 接下来我们将介绍如何在三步之内搞定 Spring Boot 缓存. 1. 创建一个Spring Boot工程 你所创建的Spring Boot应用程序的maven依赖文件至少应该是下面的样子: <?xml version="1.0" encoding="UTF-8"?

spring boot集成redis缓存

spring boot项目中使用redis作为缓存. 先创建spring boot的maven工程,在pom.xml中添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.3.RELEASE</version> </depen

spring boot 使用redis缓存

感谢大神分享! https://www.cnblogs.com/gdpuzxs/p/7222309.html (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> (2)修改项目启动类,增加注解@

Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门

文章目录 1. 声明式缓存 2. Spring Boot默认集成CacheManager 3. 默认的 ConcurrenMapCacheManager 4. 实战演练5. 扩展阅读 4.1. Maven 依赖 4.2. 开启缓存支持 4.3. 服务层 4.4. 控制层 4.5. 运行 4.6. 课后作业 6. 源代码 为了提高性能,减少数据库的压力,使用缓存是非常好的手段之一.本文,讲解 Spring Boot 如何集成缓存管理. 声明式缓存 Spring 定义 CacheManager 和

Spring Boot使用redis实现数据缓存

基于Spring Boot 1.5.2.RELEASE版本,一方面验证与Redis的集成方法,另外了解使用方法. 集成方法 配置依赖 修改pom.xml,增加如下内容. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 配置Redis

Spring Boot 入门之消息中间件篇(五)

原文地址:Spring Boot 入门之消息中间件篇(五) 博客地址:http://www.extlight.com 一.前言 在消息中间件中有 2 个重要的概念:消息代理和目的地.当消息发送者发送消息后,消息就被消息代理接管,消息代理保证消息传递到指定目的地. 我们常用的消息代理有 JMS 和 AMQP 规范.对应地,它们常见的实现分别是 ActiveMQ 和 RabbitMQ. 上篇文章<Spring Boot 入门之缓存和 NoSQL 篇(四)>. 二.整合 ActiveMQ 2.1 添