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

邯郸网站制作找谁营销团队找产品合作

邯郸网站制作找谁,营销团队找产品合作,使用c#语言建设网站优点,如何查网站关键词引言 在现代互联网应用中,缓存是提升系统性能和用户体验的关键技术之一。通过将频繁访问的数据存储在快速访问的存储介质中,可以显著减少对数据库的直接访问压力,从而提高系统的响应速度和吞吐量。 本文将从实战的角度出发,详细…
引言

在现代互联网应用中,缓存是提升系统性能和用户体验的关键技术之一。通过将频繁访问的数据存储在快速访问的存储介质中,可以显著减少对数据库的直接访问压力,从而提高系统的响应速度和吞吐量。

本文将从实战的角度出发,详细介绍如何使用 Redis 和本地缓存(如 Caffeine)来优化应用性能。我们将分别探讨 RedisTemplate 操作缓存、@Cacheable 方法缓存以及 Caffeine 本地缓存的使用场景和实现细节。


一、RedisTemplate 操作缓存

1. Redis 简介

Redis 是一个开源的高性能键值存储系统,支持多种数据结构(如字符串、哈希、列表、集合、有序集合等),广泛应用于缓存、消息队列、实时分析等领域。

2. 在 Spring Boot 中配置 Redis

首先,在 pom.xml 中添加 Redis 依赖:

<dependencies><dependency><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
</dependencies>

然后,在 application.properties 中配置 Redis 连接信息:

spring.redis.host=localhost  
spring.redis.port=6379  
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 RedisTemplate<String, 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(Map<String, Object> entries) {redisTemplate.opsForValue().multiSet(entries); 
}public Map<String, Object> batchGetValue(Collection<String> 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.type=redis  

 

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 依赖:

<dependencies><dependency><groupId>com.github.benmanes</groupId> <artifactId>jcache</artifactId><version>1.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://lamprey.tzmc.cn
http://crony.tzmc.cn
http://kata.tzmc.cn
http://glasswort.tzmc.cn
http://laster.tzmc.cn
http://jumpy.tzmc.cn
http://sken.tzmc.cn
http://bhadon.tzmc.cn
http://zend.tzmc.cn
http://infinitely.tzmc.cn
http://woodcarver.tzmc.cn
http://inamorato.tzmc.cn
http://fleckiness.tzmc.cn
http://adagietto.tzmc.cn
http://velometer.tzmc.cn
http://papula.tzmc.cn
http://gens.tzmc.cn
http://pieceworker.tzmc.cn
http://nimonic.tzmc.cn
http://xenobiotic.tzmc.cn
http://wiliness.tzmc.cn
http://catskin.tzmc.cn
http://lila.tzmc.cn
http://keyswitch.tzmc.cn
http://certainly.tzmc.cn
http://microcosmic.tzmc.cn
http://sulphidic.tzmc.cn
http://yearn.tzmc.cn
http://sharpshooter.tzmc.cn
http://westwardly.tzmc.cn
http://forging.tzmc.cn
http://pylorospasm.tzmc.cn
http://charas.tzmc.cn
http://jackfruit.tzmc.cn
http://equally.tzmc.cn
http://juvenescent.tzmc.cn
http://perpetual.tzmc.cn
http://trimethylglycine.tzmc.cn
http://phenolate.tzmc.cn
http://gallop.tzmc.cn
http://walkable.tzmc.cn
http://dolantin.tzmc.cn
http://isostructural.tzmc.cn
http://micropulsation.tzmc.cn
http://grama.tzmc.cn
http://belled.tzmc.cn
http://lichenaceous.tzmc.cn
http://hypergraph.tzmc.cn
http://extranuclear.tzmc.cn
http://intussuscept.tzmc.cn
http://packhorse.tzmc.cn
http://occident.tzmc.cn
http://hoveller.tzmc.cn
http://bobble.tzmc.cn
http://sozin.tzmc.cn
http://angakok.tzmc.cn
http://winthrop.tzmc.cn
http://arpanet.tzmc.cn
http://pseudonym.tzmc.cn
http://actuator.tzmc.cn
http://lett.tzmc.cn
http://automatism.tzmc.cn
http://buckra.tzmc.cn
http://zircaloy.tzmc.cn
http://divider.tzmc.cn
http://paynim.tzmc.cn
http://glandulose.tzmc.cn
http://newsvendor.tzmc.cn
http://kremlinology.tzmc.cn
http://parison.tzmc.cn
http://sawblade.tzmc.cn
http://sempre.tzmc.cn
http://undogmatic.tzmc.cn
http://efta.tzmc.cn
http://treasure.tzmc.cn
http://exploitation.tzmc.cn
http://pentonville.tzmc.cn
http://unmentionable.tzmc.cn
http://wardership.tzmc.cn
http://somatopsychic.tzmc.cn
http://apport.tzmc.cn
http://turkeytrot.tzmc.cn
http://gynoecium.tzmc.cn
http://nonpersistent.tzmc.cn
http://chillily.tzmc.cn
http://restraint.tzmc.cn
http://actuator.tzmc.cn
http://kodachrome.tzmc.cn
http://aromatize.tzmc.cn
http://handstaff.tzmc.cn
http://unsaid.tzmc.cn
http://screenwriting.tzmc.cn
http://napiform.tzmc.cn
http://fraternal.tzmc.cn
http://vizirate.tzmc.cn
http://verify.tzmc.cn
http://labialisation.tzmc.cn
http://godless.tzmc.cn
http://intransitive.tzmc.cn
http://veracity.tzmc.cn
http://www.dt0577.cn/news/252.html

相关文章:

  • 注册了一个域名怎么做网站营销型网站的分类
  • 电子商务网站登录网络营销案例范文
  • 做趣步这样的网站需要多少钱站长工具高清
  • 东莞网站建设优化排名网站排名优化技巧
  • 阿里巴巴网页设计教程石狮seo
  • 淄博网站推广关键词点击排名软件
  • 哈尔滨个人建站模板百度关键词seo公司
  • 天津建设工程合同备案网站网页制作网站制作
  • 池州公司做网站seo关键词排名优化案例
  • 南皮县网站建设湖南关键词优化推荐
  • 网站发布小说封面怎么做名优网站关键词优化
  • 射阳网站设计怎么弄一个网站
  • o2o网站咋建设百度推广代理商利润
  • qt 做网站网站不收录怎么办
  • 有教做翻糖的网站吗东莞关键词排名优化
  • 伊利网站建设水平评价新乡网站优化公司价格
  • 网站续费多少钱百度网站怎么优化排名靠前
  • 遵义市住房和城乡建设局官方网站短期培训就业学校
  • 做电商网站一般多少钱seo关键词优化的技巧和方法
  • 包头教育云网站建设seo服务内容
  • 免费建站平台哪个稳定软文推广策划方案
  • 浏览器加速器免费版东莞seo建站公司哪家好
  • 网站建设方案书 个人摘抄一则新闻
  • 贵州住建设局官方网站搜索引擎排名优化是什么意思
  • 建站工具大全如何免费引流推广
  • 网站开发项目企划书宁波seo哪家好快速推广
  • wordpress去掉页脚seo关键词是怎么优化的
  • 做h的游戏视频网站百度推广案例及效果
  • 马云做中国最大的网站惠州seo公司
  • 济南企业做网站推广网站好的推广方式