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

在东营怎么建网站介绍几个有趣的网站

在东营怎么建网站,介绍几个有趣的网站,万户网络是干什么的,做仪表行业推广有哪些网站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.yayakq.cn/news/780481/

相关文章:

  • 最便宜手机网站建设响应式网站的制作
  • 网站建设开发熊掌号博客网站需求分析
  • 重庆网站建设软件手机网站怎么提高关键词
  • 国家公示企业信息查询江西做网站优化好的
  • 网站推广在哪好贸易公司网站建设方案
  • 学做美食视频在哪个网站做网站属于什么职位
  • 零壹网站建设企业做电商网站有哪些
  • 英文建站多少钱茂名市城乡和住房建设局网站
  • 商城网站验收标准建立购物网站
  • 旅游景点网站策划书甘肃网站推广
  • 网站建设十年经验wordpress category id
  • 江西师范大学两学一做专题网站八宝山做网站的公司
  • 西安电子商务网站开发河南省城乡与住房建设厅网站
  • 做单页面网站文化宣传 加强网站建设 创办网站
  • 网站做优化效果怎样wordpress搭建企业网站思路
  • 网站建设合同细节小程序发布流程
  • 临淄做网站ui特效网站
  • 建网站需要买些什么外贸网站建设设计
  • 景德镇网站建设景德镇网站排名推广自己怎么做
  • 山西网站建设方案公司百度免费下载安装百度
  • .net cms网站管理系统培训心得简短50字
  • 北京电子商务网站制作Wordpress回复邮件通知
  • 电子商务网站建设实训实践总结广州网页设计软件培训
  • 网站建设制作免费咨询spa.net网站开发
  • 签约做网站模板企业服务公司排名
  • 仿4493美图网站程序浙江和海建设集团网站
  • 秦皇岛百度网站排名做一个企业网站需要多少钱
  • 网站开发工程师获奖图片编辑器免费版
  • 淘宝客网站的建设平台设计图片欣赏
  • 帮别人做高仿产品网站 违法么p2p网站建设公司哪家好