若川小于 1 分钟

缓存

let cache
let time = {}
if (options.cache) {
  const LRU = require('lru-cache')

  cache = new LRU({
    max: 500,
    length: function (n, key) { return n * 2 + key.length }
  })
}

tryCache

async function tryCache (key, checkUpdateTime = true) {
  const data = cache.get(key)

  if (checkUpdateTime) {
    const cacheUpdateTime = time[key]
    const fileUpdateTime = (await stat(path.resolve(root, key.replace(/^\//, '')))).mtime.getTime()
    if (cacheUpdateTime < fileUpdateTime) return null
  }

  return data
}

cacheData

function cacheData (key, data, updateTime) {
  const old = cache.peek(key)

  if (old != data) {
    cache.set(key, data)
    if (updateTime) time[key] = updateTime
    return true
  } else return false
}
欢迎扫码加我微信
拉你进源码共读群
一起学习源码