Permalink
Browse files

Refactored caching so it can be disabled with pref, and cache sizes h…

…ave a hard limit
  • Loading branch information...
1 parent 57531a0 commit c75a81c63a6189d268324cf72019025caa8ee74a @funkatron funkatron committed Dec 18, 2010
@@ -6,6 +6,13 @@ const SPAZ_BGNOTIFIER_DASHBOARD_STAGENAME = 'bgnotifier';
const SPAZ_DONATION_URL = 'http://getspaz.com/donate';
+const TIMELINE_CACHE_MAXENTRIES = 100;
+const TIMELINE_CACHE_MAXENTRIES_DM = 30;
+const TIMELINE_CACHE_MAXENTRIES_REPLY = 30;
+
+const CACHE_VERSION = 3; // we increment this when we change how the cache works
+
+
var LOGGEDIN_APPMENU_ITEMS = [
@@ -530,6 +537,84 @@ AppAssistant.prototype.loadTimelineCache = function(onLoad) {
};
+
+
+AppAssistant.prototype.saveTimelineCache = function(twitobj) {
+
+ sch.error('SAVETIMELINECACHE');
+
+ Mojo.Timing.resume("timing_saveTimelineCache");
+
+ var cached_items = [];
+
+ if (this.App.prefs.get('timeline-save-cache')) {
+ /*
+ generate current counts, and create array to cache
+ */
+ var num_dms = 0, num_replies = 0, num_statuses = 0;
+ var max_dms = TIMELINE_CACHE_MAXENTRIES_DM;
+ var max_replies = TIMELINE_CACHE_MAXENTRIES_REPLY;
+ var max_statuses = TIMELINE_CACHE_MAXENTRIES;
+
+ for (var i=0; i < this.App.master_timeline_model.items.length; i++) {
+ if (this.App.master_timeline_model.items[i].data.SC_is_dm) {
+ num_dms++;
+ if (num_dms <= max_dms) {
+ cached_items.push(this.App.master_timeline_model.items[i]);
+ }
+ } else if (this.App.master_timeline_model.items[i].data.SC_is_reply) {
+ num_replies++;
+ if (num_replies <= max_replies) {
+ cached_items.push(this.App.master_timeline_model.items[i]);
+ }
+ } else {
+ num_statuses++;
+ if (num_statuses <= max_statuses) {
+ cached_items.push(this.App.master_timeline_model.items[i]);
+ }
+ }
+ }
+
+
+ Mojo.Log.info('Counts: DMs %s, Replies %s, Statuses %s', num_dms, num_replies, num_statuses);
+
+ Mojo.Log.info('Length of master_timeline_model.items: '+this.App.master_timeline_model.items.length);
+ }
+
+ var twitdata = {};
+ twitdata['version'] = CACHE_VERSION;
+ twitdata['my_master_timeline_model_items'] = cached_items;
+
+ Mojo.Log.info('Length of twitdata[\'my_master_timeline_model_items\']: '+twitdata['my_master_timeline_model_items'].length);
+
+
+ if (twitobj) {
+
+ twitdata[SPAZCORE_SECTION_HOME + '_lastid'] = twitobj.getLastId(SPAZCORE_SECTION_HOME);
+ twitdata[SPAZCORE_SECTION_REPLIES + '_lastid'] = twitobj.getLastId(SPAZCORE_SECTION_REPLIES);
+ twitdata[SPAZCORE_SECTION_DMS + '_lastid'] = twitobj.getLastId(SPAZCORE_SECTION_DMS);
+
+ /*
+ write out the lastIDs to a cookie, so we can use this data in
+ the bgnotifier without loading the whole cache
+ */
+ this.saveLastIDs(
+ twitobj.getLastId(SPAZCORE_SECTION_HOME),
+ twitobj.getLastId(SPAZCORE_SECTION_REPLIES),
+ twitobj.getLastId(SPAZCORE_SECTION_DMS)
+ );
+ }
+
+ this.App.cache.save('mytimelinecache', twitdata, this.App.userid);
+
+ Mojo.Timing.pause('timing_saveTimelineCache');
+
+ Mojo.Log.error(Mojo.Timing.createTimingString("timing_", "Cache op times"));
+};
+
+
+
+
AppAssistant.prototype.considerForNotification = function(params){
Mojo.Log.error('NOTIFICATION RECEIVED in AppAssistant:%j ', params);
@@ -42,8 +42,6 @@ function MyTimelineAssistant(argFromPusher) {
this.markCacheAsRead = true;
}
- this.cacheVersion = 3; // we increment this when we change how the cache works
-
/**
* empties the timeline and resets the lastids in the twit object
*
@@ -248,6 +246,8 @@ MyTimelineAssistant.prototype.setup = function() {
swipeToDelete: false,
reorderable: false,
hasNoWidgets: true,
+ lookahead: 30,
+ renderLimit: 10,
formatters: {
'data': function(value, model) {
return thisA.renderItem(value);
@@ -709,68 +709,7 @@ MyTimelineAssistant.prototype.loadTimelineCache = function() {
MyTimelineAssistant.prototype.saveTimelineCache = function() {
- sch.error('SAVETIMELINECACHE');
-
- Mojo.Timing.resume("timing_saveTimelineCache");
-
- var cached_items = [];
-
- /*
- generate current counts, and create array to cache
- */
- var num_dms = 0, num_replies = 0, num_statuses = 0;
- var max_dms = App.prefs.get('timeline-cache-maxentries-dm');
- var max_replies = App.prefs.get('timeline-cache-maxentries-reply');
- var max_statuses = App.prefs.get('timeline-cache-maxentries');
- for (var i=0; i < App.master_timeline_model.items.length; i++) {
- if (App.master_timeline_model.items[i].data.SC_is_dm) {
- num_dms++;
- if (num_dms <= max_dms) {
- cached_items.push(App.master_timeline_model.items[i]);
- }
- } else if (App.master_timeline_model.items[i].data.SC_is_reply) {
- num_replies++;
- if (num_replies <= max_replies) {
- cached_items.push(App.master_timeline_model.items[i]);
- }
- } else {
- num_statuses++;
- if (num_statuses <= max_statuses) {
- cached_items.push(App.master_timeline_model.items[i]);
- }
- }
- }
-
-
- Mojo.Log.info('Counts: DMs %s, Replies %s, Statuses %s', num_dms, num_replies, num_statuses);
-
- Mojo.Log.info('Length of master_timeline_model.items: '+App.master_timeline_model.items.length);
-
- var twitdata = {};
- twitdata['version'] = this.cacheVersion || -1;
- twitdata['my_master_timeline_model_items'] = cached_items;
-
- Mojo.Log.info('Length of twitdata[\'my_master_timeline_model_items\']: '+twitdata['my_master_timeline_model_items'].length);
-
- twitdata[SPAZCORE_SECTION_HOME + '_lastid'] = this.twit.getLastId(SPAZCORE_SECTION_HOME);
- twitdata[SPAZCORE_SECTION_REPLIES + '_lastid'] = this.twit.getLastId(SPAZCORE_SECTION_REPLIES);
- twitdata[SPAZCORE_SECTION_DMS + '_lastid'] = this.twit.getLastId(SPAZCORE_SECTION_DMS);
-
- /*
- write out the lastIDs to a cookie, so we can use this data in
- the bgnotifier without loading the whole cache
- */
- this.getAppAssistant().saveLastIDs(
- this.twit.getLastId(SPAZCORE_SECTION_HOME),
- this.twit.getLastId(SPAZCORE_SECTION_REPLIES),
- this.twit.getLastId(SPAZCORE_SECTION_DMS)
- );
-
- App.cache.save('mytimelinecache', twitdata, App.userid);
-
- Mojo.Timing.pause('timing_saveTimelineCache');
-
- Mojo.Log.error(Mojo.Timing.createTimingString("timing_", "Cache op times"));
+ this.getAppAssistant().saveTimelineCache(this.twit);
};
@@ -60,6 +60,7 @@ PreferencesAssistant.prototype.setup = function() {
'timeline-replies-getcount':App.prefs.get('timeline-replies-getcount'),
'timeline-dm-getcount': App.prefs.get('timeline-dm-getcount'),
'timeline-text-size': App.prefs.get('timeline-text-size'),
+ 'timeline-save-cache': App.prefs.get('timeline-save-cache'),
'image-uploader': App.prefs.get('image-uploader'),
'notify-newmessages': App.prefs.get('notify-newmessages'),
'notify-mentions': App.prefs.get('notify-mentions'),
@@ -86,6 +87,17 @@ PreferencesAssistant.prototype.setup = function() {
},
this.model
);
+ /*
+ temporarily disabling sound and vibration prefs until we can sort out how to tell if sound is off
+ */
+ this.controller.setupWidget("checkbox-timeline-save-cache",
+ this.soundEnabledAtts = {
+ fieldName: 'timeline-save-cache',
+ modelProperty: 'timeline-save-cache',
+ disabledProperty: 'timeline-save-cache_disabled'
+ },
+ this.model
+ );
// this.controller.setupWidget("checkbox-vibration-enabled",
// this.soundEnabledAtts = {
// fieldName: 'vibration-enabled',
@@ -169,6 +181,7 @@ PreferencesAssistant.prototype.setup = function() {
temporarily disabling sound and vibration prefs until we can sort out how to tell if sound is off
*/
this.controller.listen('checkbox-sound-enabled', Mojo.Event.propertyChange, this.saveSettings.bindAsEventListener(this));
+ this.controller.listen('checkbox-timeline-save-cache', Mojo.Event.propertyChange, this.saveSettings.bindAsEventListener(this));
this.controller.listen('checkbox-timeline-scrollonupdate', Mojo.Event.propertyChange, this.saveSettings.bindAsEventListener(this));
this.controller.listen('checkbox-notify-newmessages', Mojo.Event.propertyChange, this.saveSettings.bindAsEventListener(this));
this.controller.listen('checkbox-network-refresh-auto', Mojo.Event.propertyChange, this.saveSettings.bindAsEventListener(this));
@@ -405,6 +418,7 @@ PreferencesAssistant.prototype.cleanup = function(event) {
temporarily disabling sound and vibration prefs until we can sort out how to tell if sound is off
*/
this.controller.stopListening('checkbox-sound-enabled', Mojo.Event.propertyChange, this.saveSettings);
+ this.controller.stopListening('checkbox-timeline-save-cache', Mojo.Event.propertyChange, this.saveSettings);
this.controller.stopListening('checkbox-network-refresh-auto', Mojo.Event.propertyChange, this.saveSettings);
this.controller.stopListening('checkbox-network-refresh-wake', Mojo.Event.propertyChange, this.saveSettings);
this.controller.stopListening('checkbox-timeline-scrollonupdate', Mojo.Event.propertyChange, this.saveSettings);
View
@@ -32,7 +32,7 @@ TempCache.prototype.initUser = function(idkey) {
idkey = this.App.userid;
}
- sch.debug('TempCache: idkey for user is '+idkey);
+ Mojo.Log.info('TempCache: idkey for user is '+idkey);
if (!this._spaztmpcache) {
this._spaztmpcache = {};
@@ -53,9 +53,9 @@ TempCache.prototype.save = function(key, val, idkey) {
idkey = this.App.userid;
}
- sch.debug("saving key:"+key);
- sch.debug("saving val:"+val);
- sch.debug("saving idkey:"+idkey);
+ Mojo.Log.info("saving key:"+key);
+ Mojo.Log.info("saving val:"+val);
+ Mojo.Log.info("saving idkey:"+idkey);
if (!this._spaztmpcache) {
this.init();
@@ -67,7 +67,12 @@ TempCache.prototype.save = function(key, val, idkey) {
this._spaztmpcache[idkey][key] = val;
- this.saveToDB(idkey);
+ /*
+ try to avoid blocking
+ */
+ var tc = this;
+ setTimeout(function() { tc.saveToDB(idkey); }, 1);
+
};
@@ -87,8 +92,8 @@ TempCache.prototype.load = function(key, idkey) {
this.initUser(idkey);
}
- sch.debug("TempCache: loading key:"+key);
- sch.debug("TempCache: loading idkey:"+idkey);
+ Mojo.Log.info("TempCache: loading key:"+key);
+ Mojo.Log.info("TempCache: loading idkey:"+idkey);
if (this._spaztmpcache[idkey][key]) {
return this._spaztmpcache[idkey][key];
@@ -109,7 +114,7 @@ TempCache.prototype.saveToDB = function(idkey) {
}
function success(tx, rs) {
- sch.debug("SUCCESS SAVING TEMP CACHE");
+ Mojo.Log.info("SUCCESS SAVING TEMP CACHE");
sch.triggerCustomEvent('temp_cache_save_db_success', document);
Mojo.Timing.pause("timing_TempCache.saveToDB");
@@ -156,7 +161,7 @@ TempCache.prototype.loadFromDB = function(onLoad, idkey) {
}
function success(tx, rs) {
- sch.debug("SUCCESS LOADING TEMP CACHE");
+ Mojo.Log.info("SUCCESS LOADING TEMP CACHE");
Mojo.Timing.resume("timing_TempCache.sch.deJSON");
for(var i = 0; i < rs.rows.length; i++) {
var this_key = rs.rows.item(i).key.replace('json_cache_', '');
@@ -168,10 +168,18 @@
<div class="palm-group palm-group-spaz">
- <div class="palm-group-title" x-mojo-loc=''>Message caches</div>
+ <div class="palm-group-title" x-mojo-loc=''>Timeline cache</div>
<div class="palm-list">
- <div class="palm-row single">
- <button class="palm-button" id="clear-cache-button">Clear caches</button>
+
+ <div class="palm-row first">
+ <div style="overflow:auto;" class="checkbox-container">
+ <div x-mojo-element="CheckBox" id="checkbox-timeline-save-cache"></div>
+ <div class="checkbox-label">Cache timeline between sessions</div>
+ </div>
+ </div>
+
+ <div class="palm-row last">
+ <button class="palm-button" id="clear-cache-button">Clear cache</button>
</div>
</div>
</div>
View
@@ -33,14 +33,12 @@ var default_preferences = {
'timeline-maxentries-dm': 50,
'timeline-maxentries-reply': 50,
- 'timeline-cache-maxentries':300,
- 'timeline-cache-maxentries-dm':100,
- 'timeline-cache-maxentries-reply':100,
-
'timeline-friends-getcount': 100,
'timeline-replies-getcount': 10,
'timeline-dm-getcount': 10,
+ 'timeline-save-cache': true,
+
'url-shortener': 'bitly',
'image-uploader': 'drippic',
@@ -633,9 +633,9 @@ BackgroundNotifier.prototype.saveTimelineCache = function() {
generate current counts, and create array to cache
*/
var num_dms = 0, num_replies = 0, num_statuses = 0;
- var max_dms = Spaz.getAppObj().prefs.get('timeline-cache-maxentries-dm');
- var max_replies = Spaz.getAppObj().prefs.get('timeline-cache-maxentries-reply');
- var max_statuses = Spaz.getAppObj().prefs.get('timeline-cache-maxentries');
+ var max_dms = TIMELINE_CACHE_MAXENTRIES_DM;
+ var max_replies = TIMELINE_CACHE_MAXENTRIES_REPLY;
+ var max_statuses = TIMELINE_CACHE_MAXENTRIES;
for (var i=0; i < Spaz.getAppObj().master_timeline_model.items.length; i++) {
if (Spaz.getAppObj().master_timeline_model.items[i].data.SC_is_dm) {
num_dms++;
@@ -661,7 +661,7 @@ BackgroundNotifier.prototype.saveTimelineCache = function() {
Mojo.Log.info('Length of master_timeline_model.items: '+Spaz.getAppObj().master_timeline_model.items.length);
var twitdata = {};
- twitdata['version'] = this.cacheVersion || -1;
+ twitdata['version'] = CACHE_VERSION;
twitdata['my_master_timeline_model_items'] = cached_items;
Mojo.Log.info('Length of twitdata[\'my_master_timeline_model_items\']: '+twitdata['my_master_timeline_model_items'].length);

0 comments on commit c75a81c

Please sign in to comment.