mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Expire caches on SQLite lists
This commit is contained in:
@@ -38,6 +38,12 @@ import java.util.*;
|
||||
public class SQLiteCheckmarkList extends CheckmarkList
|
||||
{
|
||||
|
||||
private static final String ADD_QUERY =
|
||||
"insert into Checkmarks(habit, timestamp, value) values (?,?,?)";
|
||||
|
||||
private static final String INVALIDATE_QUERY =
|
||||
"delete from Checkmarks where habit = ? and timestamp >= ?";
|
||||
|
||||
@Nullable
|
||||
private HabitRecord habitRecord;
|
||||
|
||||
@@ -45,7 +51,7 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
private final SQLiteUtils<CheckmarkRecord> sqlite;
|
||||
|
||||
@Nullable
|
||||
private Integer todayValue;
|
||||
private CachedData cache;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteStatement invalidateStatement;
|
||||
@@ -56,12 +62,6 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
@NonNull
|
||||
private final SQLiteDatabase db;
|
||||
|
||||
private static final String ADD_QUERY =
|
||||
"insert into Checkmarks(habit, timestamp, value) values (?,?,?)";
|
||||
|
||||
private static final String INVALIDATE_QUERY =
|
||||
"delete from Checkmarks where habit = ? and timestamp >= ?";
|
||||
|
||||
public SQLiteCheckmarkList(Habit habit)
|
||||
{
|
||||
super(habit);
|
||||
@@ -102,8 +102,7 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
check(habit.getId());
|
||||
compute(fromTimestamp, toTimestamp);
|
||||
|
||||
String query = "select habit, timestamp, value " +
|
||||
"from checkmarks " +
|
||||
String query = "select habit, timestamp, value from checkmarks " +
|
||||
"where habit = ? and timestamp >= ? and timestamp <= ? " +
|
||||
"order by timestamp desc";
|
||||
|
||||
@@ -127,10 +126,19 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
return toCheckmarks(records);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTodayValue()
|
||||
{
|
||||
if (cache == null || cache.expired())
|
||||
cache = new CachedData(super.getTodayValue());
|
||||
|
||||
return cache.todayValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateNewerThan(long timestamp)
|
||||
{
|
||||
todayValue = null;
|
||||
cache = null;
|
||||
invalidateStatement.bindLong(1, habit.getId());
|
||||
invalidateStatement.bindLong(2, timestamp);
|
||||
invalidateStatement.execute();
|
||||
@@ -142,10 +150,8 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
protected Checkmark getNewestComputed()
|
||||
{
|
||||
check(habit.getId());
|
||||
String query = "select habit, timestamp, value " +
|
||||
"from checkmarks " +
|
||||
"where habit = ? " +
|
||||
"order by timestamp desc " +
|
||||
String query = "select habit, timestamp, value from checkmarks " +
|
||||
"where habit = ? " + "order by timestamp desc " +
|
||||
"limit 1";
|
||||
|
||||
String params[] = { Long.toString(habit.getId()) };
|
||||
@@ -157,10 +163,8 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
protected Checkmark getOldestComputed()
|
||||
{
|
||||
check(habit.getId());
|
||||
String query = "select habit, timestamp, value " +
|
||||
"from checkmarks " +
|
||||
"where habit = ? " +
|
||||
"order by timestamp asc " +
|
||||
String query = "select habit, timestamp, value from checkmarks " +
|
||||
"where habit = ? " + "order by timestamp asc " +
|
||||
"limit 1";
|
||||
|
||||
String params[] = { Long.toString(habit.getId()) };
|
||||
@@ -194,10 +198,21 @@ public class SQLiteCheckmarkList extends CheckmarkList
|
||||
return checkmarks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTodayValue()
|
||||
private static class CachedData
|
||||
{
|
||||
if(todayValue == null) todayValue = super.getTodayValue();
|
||||
return todayValue;
|
||||
int todayValue;
|
||||
|
||||
private long today;
|
||||
|
||||
CachedData(int todayValue)
|
||||
{
|
||||
this.todayValue = todayValue;
|
||||
this.today = DateUtils.getStartOfToday();
|
||||
}
|
||||
|
||||
boolean expired()
|
||||
{
|
||||
return today != DateUtils.getStartOfToday();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.activeandroid.*;
|
||||
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.models.sqlite.records.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
import org.jetbrains.annotations.*;
|
||||
|
||||
import java.util.*;
|
||||
@@ -36,6 +37,11 @@ import java.util.*;
|
||||
*/
|
||||
public class SQLiteScoreList extends ScoreList
|
||||
{
|
||||
public static final String ADD_QUERY =
|
||||
"insert into Score(habit, timestamp, score) values (?,?,?)";
|
||||
|
||||
public static final String INVALIDATE_QUERY =
|
||||
"delete from Score where habit = ? and timestamp >= ?";
|
||||
|
||||
@Nullable
|
||||
private HabitRecord habitRecord;
|
||||
@@ -43,23 +49,17 @@ public class SQLiteScoreList extends ScoreList
|
||||
@NonNull
|
||||
private final SQLiteUtils<ScoreRecord> sqlite;
|
||||
|
||||
@Nullable
|
||||
private Integer todayValue;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteStatement invalidateStatement;
|
||||
|
||||
@NonNull
|
||||
private final SQLiteStatement addStatement;
|
||||
|
||||
public static final String ADD_QUERY =
|
||||
"insert into Score(habit, timestamp, score) values (?,?,?)";
|
||||
|
||||
public static final String INVALIDATE_QUERY =
|
||||
"delete from Score where habit = ? " + "and timestamp >= ?";
|
||||
|
||||
private final SQLiteDatabase db;
|
||||
|
||||
@Nullable
|
||||
private CachedData cache = null;
|
||||
|
||||
/**
|
||||
* Constructs a new ScoreList associated with the given habit.
|
||||
*
|
||||
@@ -105,15 +105,14 @@ public class SQLiteScoreList extends ScoreList
|
||||
check(habit.getId());
|
||||
compute(fromTimestamp, toTimestamp);
|
||||
|
||||
String query = "select habit, timestamp, score " +
|
||||
"from Score " +
|
||||
"where habit = ? and timestamp >= ? and timestamp <= ? " +
|
||||
"order by timestamp desc";
|
||||
String query = "select habit, timestamp, score from Score " +
|
||||
"where habit = ? and timestamp >= ? and timestamp <= ? " +
|
||||
"order by timestamp desc";
|
||||
|
||||
String params[] = {
|
||||
Long.toString(habit.getId()),
|
||||
Long.toString(fromTimestamp),
|
||||
Long.toString(toTimestamp)
|
||||
Long.toString(habit.getId()),
|
||||
Long.toString(fromTimestamp),
|
||||
Long.toString(toTimestamp)
|
||||
};
|
||||
|
||||
List<ScoreRecord> records = sqlite.query(query, params);
|
||||
@@ -137,10 +136,19 @@ public class SQLiteScoreList extends ScoreList
|
||||
return getScoreFromQuery(query, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTodayValue()
|
||||
{
|
||||
if (cache == null || cache.expired())
|
||||
cache = new CachedData(super.getTodayValue());
|
||||
|
||||
return cache.todayValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateNewerThan(long timestamp)
|
||||
{
|
||||
todayValue = null;
|
||||
cache = null;
|
||||
invalidateStatement.bindLong(1, habit.getId());
|
||||
invalidateStatement.bindLong(2, timestamp);
|
||||
invalidateStatement.execute();
|
||||
@@ -171,8 +179,7 @@ public class SQLiteScoreList extends ScoreList
|
||||
{
|
||||
check(habit.getId());
|
||||
String query = "select habit, timestamp, score from Score " +
|
||||
"where habit = ? order by timestamp desc " +
|
||||
"limit 1";
|
||||
"where habit = ? order by timestamp desc limit 1";
|
||||
|
||||
String params[] = { Long.toString(habit.getId()) };
|
||||
return getScoreFromQuery(query, params);
|
||||
@@ -184,8 +191,7 @@ public class SQLiteScoreList extends ScoreList
|
||||
{
|
||||
check(habit.getId());
|
||||
String query = "select habit, timestamp, score from Score " +
|
||||
"where habit = ? order by timestamp asc " +
|
||||
"limit 1";
|
||||
"where habit = ? order by timestamp asc limit 1";
|
||||
|
||||
String params[] = { Long.toString(habit.getId()) };
|
||||
return getScoreFromQuery(query, params);
|
||||
@@ -217,10 +223,21 @@ public class SQLiteScoreList extends ScoreList
|
||||
return scores;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTodayValue()
|
||||
private static class CachedData
|
||||
{
|
||||
if (todayValue == null) todayValue = super.getTodayValue();
|
||||
return todayValue;
|
||||
int todayValue;
|
||||
|
||||
private long today;
|
||||
|
||||
CachedData(int todayValue)
|
||||
{
|
||||
this.todayValue = todayValue;
|
||||
this.today = DateUtils.getStartOfToday();
|
||||
}
|
||||
|
||||
boolean expired()
|
||||
{
|
||||
return today != DateUtils.getStartOfToday();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user