做爲8年前端的我,在設計代碼以前,我比較喜歡先想好怎麼用,而後再去實現。想來想去,我以爲像先下面這樣使用會比較好一些:javascript
export function fetchList () {
return request.cache('緩存key').get('/myinterface')
}
複製代碼
fetchList()
.cache() // 若是某狀況須要強制調用接口來獲取,則去掉該方法便可。
.then(data => {})
.catch(e => {})
.finally(() => {})
複製代碼
爲何這麼使用?前端
我不能說代碼很簡單,雖然代碼量不大,但的確須要理一下。java
// 緩存請求結果
const buildCachePromise = (cacheKey, method, args, cacheImpl) => {
return {
__cacheImpl: cache[cacheImpl],
__arguments: args,
__result_promise: null,
// 開啓緩存
cache () {
const data = this.__cacheImpl.getJSON(cacheKey)
if (data != null) {
this.__result_promise = Promise.resolve(data)
}
if (this.__result_promise != null) {
return this.__result_promise
}
return this
},
then () {
return this.__access('then', arguments)
},
catch () {
return this.__access('catch', arguments)
},
finally () {
return this.__access('finally', arguments)
},
__access (methodName, args) {
if (this.__result_promise != null) {
return this.__result_promise
}
this.__result_promise = axiosInstance[method].apply(axiosInstance, this.__arguments)
this.__result_promise.then(data => {
this.__cacheImpl.setJSON(cacheKey, data)
return data
})
return this.__result_promise[methodName].apply(this.__result_promise, args)
}
}
}
const methods = ['get', 'post', 'delete', 'put', 'head', 'options', 'patch', 'request']
axiosInstance.cache = function (cacheKey, isLocal = false) {
if (cacheKey == null) {
throw Error('Request cache key can not be null.')
}
const cacheAxiosInstance = {}
for (const method of methods) {
cacheAxiosInstance[method] = function () {
return buildCachePromise(cacheKey, method, arguments, isLocal ? 'local' : 'session')
}
}
return cacheAxiosInstance
}
複製代碼
代碼雖說是我本身寫的,可是,我不得不說我本身再看一遍,我仍是得理一理。不過不重要,好用就行!這套代碼是從個人開源框架Eva中提取出來的。後面還會跟你們介紹更多代碼設計方面的內容。ios
Eva開源框架的主要目的呢,是可以進行快速開發項目,而且還能養成一套良好的開發規範,對於新手而言呢,也要作到是一套好的學習項目。git
歡迎star!github
關注我!和你一塊兒探討代碼設計的藝術。axios