Cache artists/albums for performance

This commit is contained in:
An Phan 2016-12-17 15:31:08 +08:00
parent 1ba00a0475
commit ddabe880e5
No known key found for this signature in database
GPG key ID: 05536BB4BCDC02A2
3 changed files with 16 additions and 5 deletions

View file

@ -7,6 +7,7 @@ import { songStore, artistStore } from '.'
export const albumStore = {
stub,
cache: [],
state: {
albums: [stub]
@ -61,7 +62,11 @@ export const albumStore = {
},
byId (id) {
return find(this.all, { id })
if (!this.cache[id]) {
this.cache[id] = find(this.all, { id })
}
return this.cache[id]
},
/**

View file

@ -10,6 +10,7 @@ const VARIOUS_ARTISTS_ID = 2
export const artistStore = {
stub,
cache: [],
state: {
artists: []
@ -84,7 +85,11 @@ export const artistStore = {
* @param {Number} id
*/
byId (id) {
return find(this.all, { id })
if (!this.cache[id]) {
this.cache[id] = find(this.all, { id })
}
return this.cache[id]
},
/**

View file

@ -63,9 +63,6 @@ export const songStore = {
} else {
Vue.set(song, 'artist', artistStore.byId(song.album.artist.id))
}
// Cache the song, so that byId() is faster
this.cache[song.id] = song
},
/**
@ -134,6 +131,10 @@ export const songStore = {
* @return {Object}
*/
byId (id) {
if (!this.cache[id]) {
this.cache[id] = find(this.all, { id })
}
return this.cache[id]
},