緩存解決方案-技術專題-Caffeine Cache
{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"內容簡介:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"strong"}],"text":"Google Guava Cache是一種非常優秀本地緩存解決方案,提供了基於容量,時間和引用的緩存回收方式。"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"基於容量的方式內部實現採用LRU算法,基於引用回收很好的利用了Java虛擬機的垃圾回收機制"},{"type":"text","text":"。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 其中的緩存構造器"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"CacheBuilder"},{"type":"text","text":"採用構建者模式提供了設置好各種參數的緩存對象,緩存核心類"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"LocalCache"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"裏面的內部類"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Segment"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"與"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"jdk1.7"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"及以前的"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ConcurrentHashMap"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"非常相似,都繼承於"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"ReetrantLock"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":",還有六個隊列,以實現豐富的本地緩存方案。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1.1 關於Caffeine Cache"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 通俗的講,Guava是google開源的一個公共java庫,類似於Apache Commons,它提供了集合,反射,緩存,科學計算,xml,io等一些 工具 類庫。cache只是其中的一個模塊。使用Guava cache能夠方便快速的構建本地緩存。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Caffeine是使用Java8對Guava緩存的重寫版本"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":" "},{"type":"text","text":",在Spring Boot 2.0中將取代Guava。如果出現Caffeine,"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"CaffeineCacheManager"},{"type":"text","text":"將會自動配置。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.1.1 爲什麼要用本地緩存"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相對於IO操作 => 速度快,效率高"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相對於Redis => Redis是一種優秀的分佈式緩存實現,受限於網卡等原因,遠水救不了近火 "},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"DB + Redis + LocalCache = 高效存儲,高效訪問訪問速度和花費的關係如下圖所示"},{"type":"text","text":":"}]}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/85/8559c739d5f7d95b48c583c13379356b.png","alt":"本地緩存解決方案-Caffeine Cache","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.1.2 什麼時候用"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"願意消耗一些內存空間來提升速度"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"預料到某些鍵會被多次查詢"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"緩存中存放的數據總量不會超出內存容量"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.1.3 怎麼用"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"設置緩存容量"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"設置超時時間"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"提供移除監聽器"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"提供緩存加載器"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"構建緩存"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1.2 使用Caffeine Cache"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 使用springboot2.x操作Caffeine Cache"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 搭建工程:Springboot2.x + MyBatis + MySQL + Caffeine Cache"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Caffeine是使用Java8對Guava緩存的重寫版本,在Spring 5.0或者Spring Boot 2.0中將取代,基於LRU算法實現,支持多種緩存過期策略。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.2.1 準備工作"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"準備好 "},{"type":"link","attrs":{"href":"http://www.codercto.com/category/database.html","title":null},"content":[{"type":"text","text":"數據庫"}]},{"type":"text","text":" 和數據表並插入相應實驗數據(MySQL)"}]}]}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"-- 新建表\ncreate database if not exists guavach charset utf8;\n-- 使用表\nuse guavach;\n-- 創建用戶表tbl_user\ncreate table tbl_user(\nid int(10) not null primary key auto_increment,\nname varchar(50) not null,\nage int(20) not null\n)engine=innodb default charset=utf8;\n-- 初始化數據\ninsert into tbl_user values('1','codesheep.cn','25');\ninsert into tbl_user values('2','hansongwang99','30');\ninsert into tbl_user values('3','劉能','35');\ninsert into tbl_user values('4','趙四','38');"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.2.2 java工程"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.2.2.1 添加依賴"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\n com.github.ben-manes.caffeine \n caffeine \n "}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.2.2.2 配置類"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"引入 CaffeineCache的配置文件 "},{"type":"codeinline","content":[{"type":"text","text":"CaffeineCacheConfig"}]}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@Configuration\n@EnableCaching\npublic class CaffeineCacheConfig {\n @Bean\n public CacheManager cacheManager(){\n CaffeineCacheManager cacheManager = new CaffeineCacheManager();\n //Caffeine配置\n Caffeine
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.