Authored by 陈峰

缓存策略

@@ -3,6 +3,7 @@ import App from './pages/app'; @@ -3,6 +3,7 @@ import App from './pages/app';
3 import yohoPluginCore from './plugins/yoho-plugin-core'; 3 import yohoPluginCore from './plugins/yoho-plugin-core';
4 import yohoPluginRouter from './plugins/yoho-plugin-router'; 4 import yohoPluginRouter from './plugins/yoho-plugin-router';
5 import yohoPluginAuth from './plugins/yoho-plugin-auth'; 5 import yohoPluginAuth from './plugins/yoho-plugin-auth';
  6 +import yohoPluginCache from './plugins/yoho-plugin-cache';
6 7
7 import './filters'; 8 import './filters';
8 import './directives'; 9 import './directives';
@@ -10,6 +11,7 @@ import './directives'; @@ -10,6 +11,7 @@ import './directives';
10 Vue.use(yohoPluginCore); 11 Vue.use(yohoPluginCore);
11 Vue.use(yohoPluginRouter); 12 Vue.use(yohoPluginRouter);
12 Vue.use(yohoPluginAuth); 13 Vue.use(yohoPluginAuth);
  14 +Vue.use(yohoPluginCache);
13 15
14 Vue.render({ 16 Vue.render({
15 el: '#app', 17 el: '#app',
@@ -19,6 +19,8 @@ @@ -19,6 +19,8 @@
19 </template> 19 </template>
20 20
21 <script> 21 <script>
  22 +import cache from 'cache';
  23 +
22 export default { 24 export default {
23 name: 'layout', 25 name: 'layout',
24 data() { 26 data() {
@@ -60,6 +62,7 @@ export default { @@ -60,6 +62,7 @@ export default {
60 }, 500); 62 }, 500);
61 }, 63 },
62 shopChange() { 64 shopChange() {
  65 + cache.clear();
63 this.reload = false; 66 this.reload = false;
64 this.$nextTick(() => { 67 this.$nextTick(() => {
65 this.reload = true; 68 this.reload = true;
@@ -6,7 +6,7 @@ import axios from 'axios'; @@ -6,7 +6,7 @@ import axios from 'axios';
6 import userService from 'user-service'; 6 import userService from 'user-service';
7 import Rsa from 'rsa'; 7 import Rsa from 'rsa';
8 8
9 -const plugin = { 9 +export default {
10 updateUser(Vue, user, purviews) { 10 updateUser(Vue, user, purviews) {
11 Vue.$store.set(Vue.$config.storeKeys.user, Rsa.encrypt(user)); 11 Vue.$store.set(Vue.$config.storeKeys.user, Rsa.encrypt(user));
12 Vue.prop('user', user); 12 Vue.prop('user', user);
@@ -107,5 +107,3 @@ const plugin = { @@ -107,5 +107,3 @@ const plugin = {
107 }; 107 };
108 } 108 }
109 }; 109 };
110 -  
111 -export default plugin;  
  1 +import axios from 'axios';
  2 +import settle from 'axios/lib/core/settle';
  3 +import cache from 'cache';
  4 +import md5 from 'yoho-md5';
  5 +
  6 +export default {
  7 + defaultAdapter: axios.defaults.adapter,
  8 + install() {
  9 + axios.defaults.adapter = config => {
  10 + if (config.cache) {
  11 + config.id = md5(`${config.url}|${JSON.stringify(config.params)}|${config.data}`);
  12 + let res = cache.get(config.id);
  13 +
  14 + if (res) {
  15 + return new Promise(function(resolve, reject) {
  16 + settle(resolve, reject, res);
  17 + });
  18 + }
  19 + }
  20 + return this.defaultAdapter(config).then(res => {
  21 + if (config.cache && res.status === 200 && res.data) {
  22 + cache.set(config.id, res);
  23 + }
  24 + return res;
  25 + });
  26 + };
  27 + }
  28 +};
@@ -17,7 +17,7 @@ import axios from 'axios'; @@ -17,7 +17,7 @@ import axios from 'axios';
17 import config from 'config'; 17 import config from 'config';
18 import _ from 'lodash'; 18 import _ from 'lodash';
19 19
20 -const plugin = { 20 +export default {
21 loadGlobalComponents(Vue) { 21 loadGlobalComponents(Vue) {
22 _.each(components, component => { 22 _.each(components, component => {
23 if (component.length) { 23 if (component.length) {
@@ -111,5 +111,3 @@ const plugin = { @@ -111,5 +111,3 @@ const plugin = {
111 }); 111 });
112 } 112 }
113 }; 113 };
114 -  
115 -export default plugin;  
@@ -4,7 +4,7 @@ import layout from '../pages/layout'; @@ -4,7 +4,7 @@ import layout from '../pages/layout';
4 import common from '../pages/common'; 4 import common from '../pages/common';
5 import _ from 'lodash'; 5 import _ from 'lodash';
6 6
7 -const plugin = { 7 +export default {
8 loadRouters(rous, paths, children) { 8 loadRouters(rous, paths, children) {
9 if (_.has(rous, 'path')) { 9 if (_.has(rous, 'path')) {
10 let ps = _.flattenDeep(paths).filter(p => p); 10 let ps = _.flattenDeep(paths).filter(p => p);
@@ -67,5 +67,3 @@ const plugin = { @@ -67,5 +67,3 @@ const plugin = {
67 }); 67 });
68 } 68 }
69 }; 69 };
70 -  
71 -export default plugin;  
@@ -22,7 +22,9 @@ const request = require('axios'); @@ -22,7 +22,9 @@ const request = require('axios');
22 * 获得店铺关联品牌 22 * 获得店铺关联品牌
23 */ 23 */
24 function getBrand() { 24 function getBrand() {
25 - return request.get(apiUrl.brand).then((result) => result.data); 25 + return request.get(apiUrl.brand, {
  26 + cache: true
  27 + }).then((result) => result.data);
26 } 28 }
27 29
28 /** 30 /**
@@ -50,7 +52,9 @@ function getSort(brandId, level, sortId) { @@ -50,7 +52,9 @@ function getSort(brandId, level, sortId) {
50 * 获得品牌所支持的所有颜色 52 * 获得品牌所支持的所有颜色
51 */ 53 */
52 function getColor() { 54 function getColor() {
53 - return request.get(apiUrl.color).then((result) => result.data); 55 + return request.get(apiUrl.color, {
  56 + cache: true
  57 + }).then((result) => result.data);
54 } 58 }
55 59
56 /** 60 /**
@@ -61,7 +65,8 @@ function getSize(smallSortId) { @@ -61,7 +65,8 @@ function getSize(smallSortId) {
61 return request.get(apiUrl.size, { 65 return request.get(apiUrl.size, {
62 params: { 66 params: {
63 sortId: smallSortId 67 sortId: smallSortId
64 - } 68 + },
  69 + cache: true
65 }).then(result => result.data); 70 }).then(result => result.data);
66 } 71 }
67 72
@@ -73,7 +78,8 @@ function getProductParams(sortId) { @@ -73,7 +78,8 @@ function getProductParams(sortId) {
73 return request.get(apiUrl.params, { 78 return request.get(apiUrl.params, {
74 params: { 79 params: {
75 sortId 80 sortId
76 - } 81 + },
  82 + cache: true
77 }).then(result => result.data); 83 }).then(result => result.data);
78 } 84 }
79 85
@@ -90,7 +96,8 @@ function getProductAttribute(sortId) { @@ -90,7 +96,8 @@ function getProductAttribute(sortId) {
90 categoryId: sortId, 96 categoryId: sortId,
91 saleType, 97 saleType,
92 displayPosition 98 displayPosition
93 - } 99 + },
  100 + cache: true
94 }).then(result => result.data); 101 }).then(result => result.data);
95 } 102 }
96 103
@@ -107,7 +114,8 @@ function getProductStyle(sortId) { @@ -107,7 +114,8 @@ function getProductStyle(sortId) {
107 categoryId: sortId, 114 categoryId: sortId,
108 saleType, 115 saleType,
109 displayPosition 116 displayPosition
110 - } 117 + },
  118 + cache: true
111 }).then(result => result.data); 119 }).then(result => result.data);
112 } 120 }
113 121
@@ -119,7 +127,8 @@ function getMaterial(maxSortId) { @@ -119,7 +127,8 @@ function getMaterial(maxSortId) {
119 return request.get(apiUrl.material, { 127 return request.get(apiUrl.material, {
120 params: { 128 params: {
121 maxSortId: maxSortId 129 maxSortId: maxSortId
122 - } 130 + },
  131 + cache: true
123 }).then(result => result.data); 132 }).then(result => result.data);
124 } 133 }
125 134
@@ -20,12 +20,15 @@ const apiUrl = { @@ -20,12 +20,15 @@ const apiUrl = {
20 20
21 const productService = { 21 const productService = {
22 getBrand() { 22 getBrand() {
23 - return axios.get(apiUrl.brand); 23 + return axios.get(apiUrl.brand, {
  24 + cache: true
  25 + });
24 }, 26 },
25 27
26 getAllSort(params) { 28 getAllSort(params) {
27 return axios.get(apiUrl.getSellerAllSortInfo, { 29 return axios.get(apiUrl.getSellerAllSortInfo, {
28 - params 30 + params,
  31 + cache: true
29 }) 32 })
30 .then(res => { 33 .then(res => {
31 if (res.status === 200) { 34 if (res.status === 200) {
  1 +class DefaultCache {
  2 + constructor() {
  3 + this.store = {};
  4 + }
  5 + get(key) {
  6 + return this.store[key];
  7 + }
  8 + remove(key) {
  9 + delete this.store[key];
  10 + }
  11 + set(key, value) {
  12 + this.store[key] = value;
  13 + }
  14 + clear() {
  15 + this.store = {};
  16 + }
  17 +}
  18 +
  19 +module.exports = DefaultCache;
  1 +let DbTypes = {
  2 + DEF: 'default'
  3 +};
  4 +
  5 +/**
  6 + * 缓存适配器
  7 + */
  8 +class CacheAdapter {
  9 + constructor(dbName) {
  10 + this.dbName = dbName;
  11 + if (dbName === DbTypes.DEF) {
  12 + let DbType = require('./defaultCache');
  13 +
  14 + this.db = new DbType();
  15 + }
  16 + }
  17 + get(key) {
  18 + if (this.dbName === DbTypes.DEF) {
  19 + return this.db.get(key);
  20 + }
  21 + }
  22 + remove(key) {
  23 + if (this.dbName === DbTypes.DEF) {
  24 + return this.db.remove(key);
  25 + }
  26 + }
  27 + set(key, value) {
  28 + if (this.dbName === DbTypes.DEF) {
  29 + return this.db.set(key, value);
  30 + }
  31 + }
  32 + clear() {
  33 + if (this.dbName === DbTypes.DEF) {
  34 + return this.db.clear();
  35 + }
  36 + }
  37 +}
  38 +
  39 +export default new CacheAdapter(DbTypes.DEF);