You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ro...@apache.org on 2020/05/22 22:13:05 UTC

[cloudstack-primate] 02/02: store: implement API caching for logged in user

This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack-primate.git

commit ab44b60fd386a7ad0b5c7bd693d624f940437b12
Author: Rohit Yadav <ro...@shapeblue.com>
AuthorDate: Sat May 23 03:39:03 2020 +0530

    store: implement API caching for logged in user
    
    Signed-off-by: Rohit Yadav <ro...@shapeblue.com>
---
 package-lock.json           |  2 +-
 src/permission.js           |  7 +++++--
 src/store/modules/user.js   | 40 ++++++++++++++++++++++++----------------
 src/store/mutation-types.js |  1 +
 4 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 5a5229e..4fa4a20 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "primate",
-  "version": "0.4.0",
+  "version": "0.5.0",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/src/permission.js b/src/permission.js
index 2bbc789..22dab80 100644
--- a/src/permission.js
+++ b/src/permission.js
@@ -25,7 +25,7 @@ import 'nprogress/nprogress.css' // progress bar style
 import message from 'ant-design-vue/es/message'
 import notification from 'ant-design-vue/es/notification'
 import { setDocumentTitle, domTitle } from '@/utils/domUtil'
-import { ACCESS_TOKEN } from '@/store/mutation-types'
+import { ACCESS_TOKEN, APIS } from '@/store/mutation-types'
 
 NProgress.configure({ showSpinner: false }) // NProgress Configuration
 
@@ -42,7 +42,10 @@ router.beforeEach((to, from, next) => {
       NProgress.done()
     } else {
       if (Object.keys(store.getters.apis).length === 0) {
-        message.loading('Discovering features...', 5)
+        const cachedApis = Vue.ls.get(APIS, {})
+        if (Object.keys(cachedApis).length === 0) {
+          message.loading('Loading...', 4)
+        }
         store
           .dispatch('GetInfo')
           .then(apis => {
diff --git a/src/store/modules/user.js b/src/store/modules/user.js
index 6e2aa00..ad2c253 100644
--- a/src/store/modules/user.js
+++ b/src/store/modules/user.js
@@ -19,7 +19,7 @@ import Cookies from 'js-cookie'
 import Vue from 'vue'
 import md5 from 'md5'
 import { login, logout, api } from '@/api'
-import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, ASYNC_JOB_IDS } from '@/store/mutation-types'
+import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, APIS, ASYNC_JOB_IDS } from '@/store/mutation-types'
 
 const user = {
   state: {
@@ -54,6 +54,7 @@ const user = {
     },
     SET_APIS: (state, apis) => {
       state.apis = apis
+      Vue.ls.set(APIS, apis)
     },
     SET_FEATURES: (state, features) => {
       state.features = features
@@ -114,22 +115,29 @@ const user = {
 
     GetInfo ({ commit }) {
       return new Promise((resolve, reject) => {
-        api('listApis').then(response => {
-          const apis = {}
-          const apiList = response.listapisresponse.api
-          for (var idx = 0; idx < apiList.length; idx++) {
-            const api = apiList[idx]
-            const apiName = api.name
-            apis[apiName] = {
-              params: api.params,
-              response: api.response
+        const cachedApis = Vue.ls.get(APIS, {})
+        if (Object.keys(cachedApis).length > 0) {
+          console.log('Login detected, using cached APIs')
+          commit('SET_APIS', cachedApis)
+          resolve(cachedApis)
+        } else {
+          api('listApis').then(response => {
+            const apis = {}
+            const apiList = response.listapisresponse.api
+            for (var idx = 0; idx < apiList.length; idx++) {
+              const api = apiList[idx]
+              const apiName = api.name
+              apis[apiName] = {
+                params: api.params,
+                response: api.response
+              }
             }
-          }
-          commit('SET_APIS', apis)
-          resolve(apis)
-        }).catch(error => {
-          reject(error)
-        })
+            commit('SET_APIS', apis)
+            resolve(apis)
+          }).catch(error => {
+            reject(error)
+          })
+        }
 
         api('listUsers').then(response => {
           const result = response.listusersresponse.user[0]
diff --git a/src/store/mutation-types.js b/src/store/mutation-types.js
index 6afc2d2..9a92570 100644
--- a/src/store/mutation-types.js
+++ b/src/store/mutation-types.js
@@ -27,6 +27,7 @@ export const DEFAULT_FIXED_SIDEMENU = 'DEFAULT_FIXED_SIDEMENU'
 export const DEFAULT_FIXED_HEADER_HIDDEN = 'DEFAULT_FIXED_HEADER_HIDDEN'
 export const DEFAULT_CONTENT_WIDTH_TYPE = 'DEFAULT_CONTENT_WIDTH_TYPE'
 export const DEFAULT_MULTI_TAB = 'DEFAULT_MULTI_TAB'
+export const APIS = 'APIS'
 export const ASYNC_JOB_IDS = 'ASYNC_JOB_IDS'
 
 export const CONTENT_WIDTH_TYPE = {