当我们在做vue的服务器端渲染时,可能会碰到各种各样的坑,内存泄露就是其中的一种。当然,导致内存泄露的原因有很多,不合理使用Axios也是其中一种,那下面我给大家介绍一下如何有效的避免请求中的内存泄露。
1. 安装缓存依赖: lru-cache
npm install lru-cache --dev
2. api 配置文件
config-server.js
var LRU = require('lru-cache')
let api
if (process.__API__) {
api = process.__API__
} else {
api = process.__API__ = {
api: 'http://localhost:8181/api/',
//配置缓存
cached: LRU({
max: 1000,
maxAge: 1000 * 60 * 15
}),
cachedItem: {}
}
}
module.exports = api
3. 封装下 api
import axios from 'axios'
import qs from 'qs'
import md5 from 'md5'
import config from './config-server.js'
export default {
post(url, data) {
const key = md5(url + JSON.s