Permalink
Browse files

simple cache for users

  • Loading branch information...
1 parent eb8ebba commit 90cbb68e5c5ea47ca3a40b3b1fb09eb90d06e8ec @bjdodson bjdodson committed Dec 19, 2011
@@ -182,10 +182,12 @@ public DbUser userForGlobalId(String personId) {
"/members/" + mFeedName);
Cursor cursor;
try {
+ String[] projection = new String[] {DbUser.COL_NAME, DbUser.COL_NAME,
+ DbUser.COL_PERSON_ID};
String selection = null;
String[] selectionArgs = null;
String order = null;
- cursor = mMusubi.getContext().getContentResolver().query(feedMembersUri, null,
+ cursor = mMusubi.getContext().getContentResolver().query(feedMembersUri, projection,
selection, selectionArgs, order);
} catch (Exception e) {
Log.e(TAG, "Error getting membership", e);
@@ -45,21 +45,22 @@ public static DbUser forFeedDetails(Context context, String name, long localId,
}
static class InFeedDbUser extends DbUser {
+ private static Context sLatestContext;
private final long mLocalId;
private final String mId;
private final String mName;
private final Uri mFeedUri;
private final boolean mIsLocalUser;
- private final Context mContext;
private Bitmap mPicture;
InFeedDbUser(Context context, String name, long localId, String personId,
Uri feedUri) {
+ sLatestContext = context.getApplicationContext();
mIsLocalUser = (localId == LOCAL_USER_ID);
mName = name;
mId = personId;
mFeedUri = feedUri;
- mContext = context;
+ sLatestContext = context;
mLocalId = localId;
}
@@ -105,7 +106,7 @@ public Bitmap getPicture() {
}
String[] projection = { COL_PICTURE };
String sortOrder = null;
- Cursor c = mContext.getContentResolver().query(
+ Cursor c = sLatestContext.getContentResolver().query(
uri, projection, selection, selectionArgs, sortOrder);
try {
if (c == null || !c.moveToFirst()) {
@@ -143,7 +144,7 @@ public String getAttribute(String attr) {
}
String[] projection = { COL_PUBLIC_KEY };
String sortOrder = null;
- Cursor c = mContext.getContentResolver().query(
+ Cursor c = sLatestContext.getContentResolver().query(
uri, projection, selection, selectionArgs, sortOrder);
try {
if (!c.moveToFirst()) {
@@ -164,7 +165,7 @@ public String getAttribute(String attr) {
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
- Cursor c = mContext.getContentResolver().query(
+ Cursor c = sLatestContext.getContentResolver().query(
uri, projection, selection, selectionArgs, sortOrder);
try {
if (c == null || !c.moveToFirst()) {
@@ -17,6 +17,7 @@
package mobisocial.socialkit.musubi;
import java.security.interfaces.RSAPublicKey;
+import java.util.LinkedHashMap;
import org.json.JSONException;
import org.json.JSONObject;
@@ -25,6 +26,7 @@
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
+import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
@@ -49,6 +51,10 @@
private DbFeed mFeed;
private DbObj mObj;
+ private static final Uri CONTACTS_URI = Uri.parse("content://" + AUTHORITY + "/contacts");
+ private static final LinkedHashMap<Long, DbUser> sUserCache = new UserCache();
+ private final ContentObserver mContactUpdateObserver;
+
public static boolean isMusubiInstalled(Context context) {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
@@ -61,12 +67,20 @@ public static boolean isMusubiIntent(Intent intent) {
}
private Musubi(Context context) {
- mContext = context;
+ mContext = context.getApplicationContext();
if (context instanceof Activity) {
setDataFromIntent(((Activity) context).getIntent());
}
mContentProviderThread = new ContentProviderThread();
mContentProviderThread.start();
+ mContactUpdateObserver = new ContentObserver(mContentProviderThread.mHandler) {
+ @Override
+ public void onChange(boolean selfChange) {
+ sUserCache.clear();
+ }
+ };
+ mContext.getContentResolver().registerContentObserver(CONTACTS_URI, true,
+ mContactUpdateObserver);
}
public static Musubi getInstance(Context context) {
@@ -295,9 +309,7 @@ public DbUser userForGlobalId(Uri feedUri, String personId) {
}
Uri uri = Uri.parse("content://" + Musubi.AUTHORITY + "/members/"
+ feedUri.getLastPathSegment());
- String[] projection = {
- DbUser.COL_ID, DbUser.COL_NAME
- };
+ String[] projection = { DbUser.COL_ID, DbUser.COL_NAME };
String selection = DbUser.COL_PERSON_ID + " = ?";
String[] selectionArgs = new String[] {
personId
@@ -322,18 +334,23 @@ public DbUser userForGlobalId(Uri feedUri, String personId) {
}
public DbUser userForLocalId(Uri feedUri, long localId) {
+ DbUser cachedUser = sUserCache.get(localId);
+ if (cachedUser != null) {
+ return cachedUser;
+ }
+
if (localId == DbUser.LOCAL_USER_ID) {
- return userForLocalDevice(feedUri);
+ DbUser user = userForLocalDevice(feedUri);
+ sUserCache.put(localId, user);
+ return user;
}
Uri uri = Uri.parse("content://" + Musubi.AUTHORITY + "/members/"
+ feedUri.getLastPathSegment());
String[] projection = {
DbUser.COL_PERSON_ID, DbUser.COL_NAME
};
String selection = DbUser.COL_ID + " = ?";
- String[] selectionArgs = new String[] {
- Long.toString(localId)
- };
+ String[] selectionArgs = new String[] { Long.toString(localId) };
String sortOrder = null;
Cursor c = mContext.getContentResolver().query(uri, projection, selection, selectionArgs,
sortOrder);
@@ -349,7 +366,9 @@ public DbUser userForLocalId(Uri feedUri, long localId) {
String name = c.getString(c.getColumnIndexOrThrow(DbUser.COL_NAME));
String globalId = c.getString(c.getColumnIndexOrThrow(DbUser.COL_PERSON_ID));
- return DbUser.forFeedDetails(mContext, name, localId, globalId, feedUri);
+ DbUser user = DbUser.forFeedDetails(mContext, name, localId, globalId, feedUri);
+ sUserCache.put(localId, user);
+ return user;
} finally {
c.close();
}
@@ -424,4 +443,16 @@ public Insertion(Uri uri, ContentValues cv) {
}
}
}
+
+ private static class UserCache extends LinkedHashMap<Long, DbUser> {
+ private static final int MAX_ENTRIES = 10;
+ public UserCache() {
+ super(10, 0.75f, true);
+ }
+
+ @Override
+ protected boolean removeEldestEntry(java.util.Map.Entry<Long, DbUser> eldest) {
+ return size() > MAX_ENTRIES;
+ }
+ }
}

0 comments on commit 90cbb68

Please sign in to comment.