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