中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

window2003iis建好的網(wǎng)站營銷型網(wǎng)站建設(shè)論文

window2003iis建好的網(wǎng)站,營銷型網(wǎng)站建設(shè)論文,東營建設(shè)信息網(wǎng)(東營市住房和城鄉(xiāng)建設(shè)局),河南國正建設(shè)集團公司網(wǎng)站引言 在現(xiàn)代互聯(lián)網(wǎng)應(yīng)用中,緩存是提升系統(tǒng)性能和用戶體驗的關(guān)鍵技術(shù)之一。通過將頻繁訪問的數(shù)據(jù)存儲在快速訪問的存儲介質(zhì)中,可以顯著減少對數(shù)據(jù)庫的直接訪問壓力,從而提高系統(tǒng)的響應(yīng)速度和吞吐量。 本文將從實戰(zhàn)的角度出發(fā),詳細…
引言

在現(xiàn)代互聯(lián)網(wǎng)應(yīng)用中,緩存是提升系統(tǒng)性能和用戶體驗的關(guān)鍵技術(shù)之一。通過將頻繁訪問的數(shù)據(jù)存儲在快速訪問的存儲介質(zhì)中,可以顯著減少對數(shù)據(jù)庫的直接訪問壓力,從而提高系統(tǒng)的響應(yīng)速度和吞吐量。

本文將從實戰(zhàn)的角度出發(fā),詳細介紹如何使用 Redis 和本地緩存(如 Caffeine)來優(yōu)化應(yīng)用性能。我們將分別探討 RedisTemplate 操作緩存、@Cacheable 方法緩存以及 Caffeine 本地緩存的使用場景和實現(xiàn)細節(jié)。


一、RedisTemplate 操作緩存

1. Redis 簡介

Redis 是一個開源的高性能鍵值存儲系統(tǒng),支持多種數(shù)據(jù)結(jié)構(gòu)(如字符串、哈希、列表、集合、有序集合等),廣泛應(yīng)用于緩存、消息隊列、實時分析等領(lǐng)域。

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):設(shè)置鍵值對。
  • get(key):獲取指定鍵的值。
  • delete(key):刪除指定鍵。
4. 設(shè)置過期時間

為了避免緩存數(shù)據(jù)無限期占用內(nèi)存,我們可以為鍵設(shè)置過期時間。

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 提供的一套緩存抽象層,支持多種緩存實現(xiàn)(如 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) {// 從數(shù)據(jù)庫查詢用戶 return userRepository.findById(id).orElse(null); }
}

?

  • value = "users":指定緩存的名稱。
  • key = "#id":指定緩存的鍵,使用方法參數(shù) 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) {// 從數(shù)據(jù)庫查詢用戶 return userRepository.findById(id).orElse(null); }
}

?

三、Caffeine 本地緩存

1. Caffeine 簡介

Caffeine 是一個高性能的 Java 緩存庫,由 Google 開發(fā)并維護。它支持本地緩存,并提供了豐富的功能(如自動過期、容量限制、統(tǒng)計信息等)。

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 結(jié)合 Caffeine

與 Redis 類似,我們可以使用 @Cacheable 注解結(jié)合 Caffeine 實現(xiàn)本地緩存。

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) {// 從數(shù)據(jù)庫查詢用戶 return userRepository.findById(id).orElse(null); }
}

四、總結(jié)與選擇建議

1. 總結(jié)
  • Redis:適合分布式緩存場景,支持高并發(fā)和大數(shù)據(jù)量。
  • @Cacheable:簡化緩存邏輯的實現(xiàn),支持多種緩存后端。
  • Caffeine:適合本地緩存場景,性能優(yōu)異且配置簡單。
2. 選擇建議
  • 如果需要分布式緩存且數(shù)據(jù)量較大,推薦使用 Redis。
  • 如果需要本地緩存且追求高性能,推薦使用 Caffeine。
  • 如果希望統(tǒng)一管理緩存邏輯,可以結(jié)合 @Cacheable 和具體的緩存實現(xiàn)(如 Redis 或 Caffeine)。

http://www.risenshineclean.com/news/50976.html

相關(guān)文章:

  • WordPress主題沒有刪除站內(nèi)優(yōu)化seo
  • 做網(wǎng)站北京重慶seo技術(shù)教程博客
  • 長沙優(yōu)化網(wǎng)站技術(shù)廠家seo網(wǎng)站推廣有哪些
  • 網(wǎng)站建設(shè)和錢外貿(mào)網(wǎng)站免費推廣b2b
  • 網(wǎng)站如何能讓百度收錄網(wǎng)站數(shù)據(jù)
  • 做社區(qū)網(wǎng)站用什么程序?qū)幉╯eo排名外包
  • 網(wǎng)站建設(shè)資訊版塊如何做用戶運營百度網(wǎng)盤搜索神器
  • 低代碼開發(fā)平臺 開源文大俠seo博客
  • 網(wǎng)站制作網(wǎng)站建設(shè)項目規(guī)劃書軟文平臺有哪些
  • 網(wǎng)站的建站流程個人建站
  • 網(wǎng)站做淘寶客aso優(yōu)化服務(wù)站
  • 做母嬰產(chǎn)品的網(wǎng)站寧波網(wǎng)站建設(shè)網(wǎng)站排名優(yōu)化
  • 武漢商城網(wǎng)站制作西安疫情最新通知
  • 建筑外觀設(shè)計網(wǎng)站推薦淘寶標題優(yōu)化網(wǎng)站
  • 做昆特牌的網(wǎng)站重慶seo教程
  • 修改數(shù)據(jù)庫密碼 進不了網(wǎng)站后臺石家莊網(wǎng)站建設(shè)
  • 園嶺網(wǎng)站建設(shè)附近有沒有學(xué)電腦培訓(xùn)的
  • 做接口的網(wǎng)站廣告?zhèn)髅焦窘?jīng)營范圍
  • java 網(wǎng)站開發(fā) 順序刷死粉網(wǎng)站推廣
  • 湛江企業(yè)網(wǎng)站建站模板靠譜的拉新平臺
  • 網(wǎng)站后臺更新怎么做可口可樂軟文范例
  • 山東疫情最新消息今天上海優(yōu)化網(wǎng)站seo公司
  • 鄭州百度網(wǎng)站優(yōu)化排名ios微信上的pdf亂碼
  • 網(wǎng)站建設(shè)解說詞百度手機助手下載2022官方正版
  • 酒店vi設(shè)計抖音seo培訓(xùn)
  • 網(wǎng)站怎么做微信支付寶電商網(wǎng)站開發(fā)平臺
  • 免費網(wǎng)站開發(fā)公司百度競價可以自學(xué)嗎
  • 如何注冊個人網(wǎng)站cnn頭條新聞
  • 自助建站平臺便宜營銷型網(wǎng)站特點
  • 在線觀看視頻網(wǎng)站怎么做活動推廣宣傳方案