wordpress限制站點使用時間河南品牌網(wǎng)站建設(shè)
Caffeine
Caffeine是一種基于Java的高性能緩存庫,它提供了可配置、快速、靈活的緩存實現(xiàn)。Caffeine具有以下特點:
- 高性能:Caffeine使用了一些優(yōu)化技術(shù),如基于鏈表的并發(fā)哈希表和無鎖算法,以提供卓越的讀寫性能。
- 容量控制:Caffeine支持多種容量控制策略,如基于大小、基于權(quán)重和基于時間等,可以根據(jù)需求設(shè)置緩存的最大大小或最大權(quán)重,并在緩存達(dá)到限制時進(jìn)行逐出策略。
- 過期策略:Caffeine提供了各種內(nèi)置的過期策略,如基于訪問時間、基于寫入時間和定時過期等,在緩存中存儲的對象可以根據(jù)這些策略進(jìn)行自動過期。
- 異步加載:Caffeine支持異步加載數(shù)據(jù),當(dāng)緩存中不存在某個鍵對應(yīng)的值時,可以通過自定義的Loader接口來異步加載數(shù)據(jù)。
- 統(tǒng)計和監(jiān)聽:Caffeine提供了緩存統(tǒng)計功能,可以獲取緩存的命中率、緩存項數(shù)量等信息,還支持注冊緩存監(jiān)聽器,在緩存發(fā)生變化時觸發(fā)相應(yīng)的事件。
序列化
Caffeine緩存不涉及任何序列化,因此目標(biāo)緩存對象不需要實現(xiàn)Serializable
接口。若涉及多級緩存或者多種緩存共用,其它需要網(wǎng)絡(luò)傳輸或者持久化的緩存需要序列化,Caffeine盡管也使用實現(xiàn)序列化的實體類,但是不做序列化操作。
不需要序列化,降低了緩存使用難度。
引入依賴
<dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId>
</dependency>
全局配置
spring:cache:type: caffeine
緩存配置
配置緩存管理器:多CacheName配置。
public interface CacheNameTimeConstant {String CACHE_DEFAULT = "CACHE_DEFAULT";String CACHE_10SECS = "CACHE_10SECS";String CACHE_60SECS = "CACHE_60SECS";
}
同一個CacheManager配置多個CacheName,此處僅配置過期時間的差異,其余配置可自由增加。
import com.example.demo.util.CacheNameTimeConstant;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;@Configuration
public class CaffeineConfig{@Beanpublic CacheManager caffeineCacheManager() {SimpleCacheManager cacheManager = new SimpleCacheManager();List<CaffeineCache> caches = new ArrayList<>();caches.add(new CaffeineCache(CacheNameTimeConstant.CACHE_5SECS,Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).build()));caches.add(new CaffeineCache(CacheNameTimeConstant.CACHE_10SECS,Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).build()));caches.add(new CaffeineCache(CacheNameTimeConstant.CACHE_30SECS,Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.SECONDS).build()));cacheManager.setCaches(caches);return cacheManager;}
}
controller
@RestController
@RequestMapping("/test")
public class TestController {@Resourceprivate StuMapper stuMapper;/*** 添加緩存*/@GetMapping("/selectStu/{id}")@Cacheable(value = CacheNameTimeConstant.CACHE_30SECS,key="#id")public Student selectStu(@PathVariable Integer id){return stuMapper.selectById(id);}/*** 更新緩存*/@PostMapping("/updateStu")@CachePut(value = CacheNameTimeConstant.CACHE_30SECS,key = "#student.id")public Student updateStu(Student student){if (stuMapper.updateById(student) > 0) {return stuMapper.selectById(student.getId());}return null;}/*** 刪除緩存*/@PostMapping("/deleteStu/{id}")@CacheEvict(value = CacheNameTimeConstant.CACHE_30SECS,key = "#id")public String deleteStu(@PathVariable Integer id){return stuMapper.deleteById(id) > 0 ? "刪除成功" : "刪除失敗";}}
啟動類
添加@EnableCaching注解
@SpringBootApplication
@EnableTransactionManagement
@EnableCaching
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);System.out.println("啟動成功");}@Beanpublic MessageConverter jsonMessageConverter(){return new Jackson2JsonMessageConverter();}}
測試
第一次查詢,是走數(shù)據(jù)庫的
第二次查詢不走數(shù)據(jù)庫,直接返回緩存,但是30s后過期
?更新緩存
再次查詢數(shù)據(jù)時,從更新的緩存獲取