cody-music
Advanced tools
Comparing version 2.7.3 to 2.7.4
@@ -1,12 +0,8 @@ | ||
export declare class CacheUtil { | ||
private cache; | ||
export declare class CacheManager { | ||
private static instance; | ||
private myCache; | ||
private constructor(); | ||
static getInstance(): CacheUtil; | ||
isCacheExpired(key: string): boolean; | ||
getItem(key: string): any; | ||
setItem(key: string, value: any, expireInSeconds: number): void; | ||
deleteItem(key: string): void; | ||
resetCache(): void; | ||
checkCacheItemExpiration(): void; | ||
static getInstance(): CacheManager; | ||
get(key: string): any; | ||
set(key: string, value: any, ttl?: number): void; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class CacheUtil { | ||
const NodeCache = require("node-cache"); | ||
class CacheManager { | ||
constructor() { | ||
this.cache = {}; | ||
// every 10 minutes check if we should expire any of the cache items | ||
setInterval(() => { | ||
this.checkCacheItemExpiration(); | ||
}, 1000 * 60 * 10); | ||
this.myCache = null; | ||
// default cache of 2 minutes | ||
this.myCache = new NodeCache({ stdTTL: 120 }); | ||
} | ||
static getInstance() { | ||
if (!CacheUtil.instance) { | ||
CacheUtil.instance = new CacheUtil(); | ||
if (!CacheManager.instance) { | ||
CacheManager.instance = new CacheManager(); | ||
} | ||
return CacheUtil.instance; | ||
return CacheManager.instance; | ||
} | ||
isCacheExpired(key) { | ||
const data = this.cache[key]; | ||
const now = new Date().getTime(); | ||
if (!data) { | ||
return true; | ||
get(key) { | ||
return this.myCache.get(key); | ||
} | ||
set(key, value, ttl = -1) { | ||
if (ttl > 0) { | ||
this.myCache.set(key, value, ttl); | ||
} | ||
else { | ||
// check to see if the data for this cache key has expired | ||
const millisThreshold = data.expireInSeconds * 1000; | ||
const ellapsedMillis = now - data.millisTime; | ||
if (ellapsedMillis > millisThreshold) { | ||
return true; | ||
} | ||
// use the standard cache ttl | ||
this.myCache.set(key, value); | ||
} | ||
return false; | ||
} | ||
getItem(key) { | ||
if (this.cache[key] && !this.isCacheExpired(key)) { | ||
const val = JSON.parse(this.cache[key].value); | ||
return val; | ||
} | ||
return null; | ||
} | ||
setItem(key, value, expireInSeconds) { | ||
const now = new Date().getTime(); | ||
this.cache[key] = { | ||
value: JSON.stringify(value), | ||
expireInSeconds, | ||
millisTime: now | ||
}; | ||
} | ||
deleteItem(key) { | ||
if (this.cache[key]) { | ||
this.cache[key] = null; | ||
} | ||
} | ||
resetCache() { | ||
this.cache = {}; | ||
} | ||
// logic to check if we should expire the cache | ||
checkCacheItemExpiration() { | ||
if (this.cache) { | ||
Object.keys(this.cache).forEach(key => { | ||
if (this.isCacheExpired(key)) { | ||
// delete the cache | ||
this.deleteItem(key); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
exports.CacheUtil = CacheUtil; | ||
exports.CacheManager = CacheManager; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const util_1 = require("./util"); | ||
const cache_1 = require("./cache"); | ||
const musicClient = client_1.MusicClient.getInstance(); | ||
@@ -13,3 +14,3 @@ const musicStore = store_1.MusicStore.getInstance(); | ||
const musicUtil = new util_1.MusicUtil(); | ||
let playlists = []; | ||
const cacheMgr = cache_1.CacheManager.getInstance(); | ||
class Playlist { | ||
@@ -109,3 +110,3 @@ constructor() { | ||
async getPlaylists(qsOptions = {}) { | ||
playlists = []; | ||
let playlists = []; | ||
if (!musicStore.spotifyUserId) { | ||
@@ -272,2 +273,7 @@ await userProfile.getUserProfile(); | ||
const spotifyUserId = musicStore.spotifyUserId; | ||
let playlists = cacheMgr.get("playlists"); | ||
if (!playlists) { | ||
// fetch the playlists again | ||
playlists = await this.getPlaylists(); | ||
} | ||
// check if it's already in the playlist | ||
@@ -274,0 +280,0 @@ const existingPlaylist = playlists.length |
@@ -220,5 +220,2 @@ "use strict"; | ||
} | ||
if (spotifyTrack.available_markets) { | ||
delete spotifyTrack.available_markets; | ||
} | ||
if (spotifyTrack.external_urls) { | ||
@@ -225,0 +222,0 @@ delete spotifyTrack.external_urls; |
@@ -111,2 +111,3 @@ "use strict"; | ||
let myTrack = await CodyMusic.getSpotifyTrackById("spotify:track:0i0wnv9UoFdZ5MfuFGQzMy"); | ||
console.log(`track: ${JSON.stringify(myTrack)}`); | ||
console.log(`requested ${limit} recommendations`); | ||
@@ -113,0 +114,0 @@ console.log("seed track: ", myTrack.name); |
@@ -1,73 +0,32 @@ | ||
export class CacheUtil { | ||
private cache: any = {}; | ||
const NodeCache = require("node-cache"); | ||
private static instance: CacheUtil; | ||
export class CacheManager { | ||
private static instance: CacheManager; | ||
private myCache: any = null; | ||
private constructor() { | ||
// every 10 minutes check if we should expire any of the cache items | ||
setInterval(() => { | ||
this.checkCacheItemExpiration(); | ||
}, 1000 * 60 * 10); | ||
// default cache of 2 minutes | ||
this.myCache = new NodeCache({ stdTTL: 120 }); | ||
} | ||
static getInstance() { | ||
if (!CacheUtil.instance) { | ||
CacheUtil.instance = new CacheUtil(); | ||
static getInstance(): CacheManager { | ||
if (!CacheManager.instance) { | ||
CacheManager.instance = new CacheManager(); | ||
} | ||
return CacheUtil.instance; | ||
} | ||
isCacheExpired(key: string) { | ||
const data = this.cache[key]; | ||
const now = new Date().getTime(); | ||
if (!data) { | ||
return true; | ||
} else { | ||
// check to see if the data for this cache key has expired | ||
const millisThreshold = data.expireInSeconds * 1000; | ||
const ellapsedMillis = now - data.millisTime; | ||
if (ellapsedMillis > millisThreshold) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
return CacheManager.instance; | ||
} | ||
getItem(key: string) { | ||
if (this.cache[key] && !this.isCacheExpired(key)) { | ||
const val = JSON.parse(this.cache[key].value); | ||
return val; | ||
} | ||
return null; | ||
get(key: string) { | ||
return this.myCache.get(key); | ||
} | ||
setItem(key: string, value: any, expireInSeconds: number) { | ||
const now = new Date().getTime(); | ||
this.cache[key] = { | ||
value: JSON.stringify(value), | ||
expireInSeconds, | ||
millisTime: now | ||
}; | ||
} | ||
deleteItem(key: string) { | ||
if (this.cache[key]) { | ||
this.cache[key] = null; | ||
set(key: string, value: any, ttl: number = -1) { | ||
if (ttl > 0) { | ||
this.myCache.set(key, value, ttl); | ||
} else { | ||
// use the standard cache ttl | ||
this.myCache.set(key, value); | ||
} | ||
} | ||
resetCache() { | ||
this.cache = {}; | ||
} | ||
// logic to check if we should expire the cache | ||
checkCacheItemExpiration() { | ||
if (this.cache) { | ||
Object.keys(this.cache).forEach(key => { | ||
if (this.isCacheExpired(key)) { | ||
// delete the cache | ||
this.deleteItem(key); | ||
} | ||
}); | ||
} | ||
} | ||
} |
@@ -13,2 +13,3 @@ import { MusicClient, SPOTIFY_ROOT_API } from "./client"; | ||
import { MusicUtil } from "./util"; | ||
import { CacheManager } from "./cache"; | ||
@@ -20,3 +21,3 @@ const musicClient = MusicClient.getInstance(); | ||
let playlists: PlaylistItem[] = []; | ||
const cacheMgr: CacheManager = CacheManager.getInstance(); | ||
@@ -144,3 +145,4 @@ export class Playlist { | ||
async getPlaylists(qsOptions: any = {}): Promise<PlaylistItem[]> { | ||
playlists = []; | ||
let playlists: PlaylistItem[] = []; | ||
if (!musicStore.spotifyUserId) { | ||
@@ -358,2 +360,9 @@ await userProfile.getUserProfile(); | ||
const spotifyUserId = musicStore.spotifyUserId; | ||
let playlists: PlaylistItem[] = cacheMgr.get("playlists"); | ||
if (!playlists) { | ||
// fetch the playlists again | ||
playlists = await this.getPlaylists(); | ||
} | ||
// check if it's already in the playlist | ||
@@ -360,0 +369,0 @@ const existingPlaylist: PlaylistItem[] = playlists.length |
@@ -263,5 +263,2 @@ import { | ||
} | ||
if (spotifyTrack.available_markets) { | ||
delete spotifyTrack.available_markets; | ||
} | ||
@@ -268,0 +265,0 @@ if (spotifyTrack.external_urls) { |
{ | ||
"name": "cody-music", | ||
"version": "2.7.3", | ||
"version": "2.7.4", | ||
"description": "mac osx spotify and itunes music player controller, spotify audio features, itunes and spotify genre, and playlist control", | ||
@@ -65,4 +65,5 @@ "main": "dist/index.js", | ||
"axios": "^0.19.0", | ||
"natural": "^0.6.3" | ||
"natural": "^0.6.3", | ||
"node-cache": "^5.1.0" | ||
} | ||
} |
@@ -128,2 +128,4 @@ const expect = require("chai").expect; | ||
console.log(`track: ${JSON.stringify(myTrack)}`); | ||
console.log(`requested ${limit} recommendations`); | ||
@@ -130,0 +132,0 @@ console.log("seed track: ", myTrack.name); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
531976
3
12953
+ Addednode-cache@^5.1.0
+ Addedclone@2.1.2(transitive)
+ Addednode-cache@5.1.2(transitive)