Skip to content

Commit 127fec7

Browse files
committed
Add API Cache and Update Cache System
1 parent 4ab75a5 commit 127fec7

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/cache.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,25 @@ class cache {
3737
return this.redis[commands + 'Async'](param)
3838
}
3939

40-
static set (key, value, time) {
40+
static set (key, v, time) {
4141
this.connect()
42+
const value = typeof v === 'object' ? JSON.stringify(v) : v
4243
if (time) {
4344
return this.redis.setAsync('cache:' + key, value, 'EX', time)
4445
} else {
4546
return this.redis.setAsync('cache:' + key, value)
4647
}
4748
}
4849

49-
static get (key) {
50+
static async get (key) {
5051
this.connect()
51-
return this.redis.getAsync('cache:' + key)
52+
const data = await this.redis.getAsync('cache:' + key)
53+
try {
54+
const json = JSON.parse(data)
55+
return json
56+
} catch (e) {
57+
return data
58+
}
5259
}
5360
}
5461

src/controllers/netease.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Import Packages
22
const NeteaseMusic = require('simple-netease-cloud-music')
3+
const cache = require('../cache')
34
const nm = new NeteaseMusic()
45

56
const controllers = {}
@@ -17,7 +18,13 @@ controllers.summary = async (ctx, next) => {
1718
data[_.id].url = 'https://api.a632079.me/nm/redirect/music/' + _.id
1819

1920
// Get Music Detail
20-
const detail = await nm.song(_.id.toString())
21+
let detail
22+
if (await cache.get('nm:detail:' + _.id)) {
23+
detail = await cache.get('nm:detail:' + _.id)
24+
} else {
25+
detail = await nm.song(_.id.toString())
26+
cache.set('nm:detail:' + _.id, detail, 60 * 60 * 2) // Cache 2 Hour
27+
}
2128
data[_.id].name = detail.songs[0].name
2229
data[_.id].artists = []
2330
for (let artist of detail.songs[0].ar) {
@@ -26,11 +33,26 @@ controllers.summary = async (ctx, next) => {
2633
data[_.id].album = {}
2734
data[_.id].album.id = detail.songs[0].al.id
2835
data[_.id].album.name = detail.songs[0].al.name
29-
data[_.id].album.picture = (await nm.picture((await nm.album(detail.songs[0].al.id.toString())).songs[0].al.pic_str)).url
36+
37+
let album
38+
if (await cache.get('nm:album:' + detail.songs[0].al.id)) {
39+
album = await cache.get('nm:album:' + detail.songs[0].al.id)
40+
} else {
41+
album = await nm.album(detail.songs[0].al.id.toString())
42+
cache.set('nm:album:' + detail.songs[0].al.id, album, 60 * 60 * 4) // Cache 4 Hour
43+
}
44+
data[_.id].album.picture = (await nm.picture(album.songs[0].al.pic_str)).url
3045

3146
// Get Lyric
3247
if (ctx.query && ctx.query.lyric) {
33-
const lyric = await nm.lyric(_.id.toString())
48+
let lyric
49+
if (await cache.get('nm:lyric:' + _.id)) {
50+
lyric = await cache.get('nm:lyric:' + _.id)
51+
} else {
52+
lyric = await nm.lyric(_.id.toString())
53+
cache.set('nm:lyric:' + _.id, lyric, 60 * 60 * 4) // Cache 4 Hour
54+
}
55+
3456
data[_.id].lyric = {}
3557
data[_.id].lyric.base = (lyric.lrc && lyric.lrc.lyric) ? lyric.lrc.lyric : '[00:00.00] 纯音乐,敬请聆听。'
3658
data[_.id].lyric.translate = (lyric.tlyric && lyric.tlyric.lyric) ? lyric.tlyric.lyric : null

0 commit comments

Comments
 (0)