Skip to content

Instantly share code, notes, and snippets.

@emitchel
Created April 4, 2019 04:09
Show Gist options
  • Save emitchel/a2f688e057e2e27d6b70ad34c81d8acd to your computer and use it in GitHub Desktop.
Save emitchel/a2f688e057e2e27d6b70ad34c81d8acd to your computer and use it in GitHub Desktop.
object RepositoryUtil {
fun getSecondsSinceEpoch() = OffsetDateTime.now().toEpochSecond()
/**
*
* @param cacheKey String - A unique string to represent the cache, ex: GetArtist
* @param keyDescriptor String - A string to give a secondary description ex: ACDC
* @param cacheLengthSeconds Long - How long is the cache considered fresh (use TimeUnit.[MINUTES/HOURS/DAYS].toSeconds(x))
* @return Boolean
*/
fun isCacheStale(
sharedPreferences: SharedPreferences,
cacheKey: String,
keyDescriptor: String? = "",
cacheLengthSeconds: Long
): Boolean {
val lastCacheCurrentSeconds = sharedPreferences.getLong(cacheKey.toString() + "_" + keyDescriptor, -1L)
if (lastCacheCurrentSeconds == -1L) return true
return (getSecondsSinceEpoch().minus(lastCacheCurrentSeconds)) > cacheLengthSeconds
}
fun resetCache(sharedPreferences: SharedPreferences, cacheKey: String, keyDescriptor: String? = "") {
sharedPreferences
.edit()
.putLong(cacheKey + "_" + keyDescriptor,
getSecondsSinceEpoch()
)
.apply()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment