169. LRU - Chrome storage自动清除算法

中等难度  -通过 / -执行

Chrome在需要的时候会利用LRU算法来清除数据

请观看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)