SlideShare a Scribd company logo
1 of 43
如何建立有效的快取
Who am I ,[object Object]
[email_address]
http://www.plurk.com/rickysu
系統隨著時間資料越來越多
資料庫越跑越慢
到了某天再起不能
怎麼辦 ?
放乖乖
解決方法 ,[object Object]
改用MySQL Replication。 得先改寫程式讓 sql 讀寫分離, 而且不易維護。 只有解決分流的問題,慢的查詢還是一樣慢。
改用 NoSQL DB 取代 MySQL 。 只能說心臟很大顆,除非有強力後盾,否則別輕易嘗試。
難道沒有其他方法 ?
建立 Cache 吧
Cache必須滿足以下幾個條件 ,[object Object]
是可有可無的。 在 Cache 失效的情況下,系統還是能正常運作。
Cache 必須易於擴展。 當 Cache 儲存空間滿了,可以透過增加 Server 的方式平行擴展。
快取的儲存媒體 ,[object Object]
Redis http://redis.io/
TokyoTyrant http://fallabs.com/tokyotyrant/
讀取資料 是否存在快取中 傳回資料 由DB中取得資料 寫回快取
寫入資料 刪除快取 回寫DB
人生最厲害的就是這個 But
Key Value DB 在不知道 Key 的情況下如何刪除資料 !
情境模擬 ,[object Object]
刪除分類文章的快取。
刪除某天發表的文章快取。
天阿,這得先統計出一堆 Key ,才能刪除快取。
幫資料加上 Tag 吧
Ricky 的所有文章的第 2 頁分頁內容快取 key Article:ricky:All/Page:2/RowsPerPage:10 Value 快取內容 Tag Article:ricky:All ->  時間  (2011/10/03 13:10:43) 建立快取資料 Global Tag Article:ricky:All Key Tag:Article:ricky:All Value 2011/10/03 13:10:43 如果Global Tag 不存在時建立
刪除快取 ,[object Object]
讀取資料 Ricky 的所有文章的第 2 頁分頁內容快取 key Article:ricky:All/Page:2/RowsPerPage:10 Value 快取內容 Tag Article:ricky:All ->  時間  (2011/10/03 13:10:43) Global Tag Article:ricky:All Key Tag:Article:ricky:All Value Not Exist 比對Tag的更新時間
讀取快取 依序取出Tag 比對Global Tag 更新時間 快取失效 是否還有其他Tag 回傳快取資料
原理是如此 Zend_Cache 已經實做 Tag 不要再做輪子了
實際案例 討論區的快取建立
資料結構 ,[object Object]
Topic  討論主題
Message 文章內容
將查詢介面統一 別讓 SQL 四散各地
取得討論版的主題列表 ,[object Object]
Key: FindTopicsFromBoard/BoardID: $BoardID /Page: $Pag e/RowsPerPage: $RowsPerPage
Tag: Tag:BoardTopics/BoardID: $BoardID
Example Code static public function  FindTopicsFromBoard ( $BoardID , $Pages , $RowsPerPage ){ if( $Topics = $Cache -> get ( ''FindTopicsFromBoard/BoardID: $BoardID /Page: $Page /RowsPerPage: $RowsPerPage '' )){ reutrn  $Topics ; } return self:: rebuildFindTopicsFromBoard ( $BoardID , $Pages , $RowsPerPage ); } static public function  rebuildFindTopicsFromBoard ( $BoardID , $Pages , $RowsPerPage ){ // do db query $Cache->set( ''FindTopicsFromBoard/BoardID: $BoardID /Page: $Page /RowsPerPage: $RowsPerPage '' , $Topics ,  //Data array( ''Tag:BoardTopics/BoardID: $BoardID ''  //Tag ) ); return  $Topics ; }

More Related Content

Viewers also liked

4. list of figure
4. list of figure4. list of figure
4. list of figureIzzah Noah
 
Research Paper by Dr Everett Ehrlich
Research Paper by Dr Everett EhrlichResearch Paper by Dr Everett Ehrlich
Research Paper by Dr Everett EhrlichManagedFunds
 
好康報報
好康報報好康報報
好康報報Ricky Su
 
resume draft sheet
resume draft sheetresume draft sheet
resume draft sheetIzzah Noah
 
2014: The Year Ahead for Hedge Funds
2014: The Year Ahead for Hedge Funds2014: The Year Ahead for Hedge Funds
2014: The Year Ahead for Hedge FundsManagedFunds
 
Bedrijfspresentatie Trimension
Bedrijfspresentatie TrimensionBedrijfspresentatie Trimension
Bedrijfspresentatie Trimensionmhuibers
 
Symfony簡介
Symfony簡介Symfony簡介
Symfony簡介Ricky Su
 
How Passage of the JOBS Act Impacts Regulation D: Private Placement and Gene...
How Passage of the JOBS Act Impacts Regulation D:  Private Placement and Gene...How Passage of the JOBS Act Impacts Regulation D:  Private Placement and Gene...
How Passage of the JOBS Act Impacts Regulation D: Private Placement and Gene...ManagedFunds
 
LAB1:Adherence
LAB1:AdherenceLAB1:Adherence
LAB1:AdherenceIzzah Noah
 
Kate Stephens - Slides
Kate Stephens - SlidesKate Stephens - Slides
Kate Stephens - SlidesKate Stephens
 
FP Horak Wedding Presentation
FP Horak Wedding Presentation FP Horak Wedding Presentation
FP Horak Wedding Presentation dehavenproductions
 
The marketing environment
The marketing environmentThe marketing environment
The marketing environmentIzzah Noah
 
Hedge Funds: Trends and Insight From the Industry and Investors
Hedge Funds: Trends and Insight From the Industry and InvestorsHedge Funds: Trends and Insight From the Industry and Investors
Hedge Funds: Trends and Insight From the Industry and InvestorsManagedFunds
 
Security Enhancements in Windows Server 2012 Securing the Private - Cloud Inf...
Security Enhancements in Windows Server 2012Securing the Private - Cloud Inf...Security Enhancements in Windows Server 2012Securing the Private - Cloud Inf...
Security Enhancements in Windows Server 2012 Securing the Private - Cloud Inf...yuridiogenes
 
Who Invests in Hedge Funds in My State?
Who Invests in Hedge Funds in My State?Who Invests in Hedge Funds in My State?
Who Invests in Hedge Funds in My State?ManagedFunds
 
Officejet 100 mobile printer
Officejet 100 mobile printerOfficejet 100 mobile printer
Officejet 100 mobile printerR. T. Creager
 

Viewers also liked (20)

4. list of figure
4. list of figure4. list of figure
4. list of figure
 
T-Rex
T-RexT-Rex
T-Rex
 
Research Paper by Dr Everett Ehrlich
Research Paper by Dr Everett EhrlichResearch Paper by Dr Everett Ehrlich
Research Paper by Dr Everett Ehrlich
 
好康報報
好康報報好康報報
好康報報
 
resume draft sheet
resume draft sheetresume draft sheet
resume draft sheet
 
2014: The Year Ahead for Hedge Funds
2014: The Year Ahead for Hedge Funds2014: The Year Ahead for Hedge Funds
2014: The Year Ahead for Hedge Funds
 
My Passion
My PassionMy Passion
My Passion
 
Bedrijfspresentatie Trimension
Bedrijfspresentatie TrimensionBedrijfspresentatie Trimension
Bedrijfspresentatie Trimension
 
Symfony簡介
Symfony簡介Symfony簡介
Symfony簡介
 
How Passage of the JOBS Act Impacts Regulation D: Private Placement and Gene...
How Passage of the JOBS Act Impacts Regulation D:  Private Placement and Gene...How Passage of the JOBS Act Impacts Regulation D:  Private Placement and Gene...
How Passage of the JOBS Act Impacts Regulation D: Private Placement and Gene...
 
LAB1:Adherence
LAB1:AdherenceLAB1:Adherence
LAB1:Adherence
 
Kate Stephens - Slides
Kate Stephens - SlidesKate Stephens - Slides
Kate Stephens - Slides
 
FP Horak Wedding Presentation
FP Horak Wedding Presentation FP Horak Wedding Presentation
FP Horak Wedding Presentation
 
The marketing environment
The marketing environmentThe marketing environment
The marketing environment
 
Hedge Funds: Trends and Insight From the Industry and Investors
Hedge Funds: Trends and Insight From the Industry and InvestorsHedge Funds: Trends and Insight From the Industry and Investors
Hedge Funds: Trends and Insight From the Industry and Investors
 
Security Enhancements in Windows Server 2012 Securing the Private - Cloud Inf...
Security Enhancements in Windows Server 2012Securing the Private - Cloud Inf...Security Enhancements in Windows Server 2012Securing the Private - Cloud Inf...
Security Enhancements in Windows Server 2012 Securing the Private - Cloud Inf...
 
Who Invests in Hedge Funds in My State?
Who Invests in Hedge Funds in My State?Who Invests in Hedge Funds in My State?
Who Invests in Hedge Funds in My State?
 
Officejet 100 mobile printer
Officejet 100 mobile printerOfficejet 100 mobile printer
Officejet 100 mobile printer
 
The art of seo
The art of seoThe art of seo
The art of seo
 
The Game as Design Principle
The Game as Design PrincipleThe Game as Design Principle
The Game as Design Principle
 

Similar to 如何建立有效的快取

Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and DesignHo Kim
 
Discuz技术交流
Discuz技术交流Discuz技术交流
Discuz技术交流pigso
 
大型互联网应用架构设计
大型互联网应用架构设计大型互联网应用架构设计
大型互联网应用架构设计thinkinlamp
 
jQuery底层架构
jQuery底层架构jQuery底层架构
jQuery底层架构fangdeng
 
Web开发中的缓存
Web开发中的缓存Web开发中的缓存
Web开发中的缓存jeffz
 
面向未来的重构
面向未来的重构面向未来的重构
面向未来的重构Kejun Zhang
 
Csdn Emag(Oracle)第二期
Csdn Emag(Oracle)第二期Csdn Emag(Oracle)第二期
Csdn Emag(Oracle)第二期yiditushe
 
Sina App Quick Guide 1
Sina App Quick Guide 1Sina App Quick Guide 1
Sina App Quick Guide 1guestf4aed35
 
MySQL快速入门与提高
MySQL快速入门与提高MySQL快速入门与提高
MySQL快速入门与提高mysqlpub
 
无数据库日志文件恢复数据库方法两则
无数据库日志文件恢复数据库方法两则无数据库日志文件恢复数据库方法两则
无数据库日志文件恢复数据库方法两则wensheng wei
 
Php study.20130110
Php study.20130110Php study.20130110
Php study.20130110bngoogle
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發koji lin
 
PHP & MySQL 教學
PHP & MySQL 教學PHP & MySQL 教學
PHP & MySQL 教學Bo-Yi Wu
 
Node.js在淘宝的应用实践
Node.js在淘宝的应用实践Node.js在淘宝的应用实践
Node.js在淘宝的应用实践taobao.com
 
in in der 響應式編程
in in der 響應式編程in in der 響應式編程
in in der 響應式編程景隆 張
 
缓存技术浅谈
缓存技术浅谈缓存技术浅谈
缓存技术浅谈Robbin Fan
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniterChun-Kai Wang
 

Similar to 如何建立有效的快取 (20)

Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and Design
 
Discuz技术交流
Discuz技术交流Discuz技术交流
Discuz技术交流
 
CRUD 綜合運用
CRUD 綜合運用CRUD 綜合運用
CRUD 綜合運用
 
大型互联网应用架构设计
大型互联网应用架构设计大型互联网应用架构设计
大型互联网应用架构设计
 
jQuery底层架构
jQuery底层架构jQuery底层架构
jQuery底层架构
 
Web开发中的缓存
Web开发中的缓存Web开发中的缓存
Web开发中的缓存
 
面向未来的重构
面向未来的重构面向未来的重构
面向未来的重构
 
Csdn Emag(Oracle)第二期
Csdn Emag(Oracle)第二期Csdn Emag(Oracle)第二期
Csdn Emag(Oracle)第二期
 
Sina App Quick Guide 1
Sina App Quick Guide 1Sina App Quick Guide 1
Sina App Quick Guide 1
 
MySQL快速入门与提高
MySQL快速入门与提高MySQL快速入门与提高
MySQL快速入门与提高
 
无数据库日志文件恢复数据库方法两则
无数据库日志文件恢复数据库方法两则无数据库日志文件恢复数据库方法两则
无数据库日志文件恢复数据库方法两则
 
Php study.20130110
Php study.20130110Php study.20130110
Php study.20130110
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發
 
PHP & MySQL 教學
PHP & MySQL 教學PHP & MySQL 教學
PHP & MySQL 教學
 
nodejs开发web站点
nodejs开发web站点nodejs开发web站点
nodejs开发web站点
 
Node.js在淘宝的应用实践
Node.js在淘宝的应用实践Node.js在淘宝的应用实践
Node.js在淘宝的应用实践
 
in in der 響應式編程
in in der 響應式編程in in der 響應式編程
in in der 響應式編程
 
缓存技术浅谈
缓存技术浅谈缓存技术浅谈
缓存技术浅谈
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniter
 
PHP
PHPPHP
PHP
 

如何建立有效的快取