1.起步,在utils中新建cache.ts
import { ReqResponse } from '@/shims';
import { isBoolean } from 'lodash';
import { App } from 'vue';
export default class Cacher {
private static cacher?: Cacher;
private cacheObj: Map<string, any>;
private capacity: number = -1;
private capacityArr: string[];
static instance = this.getInstance();
private constructor() {
this.cacheObj = new Map();
this.capacityArr = []
}
private static getInstance() {
if (!this.cacher) {
this.cacher = new Cacher();
}
return this.cacher
}
setCapacity(num: number) {
console.log('设置缓存数量----:', num)
this.capacity = num
}
private set(api: string, data: any): Boolean {
this.cacheObj.set(api, data);
return true
}
private get(api: string): any {
if (this.cacheObj.has(api)) {
this.capacity < this.capacityArr.length && this.shift(api)
return this.cacheObj.get(api)
} else {
return false
}
}
private shift(api: string) {
const _index = this.capacityArr.indexOf(api)
const _del = this.capacityArr.splice(_index, 1)[0];
this.capacityArr.push(_del)
}
remove(api: string): Boolean {
return this.cacheObj.delete(api)
}
private handle(api: string) {
this.capacityArr.push(api);
if (this.capacity && this.capacityArr.length > this.capacity) {
const _d = this.capacityArr.shift();
_d && this.remove(_d)
}
}
getData(call: (res: any) => Promise<ReqResponse.Result<ReqResponse.Data>>) {
return (api: string, params: {}) => {
const _this = this;
return new Promise((resolve, reject) => {
let data = _this.get(api);
if (isBoolean(data)) {
console.log('请求后端---222---')
call(params).then(res => {
_this.set(api, res);
_this.handle(api)
resolve(res);
}).catch(err => {
reject(err)
});
} else {
console.log('在前端缓存中得到的数据---111---')
resolve(data);
}
});
};
}
}
export const Cache = {
install: function (app: App, capacity?: number) {
capacity && capacity > 1 && Cacher.instance.setCapacity(capacity)
}
}
2.在api中使用
import { ReqResponse } from '@/shims';
import axios from '@/utils/axios';
import Cacher from '@/utils/cache';
const login = (params: {}) => {
const url = `/miniAuth/oauth/wechat/token`
const begin = Cacher.instance.getData((data: {}) => {
return axios.request<ReqResponse.Data>({
url: url,
method: 'POST',
data,
});
});
return begin(url, params)
};
login({ openId: '123456', nickname: '', headimgurl: '' }).then((res: any) => {
console.log('我是返回的参数--', res.data);
});
3.如果需要设置缓存api的个数的话
import {Cache} from '@/utils/cache'
createApp(App).use(Cache,3).mount('#app');
shims.d.ts
import { AxiosRequestConfig } from 'axios';
import Cacher from '@/utils/cache'
namespace ReqResponse {
export interface Result<T = any> {
code: number;
msg: string;
data: T;
}
export interface Data {
error_code: string;
error_description: string;
}
}
declare module 'axios' {
export interface AxiosInstance {
request<T = any, R = ReqResponse.Result<T>>(config: AxiosRequestConfig): Promise<R>;
get<T = any, R = ReqResponse.Result<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
delete<T = any, R = ReqResponse.Result<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
head<T = any, R = ReqResponse.Result<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
options<T = any, R = ReqResponse.Result<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
post<T = any, R = ReqResponse.Result<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
put<T = any, R = ReqResponse.Result<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
patch<T = any, R = ReqResponse.Result<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
}
}
首次发文,请大家多多指教