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

织梦网站移动化织梦手机网站建设

织梦网站移动化,织梦手机网站建设,商城网站现在可以做么,网站数据接口怎么做引言 在现代互联网应用中#xff0c;缓存是提升系统性能和用户体验的关键技术之一。通过将频繁访问的数据存储在快速访问的存储介质中#xff0c;可以显著减少对数据库的直接访问压力#xff0c;从而提高系统的响应速度和吞吐量。 本文将从实战的角度出发#xff0c;详细…引言 在现代互联网应用中缓存是提升系统性能和用户体验的关键技术之一。通过将频繁访问的数据存储在快速访问的存储介质中可以显著减少对数据库的直接访问压力从而提高系统的响应速度和吞吐量。 本文将从实战的角度出发详细介绍如何使用 Redis 和本地缓存如 Caffeine来优化应用性能。我们将分别探讨 RedisTemplate 操作缓存、Cacheable 方法缓存以及 Caffeine 本地缓存的使用场景和实现细节。 一、RedisTemplate 操作缓存 1. Redis 简介 Redis 是一个开源的高性能键值存储系统支持多种数据结构如字符串、哈希、列表、集合、有序集合等广泛应用于缓存、消息队列、实时分析等领域。 2. 在 Spring Boot 中配置 Redis 首先在 pom.xml 中添加 Redis 依赖 dependenciesdependencygroupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-redis/artifactId/dependency /dependencies 然后在 application.properties 中配置 Redis 连接信息 spring.redis.hostlocalhost spring.redis.port6379 spring.redis.password 3. 使用 RedisTemplate 进行基本操作 RedisTemplate 是 Spring Data Redis 提供的核心模板类用于简化 Redis 的操作。 示例代码 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; Service public class CacheService {Autowired private RedisTemplateString, Object redisTemplate;public void setValue(String key, Object value) {redisTemplate.opsForValue().set(key, value);}public Object getValue(String key) {return redisTemplate.opsForValue().get(key); }public void deleteKey(String key) {redisTemplate.delete(key); } } opsForValue()操作字符串类型的键值对。set(key, value)设置键值对。get(key)获取指定键的值。delete(key)删除指定键。 4. 设置过期时间 为了避免缓存数据无限期占用内存我们可以为键设置过期时间。 public void setValueWithExpire(String key, Object value, long expireSeconds) {redisTemplate.opsForValue().set(key, value, expireSeconds); } expireSeconds过期时间单位秒。 5. 批量操作 RedisTemplate 还支持批量操作以提高效率。 public void batchSetValue(MapString, Object entries) {redisTemplate.opsForValue().multiSet(entries); }public MapString, Object batchGetValue(CollectionString keys) {return redisTemplate.opsForValue().multiGet(keys); } 二、Cacheable 方法缓存 1. Spring Cache 简介 Spring Cache 是 Spring 提供的一套缓存抽象层支持多种缓存实现如 Redis、Caffeine、Ehcache 等。Cacheable 是 Spring Cache 中最常用的注解之一用于标注需要缓存的方法。 2. 配置 Spring Cache 在 application.properties 中启用缓存 spring.cache.typeredis 3. 使用 Cacheable 注解 示例代码 import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; Service public class UserService {Cacheable(value users, key #id)public User getUserById(Long id) {// 从数据库查询用户 return userRepository.findById(id).orElse(null); } } value users指定缓存的名称。key #id指定缓存的键使用方法参数 id 的值。 4. 自定义缓存配置 可以通过 CacheConfig 注解为类级别配置默认的缓存名称和键生成策略。 import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; Service CacheConfig(cacheNames users) public class UserService {Cacheable(key #id)public User getUserById(Long id) {// 从数据库查询用户 return userRepository.findById(id).orElse(null); } } 三、Caffeine 本地缓存 1. Caffeine 简介 Caffeine 是一个高性能的 Java 缓存库由 Google 开发并维护。它支持本地缓存并提供了丰富的功能如自动过期、容量限制、统计信息等。 2. 在 Spring Boot 中集成 Caffeine 首先在 pom.xml 中添加 Caffeine 依赖 dependenciesdependencygroupIdcom.github.benmanes/groupId artifactIdjcache/artifactIdversion1.0.0/version/dependency /dependencies 然后在配置类中配置 Caffeine 缓存管理器 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.cache.CacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager; import com.github.benmanes.caffeine.jcache.JCache; Configuration public class CacheConfig {Bean public CacheManager cacheManager() {CaffeineCacheManager caffeineCacheManager new CaffeineCacheManager(users);caffeineCacheManager.setCaffeine(JCache.Caffeine.newBuilder() .maximumSize(1000).expireAfterWrite(10, TimeUnit.MINUTES).build());return caffeineCacheManager;} } 3. 使用 Cacheable 结合 Caffeine 与 Redis 类似我们可以使用 Cacheable 注解结合 Caffeine 实现本地缓存。 import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; Service public class UserService {Cacheable(value users, key #id)public User getUserById(Long id) {// 从数据库查询用户 return userRepository.findById(id).orElse(null); } } 四、总结与选择建议 1. 总结 Redis适合分布式缓存场景支持高并发和大数据量。Cacheable简化缓存逻辑的实现支持多种缓存后端。Caffeine适合本地缓存场景性能优异且配置简单。 2. 选择建议 如果需要分布式缓存且数据量较大推荐使用 Redis。如果需要本地缓存且追求高性能推荐使用 Caffeine。如果希望统一管理缓存逻辑可以结合 Cacheable 和具体的缓存实现如 Redis 或 Caffeine。
http://www.dnsts.com.cn/news/109598.html

相关文章:

  • 长春电商网站建设哪家好房99西安房产网
  • 优质做网站公司贵阳市白云区官方网站
  • 深圳企业官方网站建设青海网站建设公司多少钱
  • 静态网站规范网站建设app开发合同范本
  • 衡水网站托管wordpress如何输入拼音
  • 做竞品分析的网站网站后台配置
  • 上海网站建设导航如何编写一个app
  • 机械行业网站建设制作开发方案马鞍山制作网站
  • 网站建设员是做什么的合肥 网站建设
  • 网站建设规划需要考虑做网站大概花多少钱
  • 中山外贸网站建设报价合肥市城乡建设网站
  • 网站开发用什么编辑语言好phpcms主题移植wordpress
  • 手机网站开发书籍嘉兴网站的优化
  • 网站界面设计描述建设工程业绩查询网站
  • 建设网站的企业公司做微景观的网站
  • 做自己的网站要多久门户网站开发工作室
  • 几种语言的网站如何做基于php旅游网站的毕业设计
  • 青海哪家做网站的公司最大西安在线网站制作
  • 电子商务网站建设的规划书网站开发 环境
  • 国外家居设计网站qq号码免费申请注册
  • 怎样做网站首页图片变换网站建设与维护服务器
  • 网站和服务器WordPress无法提交评论
  • muse怎么做响应式网站建筑网片焊网片机
  • 微软雅黑做网站建设网站视频教程
  • 贵港网站建设代理成都专业网站建设套餐
  • 平潭城乡住房建设厅网站西安网站建设制作价格低
  • 中国建设银行网站用户注册泰安专业网站开发公司
  • 北京做网站的公司排名青岛建设局网站
  • 做视频网站需要什么资质个人网站 名字
  • 网站开发所涉及的技术免费设计公司logo设计