Authored by 陈峰

缓存策略

... ... @@ -3,6 +3,7 @@ import App from './pages/app';
import yohoPluginCore from './plugins/yoho-plugin-core';
import yohoPluginRouter from './plugins/yoho-plugin-router';
import yohoPluginAuth from './plugins/yoho-plugin-auth';
import yohoPluginCache from './plugins/yoho-plugin-cache';
import './filters';
import './directives';
... ... @@ -10,6 +11,7 @@ import './directives';
Vue.use(yohoPluginCore);
Vue.use(yohoPluginRouter);
Vue.use(yohoPluginAuth);
Vue.use(yohoPluginCache);
Vue.render({
el: '#app',
... ...
... ... @@ -19,6 +19,8 @@
</template>
<script>
import cache from 'cache';
export default {
name: 'layout',
data() {
... ... @@ -60,6 +62,7 @@ export default {
}, 500);
},
shopChange() {
cache.clear();
this.reload = false;
this.$nextTick(() => {
this.reload = true;
... ...
... ... @@ -6,7 +6,7 @@ import axios from 'axios';
import userService from 'user-service';
import Rsa from 'rsa';
const plugin = {
export default {
updateUser(Vue, user, purviews) {
Vue.$store.set(Vue.$config.storeKeys.user, Rsa.encrypt(user));
Vue.prop('user', user);
... ... @@ -107,5 +107,3 @@ const plugin = {
};
}
};
export default plugin;
... ...
import axios from 'axios';
import settle from 'axios/lib/core/settle';
import cache from 'cache';
import md5 from 'yoho-md5';
export default {
defaultAdapter: axios.defaults.adapter,
install() {
axios.defaults.adapter = config => {
if (config.cache) {
config.id = md5(`${config.url}|${JSON.stringify(config.params)}|${config.data}`);
let res = cache.get(config.id);
if (res) {
return new Promise(function(resolve, reject) {
settle(resolve, reject, res);
});
}
}
return this.defaultAdapter(config).then(res => {
if (config.cache && res.status === 200 && res.data) {
cache.set(config.id, res);
}
return res;
});
};
}
};
... ...
... ... @@ -17,7 +17,7 @@ import axios from 'axios';
import config from 'config';
import _ from 'lodash';
const plugin = {
export default {
loadGlobalComponents(Vue) {
_.each(components, component => {
if (component.length) {
... ... @@ -111,5 +111,3 @@ const plugin = {
});
}
};
export default plugin;
... ...
... ... @@ -4,7 +4,7 @@ import layout from '../pages/layout';
import common from '../pages/common';
import _ from 'lodash';
const plugin = {
export default {
loadRouters(rous, paths, children) {
if (_.has(rous, 'path')) {
let ps = _.flattenDeep(paths).filter(p => p);
... ... @@ -67,5 +67,3 @@ const plugin = {
});
}
};
export default plugin;
... ...
... ... @@ -22,7 +22,9 @@ const request = require('axios');
* 获得店铺关联品牌
*/
function getBrand() {
return request.get(apiUrl.brand).then((result) => result.data);
return request.get(apiUrl.brand, {
cache: true
}).then((result) => result.data);
}
/**
... ... @@ -50,7 +52,9 @@ function getSort(brandId, level, sortId) {
* 获得品牌所支持的所有颜色
*/
function getColor() {
return request.get(apiUrl.color).then((result) => result.data);
return request.get(apiUrl.color, {
cache: true
}).then((result) => result.data);
}
/**
... ... @@ -61,7 +65,8 @@ function getSize(smallSortId) {
return request.get(apiUrl.size, {
params: {
sortId: smallSortId
}
},
cache: true
}).then(result => result.data);
}
... ... @@ -73,7 +78,8 @@ function getProductParams(sortId) {
return request.get(apiUrl.params, {
params: {
sortId
}
},
cache: true
}).then(result => result.data);
}
... ... @@ -90,7 +96,8 @@ function getProductAttribute(sortId) {
categoryId: sortId,
saleType,
displayPosition
}
},
cache: true
}).then(result => result.data);
}
... ... @@ -107,7 +114,8 @@ function getProductStyle(sortId) {
categoryId: sortId,
saleType,
displayPosition
}
},
cache: true
}).then(result => result.data);
}
... ... @@ -119,7 +127,8 @@ function getMaterial(maxSortId) {
return request.get(apiUrl.material, {
params: {
maxSortId: maxSortId
}
},
cache: true
}).then(result => result.data);
}
... ...
... ... @@ -20,12 +20,15 @@ const apiUrl = {
const productService = {
getBrand() {
return axios.get(apiUrl.brand);
return axios.get(apiUrl.brand, {
cache: true
});
},
getAllSort(params) {
return axios.get(apiUrl.getSellerAllSortInfo, {
params
params,
cache: true
})
.then(res => {
if (res.status === 200) {
... ...
class DefaultCache {
constructor() {
this.store = {};
}
get(key) {
return this.store[key];
}
remove(key) {
delete this.store[key];
}
set(key, value) {
this.store[key] = value;
}
clear() {
this.store = {};
}
}
module.exports = DefaultCache;
... ...
let DbTypes = {
DEF: 'default'
};
/**
* 缓存适配器
*/
class CacheAdapter {
constructor(dbName) {
this.dbName = dbName;
if (dbName === DbTypes.DEF) {
let DbType = require('./defaultCache');
this.db = new DbType();
}
}
get(key) {
if (this.dbName === DbTypes.DEF) {
return this.db.get(key);
}
}
remove(key) {
if (this.dbName === DbTypes.DEF) {
return this.db.remove(key);
}
}
set(key, value) {
if (this.dbName === DbTypes.DEF) {
return this.db.set(key, value);
}
}
clear() {
if (this.dbName === DbTypes.DEF) {
return this.db.clear();
}
}
}
export default new CacheAdapter(DbTypes.DEF);
... ...