Skip to content

Commit 8d0cc37

Browse files
committed
ui: add cache for oslogo request using osId
When OsLogo component is used in the items of a list having same OS type it was causing listOsTypes API call multiple time. This change allows caching request and response value for 30 seconds. Caching behaviour is controlled using `useCache` flag. Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 7d59bfe commit 8d0cc37

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

ui/src/components/widgets/OsLogo.vue

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<script>
3232
import { getAPI } from '@/api'
3333
34+
const CACHE_TTL_MS = 30_000
35+
const osTypeCache = new Map() // osId -> { ts, value?, promise? }
36+
3437
export default {
3538
name: 'OsLogo',
3639
props: {
@@ -45,55 +48,57 @@ export default {
4548
size: {
4649
type: String,
4750
default: 'lg'
51+
},
52+
useCache: {
53+
type: Boolean,
54+
default: false
4855
}
4956
},
5057
data () {
51-
return {
52-
name: '',
53-
osLogo: ['fas', 'image']
54-
}
55-
},
56-
computed: {
57-
logo: function () {
58-
if (!this.name) {
59-
this.fetchData()
60-
}
61-
return this.osLogo
62-
}
58+
return { name: '', osLogo: ['fas', 'image'] }
6359
},
60+
computed: { logo () { return this.osLogo } },
61+
mounted () { this.fetchData() },
6462
watch: {
65-
osId: function () {
66-
this.fetchData()
67-
}
63+
osId () { this.fetchData() },
64+
osName () { this.fetchData() }
6865
},
6966
methods: {
67+
async fetchOsTypeName (osId, useCache = this.useCache) {
68+
const now = Date.now()
69+
if (useCache) {
70+
const cached = osTypeCache.get(osId)
71+
if (cached?.value && (now - cached.ts) < CACHE_TTL_MS) return cached.value
72+
if (cached?.promise) return cached.promise
73+
const promise = getAPI('listOsTypes', { id: osId })
74+
.then(json => {
75+
const t = json?.listostypesresponse?.ostype
76+
const name = t?.length
77+
? (t[0].description || t[0].osdisplayname || 'Linux')
78+
: 'Linux'
79+
osTypeCache.set(osId, { ts: Date.now(), value: name })
80+
return name
81+
})
82+
.catch(e => { osTypeCache.delete(osId); throw e })
83+
osTypeCache.set(osId, { ts: now, promise })
84+
return promise
85+
}
86+
const json = await getAPI('listOsTypes', { id: osId })
87+
const t = json?.listostypesresponse?.ostype
88+
return t?.length ? (t[0].description || t[0].osdisplayname || 'Linux') : 'Linux'
89+
},
7090
fetchData () {
7191
if (this.osName) {
7292
this.discoverOsLogo(this.osName)
73-
} else if (this.osId) {
74-
this.findOsName(this.osId)
93+
} else if (this.osId && ('listOsTypes' in this.$store.getters.apis)) {
94+
this.fetchOsTypeName(this.osId)
95+
.then(this.discoverOsLogo)
96+
.catch(() => this.discoverOsLogo('Linux'))
7597
}
7698
},
77-
findOsName (osId) {
78-
if (!('listOsTypes' in this.$store.getters.apis)) {
79-
return
80-
}
81-
this.name = 'linux'
82-
getAPI('listOsTypes', { id: osId }).then(json => {
83-
if (json && json.listostypesresponse && json.listostypesresponse.ostype && json.listostypesresponse.ostype.length > 0) {
84-
this.discoverOsLogo(json.listostypesresponse.ostype[0].description)
85-
} else {
86-
this.discoverOsLogo('Linux')
87-
}
88-
})
89-
},
90-
getFontAwesomeIcon (name) {
91-
return ['fab', name]
92-
},
9399
discoverOsLogo (name) {
94100
this.name = name
95-
this.$emit('update-osname', this.name)
96-
const osname = name.toLowerCase()
101+
const osname = (name || '').toLowerCase()
97102
const logos = [
98103
{ name: 'centos' },
99104
{ name: 'debian' },
@@ -119,6 +124,3 @@ export default {
119124
}
120125
}
121126
</script>
122-
123-
<style scoped>
124-
</style>

ui/src/views/compute/wizard/OsBasedImageRadioGroup.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
class="radio-group__os-logo"
4343
size="2x"
4444
:osId="item.ostypeid"
45-
:os-name="item.osName" />
45+
:os-name="item.osName"
46+
:use-cache="true" />
4647
&nbsp;
4748
{{ item.displaytext }}
4849
<span v-if="item?.projectid">

0 commit comments

Comments
 (0)