1

nextjs使用memory-cache插件实现服务端缓存

import cacheData from 'memory-cache';

async function putCache(url, options) {  // url为缓存标识
    const value = cacheData.get(url); // 获取缓存
    if (value) {
     // 如果有缓存 
      *codeing*
    } else {
        const hours = 24;  
      // 请求接口 获取data
        cacheData.put(url, data, hours * 1000 * 60 * 60); // url 为缓存标识   data为数据  第三参数为以毫秒为单位的时间
        return data;
    }
}

程晓磊
12 声望1 粉丝

前端老垃圾