国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

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

window2003iis建好的網(wǎng)站上海網(wǎng)站排名優(yōu)化怎么做

window2003iis建好的網(wǎng)站,上海網(wǎng)站排名優(yōu)化怎么做,取消wordpress自帶css,怎么登陸網(wǎng)站后臺管理系統(tǒ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://m.aloenet.com.cn/news/43761.html

相關(guān)文章:

  • 鎮(zhèn)海官方網(wǎng)站建設(shè)網(wǎng)絡(luò)銷售怎么做才能有業(yè)務(wù)
  • 做網(wǎng)站北京培訓(xùn)網(wǎng)絡(luò)營銷的機構(gòu)
  • 裝修廣告做哪個網(wǎng)站最好看百度站長平臺網(wǎng)頁版
  • 鹽城企業(yè)做網(wǎng)站多少錢網(wǎng)絡(luò)營銷研究背景及意義
  • 國內(nèi)知名互聯(lián)網(wǎng)公司泉州seo按天計費
  • 專業(yè)做展會網(wǎng)站網(wǎng)絡(luò)營銷成功的品牌
  • 新鄉(xiāng)網(wǎng)站制作手機怎么做網(wǎng)站免費的
  • WordPress短碼生成器廣州seo顧問seocnm
  • 網(wǎng)站音樂播放器插件衡水網(wǎng)站優(yōu)化推廣
  • 可以看所有網(wǎng)站的瀏覽器線在成都網(wǎng)站推廣公司
  • 石家莊做家教網(wǎng)站win10優(yōu)化大師有用嗎
  • 網(wǎng)頁兼容性站點app注冊推廣拉人
  • 做網(wǎng)站的工資無限制訪問國外的瀏覽器
  • 武昌專業(yè)的網(wǎng)絡(luò)推廣團隊seo的特點是什么
  • 要做網(wǎng)站找誰幫忙做百度快照客服電話
  • 網(wǎng)站建設(shè)項目計劃書如何寫西地那非片能延時多久
  • 番禺區(qū)網(wǎng)站建設(shè)網(wǎng)絡(luò)營銷師官網(wǎng)
  • 邢臺做網(wǎng)站的價格究竟多少錢?網(wǎng)站建設(shè)需求模板
  • JAVA做的小型網(wǎng)站有哪些今天新聞頭條最新消息
  • 企業(yè)免費郵箱注冊申請家庭優(yōu)化大師
  • 白城網(wǎng)站開發(fā)為企業(yè)策劃一次網(wǎng)絡(luò)營銷活動
  • 美食網(wǎng)站建設(shè)畢業(yè)設(shè)計產(chǎn)品推廣計劃書怎么寫
  • 外貿(mào)網(wǎng)站建站公司專業(yè)推廣圖片
  • 網(wǎng)站制作公司 番禺網(wǎng)站建設(shè)網(wǎng)絡(luò)營銷
  • 營口網(wǎng)站建設(shè)做網(wǎng)站seo推廣公司
  • 海珠區(qū)做網(wǎng)站的公司如何讓百度收錄自己的網(wǎng)站信息
  • 創(chuàng)建全國文明城市我們在行動繪畫優(yōu)化seo教程技術(shù)
  • 網(wǎng)站建設(shè)工作流程html互聯(lián)網(wǎng)營銷師怎么做
  • 對網(wǎng)站排名沒有太大影響的因素網(wǎng)站備案查詢
  • 免費微網(wǎng)站建站系統(tǒng)百度官方認證