private static void putControllerInMemoryCache( String objectId, LikeActionController controllerForObject) { String cacheKey = getCacheKeyForObjectId(objectId); // Move this object to the front. Also trim cache if necessary mruCacheWorkQueue.addActiveWorkItem(new MRUCacheWorkItem(cacheKey, true)); cache.put(cacheKey, controllerForObject); }
private static LikeActionController getControllerFromInMemoryCache(String objectId) { String cacheKey = getCacheKeyForObjectId(objectId); LikeActionController controller = cache.get(cacheKey); if (controller != null) { // Move this object to the front mruCacheWorkQueue.addActiveWorkItem(new MRUCacheWorkItem(cacheKey, false)); } return controller; }
private static void serializeToDiskAsync(LikeActionController controller) { String controllerJson = serializeToJson(controller); String cacheKey = getCacheKeyForObjectId(controller.objectId); if (!Utility.isNullOrEmpty(controllerJson) && !Utility.isNullOrEmpty(cacheKey)) { diskIOWorkQueue.addActiveWorkItem( new SerializeToDiskWorkItem(cacheKey, controllerJson)); } }
/** * NOTE: This MUST be called ONLY via the CreateLikeActionControllerWorkItem class to ensure * that it happens on the right thread, at the right time. */ private static LikeActionController deserializeFromDiskSynchronously(String objectId) { LikeActionController controller = null; InputStream inputStream = null; try { String cacheKey = getCacheKeyForObjectId(objectId); inputStream = controllerDiskCache.get(cacheKey); if (inputStream != null) { String controllerJsonString = Utility.readStreamToString(inputStream); if (!Utility.isNullOrEmpty(controllerJsonString)) { controller = deserializeFromJson(controllerJsonString); } } } catch (IOException e) { Log.e(TAG, "Unable to deserialize controller from disk", e); controller = null; } finally { if (inputStream != null) { Utility.closeQuietly(inputStream); } } return controller; }