@@ -88,7 +88,6 @@ abstract class Cache {
8888 val cachedData =
8989 sharedPreferences.all.keys
9090
91- Log .e(" cold_storage" , cachedData.toString())
9291 // removing stale objects from the cache
9392 cachedData.forEach { key ->
9493 val current = System .currentTimeMillis()
@@ -244,42 +243,6 @@ abstract class Cache {
244243 }
245244 }
246245
247-
248- /* *
249- * Method to fetch the data from the cache.
250- *
251- * @param key The key for which the data needs to be fetched.
252- */
253- private fun fetchFromCache (key : String ): CachedDataModel ? {
254- return if (cache.containsKey(key)) {
255- val current = System .currentTimeMillis()
256- val cachedDataModel = cache[key]!!
257- if (cachedDataModel.timeToLive != null ) {
258- val difference = current - cachedDataModel.timestamp
259- if (difference > cachedDataModel.timeToLive) {
260- Log .i(" COLD_STORAGE" , " Cache miss due to stale data" )
261- return null
262- }
263- Log .i(" COLD_STORAGE" , " Cache hit" )
264- return cachedDataModel
265- } else if (maxTimeToLive != null ) {
266- val differenceGlobal = current - cachedDataModel.timestamp
267- if (differenceGlobal > maxTimeToLive!! ) {
268- Log .i(" COLD_STORAGE" , " Cache miss due to stale data" )
269- return null
270- }
271- Log .i(" COLD_STORAGE" , " Cache hit" )
272- return cachedDataModel
273- }
274- Log .i(" COLD_STORAGE" , " Cache hit" )
275- return cache[key]
276- } else {
277- Log .i(" COLD_STORAGE" , " Cache miss" )
278- null
279- }
280- }
281-
282-
283246 /* *
284247 * Method that is used to persist the current cache into the
285248 * shared preferences.
@@ -299,6 +262,100 @@ abstract class Cache {
299262 }
300263 }
301264
265+ /* *
266+ * Method that can be used to cache an object if the get method is not used
267+ * directly.This can be used when the data fetching logic cannot be implemented
268+ * inside the update method or the cache needs to be updated
269+ * from a different async process.
270+ *
271+ * @param key the key for which the object needs to be cached
272+ *
273+ * @param objectToCache the object that needs to be cached.This object
274+ * should be serializable so that it can be converted to a string.
275+ *
276+ * @param timeToLive the time after which the object will be considered stale.
277+ */
278+ fun addToCache (key : String , objectToCache : Any , timeToLive : Long? ) {
279+ val objectAsString = objectMapper.writeValueAsString(objectToCache)
280+ cache[key] = CachedDataModel (
281+ objectAsString,
282+ System .currentTimeMillis(), timeToLive
283+ )
284+ }
285+
286+ /* *
287+ * Method that will return the value from cache if present but it will
288+ * not make a call to the update method for updating the cache.
289+ * The caller is responsible for updating the cache if there is a cache miss
290+ * using the addToCache method.
291+ *
292+ * @param key The key for which the value needs to be fetched.
293+ *
294+ * @param converter An optional converter that will transform the string
295+ * into the required model. If the converter is not passed , the method will
296+ * return a string or a null value if the cache does not contain the data or
297+ * if the data is stale.
298+ */
299+ fun getWithoutUpdate (key : String , converter : IConverter <Any ?>? = null): Any? {
300+ if (cache.containsKey(key)) {
301+ // if data is stale null is returned.
302+ if (isDataStale(cache[key]!! )) {
303+ return null
304+ }
305+ val cachedString = cache[key]!! .objectToCache
306+ if (converter != null ) {
307+ return converter.convert(cachedString)
308+ }
309+ return cachedString
310+ }
311+ return null
312+ }
313+
314+ /* *
315+ * Method that checks if the data is stale.
316+ *
317+ * @param cachedDataModel the cached data.
318+ */
319+ private fun isDataStale (cachedDataModel : CachedDataModel ): Boolean {
320+ val current = System .currentTimeMillis()
321+ if (cachedDataModel.timeToLive != null ) {
322+ val difference = current - cachedDataModel.timestamp
323+ if (difference > cachedDataModel.timeToLive) {
324+ Log .i(" COLD_STORAGE" , " Cache miss due to stale data" )
325+ return true
326+ }
327+ return false
328+ } else if (maxTimeToLive != null ) {
329+ val differenceGlobal = current - cachedDataModel.timestamp
330+ if (differenceGlobal > maxTimeToLive!! ) {
331+ Log .i(" COLD_STORAGE" , " Cache miss due to stale data" )
332+ return true
333+ }
334+ Log .i(" COLD_STORAGE" , " Cache hit" )
335+ return false
336+ }
337+ Log .i(" COLD_STORAGE" , " Cache hit" )
338+ return false
339+
340+ }
341+
342+ /* *
343+ * Method to fetch the data from the cache.
344+ *
345+ * @param key The key for which the data needs to be fetched.
346+ */
347+ private fun fetchFromCache (key : String ): CachedDataModel ? {
348+ return if (cache.containsKey(key)) {
349+ val cachedDataModel = cache[key]!!
350+ if (isDataStale(cachedDataModel)) {
351+ return null
352+ }
353+ return cachedDataModel
354+ } else {
355+ null
356+ }
357+ }
358+
302359 /* *
303360 * The update function needs to be implemented. This method should
304361 * specify from where the data needs to be fetched if the key is
0 commit comments