forked from fxb/javascript-last.fm-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastfm.api.cache.js
127 lines (104 loc) · 2.8 KB
/
lastfm.api.cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
*
* Copyright (c) 2008-2009, Felix Bruns <[email protected]>
*
*/
/* Set an object on a Storage object. */
Storage.prototype.setObject = function(key, value){
this.setItem(key, JSON.stringify(value));
};
/* Get an object from a Storage object. */
Storage.prototype.getObject = function(key){
var item = this.getItem(key);
return JSON.parse(item);
};
/* Creates a new cache object. */
function LastFMCache(){
/* Expiration times. */
var MINUTE = 60;
var HOUR = MINUTE * 60;
var DAY = HOUR * 24;
var WEEK = DAY * 7;
var MONTH = WEEK * 4.34812141;
var YEAR = MONTH * 12;
/* Methods with weekly expiration. */
var weeklyMethods = [
'artist.getSimilar',
'tag.getSimilar',
'track.getSimilar',
'artist.getTopAlbums',
'artist.getTopTracks',
'geo.getTopArtists',
'geo.getTopTracks',
'tag.getTopAlbums',
'tag.getTopArtists',
'tag.getTopTags',
'tag.getTopTracks',
'user.getTopAlbums',
'user.getTopArtists',
'user.getTopTags',
'user.getTopTracks'
];
/* Name for this cache. */
var name = 'lastfm';
/* Create cache if it doesn't exist yet. */
if(localStorage.getObject(name) === null){
localStorage.setObject(name, {});
}
/* Get expiration time for given parameters. */
this.getExpirationTime = function(params){
var method = params.method;
if((/Weekly/).test(method) && !(/List/).test(method)){
if(typeof(params.to) != 'undefined' && typeof(params.from) != 'undefined'){
return YEAR;
}
else{
return WEEK;
}
}
for(var key in this.weeklyMethods){
if(method == this.weeklyMethods[key]){
return WEEK;
}
}
return -1;
};
/* Check if this cache contains specific data. */
this.contains = function(hash){
return typeof(localStorage.getObject(name)[hash]) != 'undefined' &&
typeof(localStorage.getObject(name)[hash].data) != 'undefined';
};
/* Load data from this cache. */
this.load = function(hash){
return localStorage.getObject(name)[hash].data;
};
/* Remove data from this cache. */
this.remove = function(hash){
var object = localStorage.getObject(name);
object[hash] = undefined;
localStorage.setObject(name, object);
};
/* Store data in this cache with a given expiration time. */
this.store = function(hash, data, expiration){
var object = localStorage.getObject(name);
var time = Math.round(new Date().getTime() / 1000);
object[hash] = {
data : data,
expiration : time + expiration
};
localStorage.setObject(name, object);
};
/* Check if some specific data expired. */
this.isExpired = function(hash){
var object = localStorage.getObject(name);
var time = Math.round(new Date().getTime() / 1000);
if(time > object[hash].expiration){
return true;
}
return false;
};
/* Clear this cache. */
this.clear = function(){
localStorage.setObject(name, {});
};
}