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

那个网站推作者vs2017 asp网站开发

那个网站推作者,vs2017 asp网站开发,地方门户网站运营方案,刚刚Cacheable 注解是 Spring Cache 抽象的一部分#xff0c;用于声明式地管理缓存。Cacheable 注解本身并不直接指定缓存的存储位置#xff0c;而是依赖于配置的缓存管理器#xff08;CacheManager#xff09;来决定缓存数据的存储位置。 常见的缓存存储方式: 1、内存缓存Cacheable 注解是 Spring Cache 抽象的一部分用于声明式地管理缓存。Cacheable 注解本身并不直接指定缓存的存储位置而是依赖于配置的缓存管理器CacheManager来决定缓存数据的存储位置。 常见的缓存存储方式: 1、内存缓存 Simple Cache Manager默认情况下Spring 使用 ConcurrentMapCacheManager它将缓存数据存储在内存中的 ConcurrentHashMap 中。 CaffeineCaffeine 是一个高性能的内存缓存库常用于本地缓存。Spring Cache 可以集成 Caffeine 作为缓存管理器。 2、分布式缓存 RedisRedis 是一个高性能的键值存储系统支持网络、持久化、多语言 API 等特性。Spring Cache 可以通过 RedisCacheManager 集成 Redis。 EhcacheEhcache 是一个广受欢迎的分布式缓存解决方案支持本地缓存和分布式缓存。Spring Cache 可以通过 EhCacheCacheManager 集成 Ehcache。 HazelcastHazelcast 是一个开源的内存数据网格支持分布式缓存。Spring Cache 可以通过 HazelcastCacheManager 集成 Hazelcast。 下面举例在 Spring Boot 应用程序中结合 Redis 实现缓存。我们将使用 Cacheable 注解来缓存方法的结果并使用 RedisCacheManager 作为缓存管理器。 1. 添加依赖 首先在 pom.xml 中添加必要的依赖 dependencies!-- Spring Boot Starter Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Spring Boot Starter Data Redis --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!-- Spring Boot Starter Cache --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependency!-- Lombok (可选) --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdscopeprovided/scope/dependency /dependencies2. 配置 Redis 在 application.properties 文件中配置 Redis 连接信息 spring.redis.hostlocalhost spring.redis.port63793. 配置缓存管理器 创建一个配置类来配置 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 {OverrideCacheable(value attributes, key #key)public String getAttribute(String key) {// 模拟耗时的数据库查询或其他操作try {Thread.sleep(2000); // 模拟延迟} catch (InterruptedException e) {e.printStackTrace();}return Attribute value for key;}//清除缓存 OverrideCacheEvict(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.laogonggong.com/news/118486.html

相关文章:

  • 做网站分几种北京怎样建设公司网站
  • 游戏网站的导航条怎么做的公司做网站推广要注意什么
  • 针对餐饮公司推广做网站方法山西大同企业做网站
  • 兰州市做网站的企业有哪些沈阳网站开发培训多少钱
  • 做国珍新时代 网站一个网站服务器一年大概多少钱
  • 免费微网站模板增加网站和接入备案吗
  • 永州网站开发公司win8式网站后台模板
  • 做教师知识网站有哪些内容华为手机开发者选项在哪里
  • 怎样建设卡盟网站好的空间网站
  • 胶南市场建设服务中心网站什么软件做电影短视频网站
  • 阿里云 做网站 百度开放云企业网站建设问卷
  • 做汽车网站费用茂名制作网站软件
  • 网站免费虚拟空间免费推广网站入口202
  • 《30天网站建设实录》西宁做网站君博优选
  • 单位网站建设注意事项密码管理系统admin
  • 建设银行网站修改预留手机号电子政务网站建设的步骤一般为
  • 查询网站服务商广告设计培训哪家好
  • 免费网站优化怎么做新上市手机
  • 农业网站怎么做深圳网络推广工资
  • 高端模板网站建设可以做旅行行程的网站
  • 泰州免费网站建站模板wordpress判断熊掌号收录
  • 网站权重排行网站技术策划
  • 网站运营计划书网站导航栏下拉菜单
  • 成都网站建设与推广wordpress开发导航菜单
  • 有哪些网站可以做外贸凡科送审平台
  • 公司无网站无平台怎么做外贸360收录提交
  • 广东做网站优化公司报价如何制作个人网页主题是周末愉快
  • 做饮食找工作哪个网站好比较好的网络推广平台
  • 唯样商城网站西安网站维护
  • 做财经比较好的网站有哪些网站取源用iapp做软件