Skip to content

Commit b053016

Browse files
authored
Merge pull request #134 from mattermost/android-alias-token-cache
perf(android): add a cache object to avoid having to decrypt the alias in every access
2 parents 9550de9 + 87cf323 commit b053016

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

android/src/main/java/com/mattermost/networkclient/ApiClientModuleImpl.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ class ApiClientModuleImpl(reactApplicationContext: ReactApplicationContext) {
2525
companion object {
2626
const val NAME = "ApiClient"
2727

28-
public lateinit var context: ReactApplicationContext
28+
lateinit var context: ReactApplicationContext
2929
private val clients = mutableMapOf<HttpUrl, NetworkClient>()
3030
private val calls = mutableMapOf<String, Call>()
3131
private lateinit var sharedPreferences: SharedPreferences
3232
private const val SHARED_PREFERENCES_NAME = "APIClientPreferences"
3333
internal val cookieJar = ReactCookieJarContainer()
34+
private val aliasTokenCache = mutableMapOf<String, String?>()
3435

3536
internal fun getClientForRequest(request: Request): NetworkClient? {
3637
var urlParts = request.url.toString().split("/")
@@ -54,9 +55,16 @@ class ApiClientModuleImpl(reactApplicationContext: ReactApplicationContext) {
5455
}
5556

5657
internal fun retrieveValue(alias: String): String? {
58+
val cacheData = this.aliasTokenCache[alias]
59+
if (cacheData != null) {
60+
return cacheData
61+
}
62+
5763
val encryptedData = sharedPreferences.getString(alias, null)
5864
if (encryptedData != null) {
59-
return KeyStoreHelper.decryptData(encryptedData)
65+
val data = KeyStoreHelper.decryptData(encryptedData)
66+
this.aliasTokenCache[alias] = data
67+
return data
6068
}
6169

6270
return null
@@ -66,6 +74,7 @@ class ApiClientModuleImpl(reactApplicationContext: ReactApplicationContext) {
6674
sharedPreferences.edit()
6775
.remove(alias)
6876
.apply()
77+
this.aliasTokenCache.remove(alias)
6978
}
7079

7180
internal fun sendJSEvent(eventName: String, data: WritableMap?) {

0 commit comments

Comments
 (0)