Browse Source

add removing of old cached avatars and profiles from localStorage, see #339

readme-update
Simon Grim 9 years ago
parent
commit
c07e6281ba
  1. 2
      js/interface_home.js
  2. 1
      js/tmobile.js
  3. 35
      js/twister_io.js

2
js/interface_home.js

@ -33,6 +33,8 @@ var InterfaceFunctions = function() { @@ -33,6 +33,8 @@ var InterfaceFunctions = function() {
requestTimelineUpdate("latestFirstTime",postsPerRefresh,followingUsers,promotedPostsOnly);
});
cleanupStorage();
initInterfaceCommon();
initUserSearch();
initInterfaceDirectMsg();

1
js/tmobile.js

@ -101,6 +101,7 @@ var router=new $.mobile.Router( @@ -101,6 +101,7 @@ var router=new $.mobile.Router(
initializeTwister( true, true, function() {
if( !$("#home .posts").children().length ) {
$.mobile.showPageLoadingMsg();
cleanupStorage();
getFullname( defaultScreenName, $("#home .rtitle"));
$(".mentions-count").attr("href","#mentions?user="+defaultScreenName );
$.MAL.setPostTemplate( $("#post-template-home") );

35
js/twister_io.js

@ -318,8 +318,9 @@ function _getResourceFromStorage(locator) { @@ -318,8 +318,9 @@ function _getResourceFromStorage(locator) {
if (storage.isSet(locator)) {
var storedResource = storage.get(locator);
var curTime = new Date().getTime() / 1000;
// avatar is downloaded once per day
if( storedResource.time + 3600*24 > curTime ) {
// avatar is downloaded once per day FIXME why once per day? what about profiles?
// FIXME need to check what type of data is requested and what time is allowed for it
if (storedResource.time + 86400 > curTime) { // 3600 * 24
return storedResource.data;
}
}
@ -334,6 +335,35 @@ function _putResourceIntoStorage(locator, data) { @@ -334,6 +335,35 @@ function _putResourceIntoStorage(locator, data) {
storage.set(locator, storedResource);
}
function cleanupStorage() {
var curTime = new Date().getTime() / 1000;
var storage = $.localStorage, keys = storage.keys(), item = '';
var delAvatars = delProfiles = 0;
for (var i = 0; i < keys.length; i++) {
item = keys[i];
// FIXME need to decide what time for type of data is allowed
if (item.substr(0, 7) === 'avatar:') {
if (storage.get(item).time + 86400 < curTime) { // 3600 * 24 hours
storage.remove(item);
delAvatars++;
//console.log('local storage item \'' + item + '\' was too old, deleted');
}
} else if (item.substr(0, 8) === 'profile:') {
if (storage.get(item).time + 86400 < curTime) { // 3600 * 24 hours
storage.remove(item);
delProfiles++;
//console.log('local storage item \'' + item + '\' was too old, deleted');
}
}
}
console.log('cleaning of storage is completed for ' + (new Date().getTime() / 1000 - curTime) + 's');
if (delAvatars) console.log(' ' + delAvatars + ' cached avatars was too old, deleted');
if (delProfiles) console.log(' ' + delProfiles + ' cached profiles was too old, deleted');
console.log(' ' + 'there was ' + i + ' items in total, now ' + (i - delAvatars - delProfiles));
}
// get avatar and set it in img.attr("src")
// TODO rename to getAvatarImgToELem(), move nin theme related stuff to nin's theme_option.js
function getAvatar( username, img ){
@ -351,6 +381,7 @@ function getAvatar( username, img ){ @@ -351,6 +381,7 @@ function getAvatar( username, img ){
img.attr('src', _avatarMap[username]);
} else {
var data = _getResourceFromStorage("avatar:" + username);
if( data ) {
_avatarMap[username] = data;
img.attr('src', data);

Loading…
Cancel
Save