close

Caching is a common way to improve the performance of any project.

在專案上快取,經常可以加速專案的執行速度,例如資料處理、Server回應速度等等

KeyWord:

1.  Calling Library: 真的需要快取服務的Library or Code ,他們可以Implement Caching Service。 如果他們不需要Caching Service則不能去Implments

2.  Implementing Library:  專職於提供快取服務給 Any Calling Library,這個Library "必須" 提供 實作 Cache\CacheItemPoolInterface 和   Cache\CacheItemInterface interfaces 的Class。 必須支援最小TTL的功能。 TTL 如下

3.  TTL : 決定已經存放在Cache的暫存的物件 是否 屬於 "新鮮"  或是 "老舊"  的時間, 可以是"秒數" 也可以是DateInterval Object。

4.  Expiration - 真正決定 Item是老舊的時間。通常會利用TTL來計算Expiration,且形態MAY be set DateTime Object 假設有一個TTL 300秒的物件在1:30:00的時候被存放起來了,則Expiration為1:35:00會失效

而Caching Service可能會把item 在 expire time之前提早移除。但是 必須至少在expiration Time is Reached的時候,移除一次。避免產生幽靈的Item。造成memory leak。

假設calling Library設置expiration time or TTL 為null值,則Implementing Library MAY use 預設時間。如果Implementing連預設時間都沒有設置則,必須永久保存cache item,或者是as long as it supported

5.  Key : 必須至少為64長度且為UTF-8 encoding,內容為 A-Z,

 a-z0-9_, and .,  然而其實(MAY)可以支援更長的長度,和其他Char,但長度至少64字元起跳

而如果想要增加客製化的Char則必定不能包含  {}()/\@:  這個是保留給未來使用的,且客製化的Char必須有辦法自己做轉換,確保你可以還原回原始字串

6.Hit: 當Calling Library 藉由Key值,送出請求,且key值有對應的value,且該value尚未expire並且該value是合法的for some reason, Calling Libraries SHOULD make sure to verify isHit() on all get() calls.

7.Miss: A cache miss 剛好是hit的相反,當Calling library藉由key發送請求卻找不到value,或者是value被找到但是expire了, 又或者是value是不合法的for some reason, An expired value MUST always be considered a cache miss.

8. Deferred : 遞延??  在Cache系統中,為了避免瘋狂發送Request,造成資源浪費,所以通常會累積一定量才做一次發送,或定時處理。  A Pool object MAY delay persisting a deferred cache item in order to take advantage of bulk-set operations supported by some storage engines

A Pool 必須 確定deferred cache items最終會保存起來而不是丟失, 並且MAY persist them 在Calling Library發送請求前。 或者是當Calling Library invokes the commit() 方法時,強制推送儲存至Cache

Implementing Library MAY 使用任何邏輯去決定怎麼儲存deferred items,例如destructor,在被消滅之前一定要先存進去。 or 提供save() 方法執行,或者是timeout or max-items或其他邏輯。

存進去再取出來假設有一個請求要求deffered item則必須直接返為deferred,而不是

 

 

 

Data:

Implementing libraries MUST support all serializable PHP data types, including:

  • Strings - Character strings of arbitrary size in any PHP-compatible encoding.
  • Integers - All integers of any size supported by PHP, up to 64-bit signed.
  • Floats - All signed floating point values.
  • Boolean - True and False.
  • Null - The actual null value.
  • Arrays - Indexed, associative and multidimensional arrays of arbitrary depth.
  • Object - Any object that supports lossless serialization and deserialization such that $o == unserialize(serialize($o)). Objects MAY leverage PHP’s Serializable interface, __sleep() or __wakeup() magic methods, or similar language functionality if appropriate.

所有的資料傳到Implementing Library在取出來時,必須和傳進去時,是一致的,快取系統總不能傳String a 回傳時變成 String b吧, 你說對不對! 且這個包含variable type,例如string 5 和 int 5是不同的。 而物件也可以使用serialize 和 unserialize方法進行傳遞,但是很重要的一點。!!!! 如果原本傳進去的值已經Corrupted了,則回傳必須 response Cache miss,而不是回傳一個corrupted data

 

Key Concepts:

Pool: 池子

池子代表Items的集合,是一個邏輯的儲存庫,所有的items再返回時被當成是一個Item object。另外所有種類的cached object的互動都發生在pool裡面

Items:

一個Item就是代表 single key/value 在一個pool,這跟Redis一樣,雖然還沒深入研究redis @,,@ ,Key是獨一無二的且不可改變,而value MAY be changed in any times.

 

Error handling

Cache 主要的功能只是要提高專案的運行表現而已,所以Cache的錯誤不應該影響到專案的功能面,例如Cache的錯誤,專案就shutdown了!?

頂多應該變比較慢而已。Implementing Libraries MUST NOT throw exceptions 除了interface所定義的,另外 SHOULD 抓取any errors or exceptions是因為 儲存數據庫的錯誤

而不要讓他們浮起來(bubble) , 意思就是例如今天使用redis,而redis出現一些底層的錯誤時, Implementing Libraries 應該抓取這些錯誤,讓開發者知道Redis可能有一些問題,而不是

讓錯誤直接出現在client side或放任不管。

So   An Implementing Library SHOULD log such errors or otherwise report them to an administrator as appropriate.

假如  If a Calling Library requests that one or more Items be deleted, or that a pool be cleared, it MUST NOT be considered an error condition if the specified key does not exist. The post-condition is the same (the key does not exist, or the pool is empty), thus there is no error condition. ​​​​​​​

It's just a cache miss!!!  NOT An ERROR

 

Interfaces

CacheItemInterface

 

 Each Item object MUST be associated with a specific key, and  passed by the Cache\CacheItemPoolInterface object. ​​​​​​​

Cache\CacheItemInterface object ​​​​​​​封裝了 storage和retrieval of cache items 每個Item都是從Cache\CacheItemPoolInterface object. ​​​​​​​ 產生的

且CacheItemPoolInterface負責任何 any required setup as well as associating the object with a unique Key ​​​​​​​

Cache\CacheItemInterface objects MUST be able to store and retrieve any type of PHP value defined in the Data section of this document. ​​​​​​​

 

Calling Libraries MUST NOT 自己創造 Item,他們只限制能使用getItem()方法而已,並且Calling Libraries SHOULD NOT 不能假設Item被兩種不同的Implementing Library創造出來的Item,Item還可以是相同的,

 

參考至:

https://www.php-fig.org/psr/psr-6/

 

 

arrow
arrow
    全站熱搜

    蕭瑞文 發表在 痞客邦 留言(0) 人氣()