From d0fb4de8ce6b1c1c1998c17d5efddcf55f7c8636 Mon Sep 17 00:00:00 2001 From: dosiecki Date: Wed, 31 Jan 2018 16:09:37 -0800 Subject: [PATCH] gate only story cleanup behind cooldown so cache cleanup is immediate --- .../newsblur/database/BlurDatabaseHelper.java | 2 ++ .../src/com/newsblur/service/CleanupService.java | 11 +++++++---- .../src/com/newsblur/util/AppConstants.java | 3 +++ .../src/com/newsblur/util/PrefsUtils.java | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/clients/android/NewsBlur/src/com/newsblur/database/BlurDatabaseHelper.java b/clients/android/NewsBlur/src/com/newsblur/database/BlurDatabaseHelper.java index 39f87a417..7f0e55c14 100644 --- a/clients/android/NewsBlur/src/com/newsblur/database/BlurDatabaseHelper.java +++ b/clients/android/NewsBlur/src/com/newsblur/database/BlurDatabaseHelper.java @@ -149,6 +149,7 @@ public class BlurDatabaseHelper { " AND " + DatabaseConstants.STORY_TEXT_STORY_HASH + " NOT IN " + "( SELECT " + DatabaseConstants.READING_SESSION_STORY_HASH + " FROM " + DatabaseConstants.READING_SESSION_TABLE + ")", new String[]{Long.toString(cutoffDate.getTime().getTime())}); + com.newsblur.util.Log.d(this, "cleaned up ancient stories: " + count); } } @@ -163,6 +164,7 @@ public class BlurDatabaseHelper { " AND " + DatabaseConstants.STORY_TEXT_STORY_HASH + " NOT IN " + "( SELECT " + DatabaseConstants.READING_SESSION_STORY_HASH + " FROM " + DatabaseConstants.READING_SESSION_TABLE + ")", null); + com.newsblur.util.Log.d(this, "cleaned up read stories: " + count); } } diff --git a/clients/android/NewsBlur/src/com/newsblur/service/CleanupService.java b/clients/android/NewsBlur/src/com/newsblur/service/CleanupService.java index 8e6709ced..53394d137 100644 --- a/clients/android/NewsBlur/src/com/newsblur/service/CleanupService.java +++ b/clients/android/NewsBlur/src/com/newsblur/service/CleanupService.java @@ -18,10 +18,13 @@ public class CleanupService extends SubService { protected void exec() { gotWork(); - com.newsblur.util.Log.d(this.getClass().getName(), "cleaning up old stories"); - parent.dbHelper.cleanupVeryOldStories(); - if (!PrefsUtils.isKeepOldStories(parent)) { - parent.dbHelper.cleanupReadStories(); + if (PrefsUtils.isTimeToCleanup(parent)) { + com.newsblur.util.Log.d(this.getClass().getName(), "cleaning up old stories"); + parent.dbHelper.cleanupVeryOldStories(); + if (!PrefsUtils.isKeepOldStories(parent)) { + parent.dbHelper.cleanupReadStories(); + } + PrefsUtils.updateLastCleanupTime(parent); } com.newsblur.util.Log.d(this.getClass().getName(), "cleaning up old story texts"); diff --git a/clients/android/NewsBlur/src/com/newsblur/util/AppConstants.java b/clients/android/NewsBlur/src/com/newsblur/util/AppConstants.java index e8d914629..8e86a96ad 100644 --- a/clients/android/NewsBlur/src/com/newsblur/util/AppConstants.java +++ b/clients/android/NewsBlur/src/com/newsblur/util/AppConstants.java @@ -34,6 +34,9 @@ public class AppConstants { // how often to rebuild the DB public static final long VACUUM_TIME_MILLIS = 12L * 60L * 60L * 1000L; + // how often to clean up stories from the DB + public static final long CLEANUP_TIME_MILLIS = 6L * 60L * 60L * 1000L; + // how often to trigger the BG service. slightly longer than how often we will find new stories, // to account for the fact that it is approximate, and missing a cycle is bad. public static final long BG_SERVICE_CYCLE_MILLIS = AUTO_SYNC_TIME_MILLIS + 30L * 1000L; diff --git a/clients/android/NewsBlur/src/com/newsblur/util/PrefsUtils.java b/clients/android/NewsBlur/src/com/newsblur/util/PrefsUtils.java index ef18cdc0b..85aa8c776 100644 --- a/clients/android/NewsBlur/src/com/newsblur/util/PrefsUtils.java +++ b/clients/android/NewsBlur/src/com/newsblur/util/PrefsUtils.java @@ -318,6 +318,22 @@ public class PrefsUtils { prefs.edit().putLong(PrefConstants.LAST_VACUUM_TIME, (new Date()).getTime()).commit(); } + public static boolean isTimeToCleanup(Context context) { + SharedPreferences prefs = context.getSharedPreferences(PrefConstants.PREFERENCES, 0); + long lastTime = prefs.getLong(PrefConstants.LAST_CLEANUP_TIME, 1L); + long nowTime = (new Date()).getTime(); + if ( (lastTime + AppConstants.CLEANUP_TIME_MILLIS) < nowTime ) { + return true; + } else { + return false; + } + } + + public static void updateLastCleanupTime(Context context) { + SharedPreferences prefs = context.getSharedPreferences(PrefConstants.PREFERENCES, 0); + prefs.edit().putLong(PrefConstants.LAST_CLEANUP_TIME, (new Date()).getTime()).commit(); + } + public static StoryOrder getStoryOrderForFeed(Context context, String feedId) { SharedPreferences prefs = context.getSharedPreferences(PrefConstants.PREFERENCES, 0); return StoryOrder.valueOf(prefs.getString(PrefConstants.FEED_STORY_ORDER_PREFIX + feedId, getDefaultStoryOrder(prefs).toString()));