当前位置: 首页 > news >正文

网站建设趋势app开发平台

网站建设趋势,app开发平台,长沙招聘网站制作,专业做招商的公司Cacheable 注解是 Spring Cache 抽象的一部分,用于声明式地管理缓存。Cacheable 注解本身并不直接指定缓存的存储位置,而是依赖于配置的缓存管理器(CacheManager)来决定缓存数据的存储位置。 常见的缓存存储方式: 1、内存缓存&a…

@Cacheable 注解是 Spring Cache 抽象的一部分,用于声明式地管理缓存。@Cacheable 注解本身并不直接指定缓存的存储位置,而是依赖于配置的缓存管理器(CacheManager)来决定缓存数据的存储位置。

常见的缓存存储方式:

1、内存缓存:

Simple Cache Manager:默认情况下,Spring 使用 ConcurrentMapCacheManager,它将缓存数据存储在内存中的 ConcurrentHashMap 中。
Caffeine:Caffeine 是一个高性能的内存缓存库,常用于本地缓存。Spring Cache 可以集成 Caffeine 作为缓存管理器。

2、分布式缓存:

Redis:Redis 是一个高性能的键值存储系统,支持网络、持久化、多语言 API 等特性。Spring Cache 可以通过 RedisCacheManager 集成 Redis。
Ehcache:Ehcache 是一个广受欢迎的分布式缓存解决方案,支持本地缓存和分布式缓存。Spring Cache 可以通过 EhCacheCacheManager 集成 Ehcache。
Hazelcast:Hazelcast 是一个开源的内存数据网格,支持分布式缓存。Spring Cache 可以通过 HazelcastCacheManager 集成 Hazelcast。

下面举例在 Spring Boot 应用程序中结合 Redis 实现缓存。我们将使用 @Cacheable 注解来缓存方法的结果,并使用 RedisCacheManager 作为缓存管理器。

1. 添加依赖 首先,在 pom.xml 中添加必要的依赖:

<dependencies><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot Starter Data Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- Spring Boot Starter Cache --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- Lombok (可选) --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency>
</dependencies>

2. 配置 Redis

在 application.properties 文件中配置 Redis 连接信息:

spring.redis.host=localhost
spring.redis.port=6379

3. 配置缓存管理器

创建一个配置类来配置 RedisCacheManager:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;import java.time.Duration;@Configuration
public class CacheConfig {@Beanpublic RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1)) // 设置缓存过期时间为1小时.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).build();}
}

ps: 补充一下,使用ConcurrentMapCacheManager 默认的缓存方式:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;@Configuration
public class CacheConfig {//spring 默认的缓存配置@Beanpublic CacheManager cacheManager() {return new ConcurrentMapCacheManager("personal_Cache");}
}

4. 创建缓存接口和实现

创建一个简单的接口和实现类,用于演示缓存功能:

// 缓存接口
public interface CacheService {String getAttribute(String key);
}// 缓存实现类
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class CacheServiceImpl implements CacheService {@Override@Cacheable(value = "attributes", key = "#key")public String getAttribute(String key) {// 模拟耗时的数据库查询或其他操作try {Thread.sleep(2000); // 模拟延迟} catch (InterruptedException e) {e.printStackTrace();}return "Attribute value for " + key;}//清除缓存 @Override@CacheEvict(value = "attributes", key = "#key")public void evictAttribute(String key) {.....}
}

5. 创建控制器

创建一个控制器来测试缓存功能:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class CacheController {@Autowiredprivate CacheService cacheService;//模拟使用缓存@GetMapping("/getAttribute")public String getAttribute(@RequestParam String key) {return cacheService.getAttribute(key);}//模拟清除缓存@GetMapping("/evictAttribute")public String evictAttribute(@RequestParam String key) {return cacheService.evictAttribute(key);}
}

6. 启动类

创建一个启动类来启动 Spring Boot 应用程序:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class CacheApplication {public static void main(String[] args) {SpringApplication.run(CacheApplication.class, args);}
}

总结

通过上述步骤,我们成功地在 Spring Boot 应用程序中集成了 Redis 作为缓存存储,并使用 @Cacheable 注解实现了方法级别的缓存。这样可以显著提高应用程序的性能,特别是在处理频繁且耗时的数据查询时。

http://www.yidumall.com/news/20209.html

相关文章:

  • 盗图来做网站外贸网站平台哪个好
  • 定兴做网站网站seo李守洪排名大师
  • 程序员开源网站活动软文怎么写
  • 媒体平台推广网站用户体验优化
  • 淘宝上做网站 源代码怎么给你何鹏seo
  • 东莞网站建设 兼职免费seo工具
  • 政府网站开发报价建立网站的流程
  • 网站建设犭金手指a排名15百度竞价代理公司
  • 云南大学做行测的网站补习班
  • 餐饮管理系统哪个好冯宗耀seo教程
  • 个人做的网站有什么危险网络舆情监测系统
  • 网站建好了怎么做十大品牌营销策划公司
  • 网站制作图片插入代码免费个人网站平台
  • 织梦大气蓝色门户资讯网站模板金华百度seo
  • 个人网站还用备案吗百度怎么做广告
  • 小程序注册商标第几类百度seo文章
  • 做网站 php j2ee上海营销公司
  • 手机凡客网上海专业的seo推广咨询电话
  • 自己做壁纸的网站网络营销与直播电商是干什么的
  • 珠海建网站百度竞价排名商业模式
  • 手机app wap网站模板下载网络营销的四大特点
  • 做网站到底要不要备案谷歌关键词热度查询
  • 怎么样更好的做网站千锋教育北京校区
  • 深圳做企业网站的公司推荐创建网址快捷方式
  • 群晖 同步 wordpress站长工具seo综合查询引流
  • 广东省住房和建设局网站定制建站网站建设
  • 杨凌网站开发今日重大军事新闻
  • 免费给我推广湖北百度seo排名
  • 武汉网站建设供应商百度搜索引擎投放
  • 苏州网站建设设计公司网络广告策划