169. LRU - Chrome storage自動消去

少し工夫が必要  - accepted / - tried

Chromeは必要に応じて、LRU algorithmを利用してデータを消去する。

こちらのYoutube 動画をご覧になってください、6:25 から 7:38までの間。

さー、近いロジックを持つLRUStorageを実装してください。

  1. Chromeでの実装を再現する目的ではない
  2. getDatasetDataは呼ばれるたびに、'used'とするべき。
  3. 時間の精度の問題で、classのconstructorにgetTimestamp を二番目引数として受けられるようにしてください。
interface OriginData {
  origin: string
  lastUsed: number
  size: number
  persistent: boolean
}

interface LRUStorage {
  capacity: number
  // to use the data for origin
  // return size of the data or undefined if not exist
  getData(origin: string): OriginData | undefined
  
  // updating data for origin
  // return boolean to indicate success or failure
  // If the total size exceeds capacity,
  // Least Recently Used non-persistent origin data other than itself should be evicted.

  setData(origin: string, size: number): boolean

  // manually clear data for origin
  clearData(origin: string): void

  // change data for origin to be persistent
  // it only handles existing data not the data added later
  // persistent data cannot be evicted unless manually clear it
  makePersistent(origin: string): void 
}

常にもっといい方法を求めよう。

(26)