Springboot如何详细解析整合GuavaCache缓存步骤?

2026-05-28 11:561阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1226个文字,预计阅读时间需要5分钟。

Springboot如何详细解析整合GuavaCache缓存步骤?

这篇文章主要介绍了Spring Boot整合Guava Cache的缓存过程,并通过示例代码展示了其使用方法。内容较为详细,对于有一定基础的读者或工作者具有一定的参考价值。需要的朋友可以参考以下内容:

Guava Cache是一种本地缓存,可以有效地存储和检索数据。在Spring Boot项目中整合Guava Cache,可以显著提高应用程序的性能。

以下是一个简单的示例,展示如何在Spring Boot项目中使用Guava Cache:

javaimport com.google.common.cache.CacheBuilder;import com.google.common.cache.CacheLoader;import com.google.common.cache.LoadingCache;

import java.util.concurrent.TimeUnit;

public class GuavaCacheExample {

private static final LoadingCache cache=CacheBuilder.newBuilder() .expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(100) .build(new CacheLoader() { @Override public String load(String key) throws Exception { // 模拟从数据库或其他数据源加载数据 return Loaded value for + key; } });

public static String getValue(String key) { try { return cache.get(key); } catch (Exception e) { e.printStackTrace(); return null; } }

public static void main(String[] args) { String value=getValue(exampleKey); System.out.println(value); }}

通过以上示例,我们可以看到Guava Cache的基本使用方法。在实际项目中,可以根据需求调整缓存策略,如过期时间、最大缓存大小等。

对于有学习或工作需求的朋友,Guava Cache是一个值得参考的缓存工具。希望这篇文章能对您有所帮助。

这篇文章主要介绍了springboot整合GuavaCache缓存过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Guava Cache是一种本地缓存机制,之所以叫本地缓存,是因为它不会把缓存数据放到外部文件或者其他服务器上,而是存放到了应用内存中。

Guava Cache的优点是:简单、强大、轻量级。

GuavaCache适用场景:

1.某些接口或者键值会被查询多次以上;

2.愿意使用或牺牲一些内存空间来提升访问或者计算速度;

3.缓存内容或者结果值较小,不会超过内存总容量;

GuavaCache中基于注解的声明式缓存操作

@Cacheable 触发缓存逻辑

Spring 在执行 @Cacheable 标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。

参数: value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件

@CacheEvict

触发缓存逐出逻辑

方法执行成功后会从缓存中移除相应数据。

参数: value缓存名、 key缓存键值、 condition满足缓存条件、 unless否决缓存条件、 allEntries是否移除所有数据 (设置为true时会移除所有缓存)

@CachePut

和 @Cacheable 类似,但会把方法的返回值放入缓存中, 主要用于数据新增和修改方法。

pom.xml配置文件:

Springboot如何详细解析整合GuavaCache缓存步骤?

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>1.5.9.RELEASE</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>

GuavaCacheConfig:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.config; import java.util.concurrent.TimeUnit; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.guava.GuavaCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.google.common.cache.CacheBuilder; @Configuration @EnableCaching public class GuavaCacheConfig { @Bean public CacheManager cacheManager() { GuavaCacheManager cacheManager = new GuavaCacheManager(); cacheManager.setCacheBuilder( CacheBuilder.newBuilder(). expireAfterWrite(10, TimeUnit.SECONDS). //存活时间10秒 maximumSize(1000)); //最大线程数 return cacheManager; } }

CacheController:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.guava; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/guava") public class CacheController { @Autowired private CacheService cacheService; /** * 查询方法 */ @RequestMapping(value = "/get", method = RequestMethod.GET) public String getByCache() { Long startTime = System.currentTimeMillis(); long timestamp = this.cacheService.getByCache(); Long endTime = System.currentTimeMillis(); System.out.println("耗时: " + (endTime - startTime)); return timestamp+""; } /** * 保存方法 */ @RequestMapping(value = "/save", method = RequestMethod.POST) public void save() { this.cacheService.save(); } /** * 删除方法 */ @RequestMapping(value = "delete", method = RequestMethod.DELETE) public void delete() { this.cacheService.delete(); } }

CacheService:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.guava; import java.sql.Timestamp; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class CacheService { @CachePut(value = "guavacache") public long save() { long timestamp = new Timestamp(System.currentTimeMillis()).getTime(); System.out.println("进行缓存:" + timestamp); return timestamp; } @CacheEvict(value = "guavacache") public void delete() { System.out.println("删除缓存"); } @Cacheable(value = "guavacache") public long getByCache() { try { Thread.sleep(3 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } return new Timestamp(System.currentTimeMillis()).getTime(); } }

Application:

package com.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { private static final Logger LOG = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.setWebEnvironment(true); app.run(args); LOG.info("**************** Startup Success ****************"); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计1226个文字,预计阅读时间需要5分钟。

Springboot如何详细解析整合GuavaCache缓存步骤?

这篇文章主要介绍了Spring Boot整合Guava Cache的缓存过程,并通过示例代码展示了其使用方法。内容较为详细,对于有一定基础的读者或工作者具有一定的参考价值。需要的朋友可以参考以下内容:

Guava Cache是一种本地缓存,可以有效地存储和检索数据。在Spring Boot项目中整合Guava Cache,可以显著提高应用程序的性能。

以下是一个简单的示例,展示如何在Spring Boot项目中使用Guava Cache:

javaimport com.google.common.cache.CacheBuilder;import com.google.common.cache.CacheLoader;import com.google.common.cache.LoadingCache;

import java.util.concurrent.TimeUnit;

public class GuavaCacheExample {

private static final LoadingCache cache=CacheBuilder.newBuilder() .expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(100) .build(new CacheLoader() { @Override public String load(String key) throws Exception { // 模拟从数据库或其他数据源加载数据 return Loaded value for + key; } });

public static String getValue(String key) { try { return cache.get(key); } catch (Exception e) { e.printStackTrace(); return null; } }

public static void main(String[] args) { String value=getValue(exampleKey); System.out.println(value); }}

通过以上示例,我们可以看到Guava Cache的基本使用方法。在实际项目中,可以根据需求调整缓存策略,如过期时间、最大缓存大小等。

对于有学习或工作需求的朋友,Guava Cache是一个值得参考的缓存工具。希望这篇文章能对您有所帮助。

这篇文章主要介绍了springboot整合GuavaCache缓存过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Guava Cache是一种本地缓存机制,之所以叫本地缓存,是因为它不会把缓存数据放到外部文件或者其他服务器上,而是存放到了应用内存中。

Guava Cache的优点是:简单、强大、轻量级。

GuavaCache适用场景:

1.某些接口或者键值会被查询多次以上;

2.愿意使用或牺牲一些内存空间来提升访问或者计算速度;

3.缓存内容或者结果值较小,不会超过内存总容量;

GuavaCache中基于注解的声明式缓存操作

@Cacheable 触发缓存逻辑

Spring 在执行 @Cacheable 标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。

参数: value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件

@CacheEvict

触发缓存逐出逻辑

方法执行成功后会从缓存中移除相应数据。

参数: value缓存名、 key缓存键值、 condition满足缓存条件、 unless否决缓存条件、 allEntries是否移除所有数据 (设置为true时会移除所有缓存)

@CachePut

和 @Cacheable 类似,但会把方法的返回值放入缓存中, 主要用于数据新增和修改方法。

pom.xml配置文件:

Springboot如何详细解析整合GuavaCache缓存步骤?

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>1.5.9.RELEASE</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>

GuavaCacheConfig:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.config; import java.util.concurrent.TimeUnit; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.guava.GuavaCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.google.common.cache.CacheBuilder; @Configuration @EnableCaching public class GuavaCacheConfig { @Bean public CacheManager cacheManager() { GuavaCacheManager cacheManager = new GuavaCacheManager(); cacheManager.setCacheBuilder( CacheBuilder.newBuilder(). expireAfterWrite(10, TimeUnit.SECONDS). //存活时间10秒 maximumSize(1000)); //最大线程数 return cacheManager; } }

CacheController:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.guava; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/guava") public class CacheController { @Autowired private CacheService cacheService; /** * 查询方法 */ @RequestMapping(value = "/get", method = RequestMethod.GET) public String getByCache() { Long startTime = System.currentTimeMillis(); long timestamp = this.cacheService.getByCache(); Long endTime = System.currentTimeMillis(); System.out.println("耗时: " + (endTime - startTime)); return timestamp+""; } /** * 保存方法 */ @RequestMapping(value = "/save", method = RequestMethod.POST) public void save() { this.cacheService.save(); } /** * 删除方法 */ @RequestMapping(value = "delete", method = RequestMethod.DELETE) public void delete() { this.cacheService.delete(); } }

CacheService:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.guava; import java.sql.Timestamp; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class CacheService { @CachePut(value = "guavacache") public long save() { long timestamp = new Timestamp(System.currentTimeMillis()).getTime(); System.out.println("进行缓存:" + timestamp); return timestamp; } @CacheEvict(value = "guavacache") public void delete() { System.out.println("删除缓存"); } @Cacheable(value = "guavacache") public long getByCache() { try { Thread.sleep(3 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } return new Timestamp(System.currentTimeMillis()).getTime(); } }

Application:

package com.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { private static final Logger LOG = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.setWebEnvironment(true); app.run(args); LOG.info("**************** Startup Success ****************"); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。