Created
April 4, 2019 04:09
-
-
Save emitchel/a2f688e057e2e27d6b70ad34c81d8acd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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