@@ -113,30 +113,48 @@ class DiskCache(
113113 override fun loadClientRegistration (cacheKey : ClientRegistrationCacheKey ): ClientRegistration ? {
114114 LOG .info { " loadClientRegistration for $cacheKey " }
115115 val cacheFile = clientRegistrationCache(cacheKey)
116-
117- // try InMemoryCacheFirst in case of stale registration on full disk
118- InMemoryCache .get(cacheFile.toString())?.let { data ->
119- ByteArrayInputStream (data).use { memoryStream ->
120- return loadClientRegistration(memoryStream)
116+ val diskData = cacheFile.tryInputStreamIfExists()?.use { it.readBytes() }
117+ val memoryData = InMemoryCache .get(cacheFile.toString())
118+
119+ when {
120+ diskData != null && memoryData != null -> {
121+ // Both disk and memory have data, compare them
122+ if (! diskData.contentEquals(memoryData)) {
123+ LOG .info { " Inconsistency detected between disk and memory cache" }
124+ // Prefer disk data as it's considered more up-to-date
125+ InMemoryCache .put(cacheFile.toString(), diskData)
126+ return loadClientRegistration(ByteArrayInputStream (diskData))
127+ }
128+ // If they're the same, use memory data for performance
129+ return loadClientRegistration(ByteArrayInputStream (memoryData))
130+ }
131+ diskData != null -> {
132+ // Only disk has data, update memory and use disk data
133+ InMemoryCache .put(cacheFile.toString(), diskData)
134+ return loadClientRegistration(ByteArrayInputStream (diskData))
135+ }
136+ memoryData != null -> {
137+ // Only memory has data, unusual case, log warning and use memory data
138+ LOG .info { " Client registration found in memory but not on disk" }
139+ return loadClientRegistration(ByteArrayInputStream (memoryData))
140+ }
141+ else -> {
142+ // Neither disk nor memory has data
143+ LOG .info { " Failed to load Client Registration: not found in disk or memory cache" }
144+ val stage = LoadCredentialStage .ACCESS_FILE
145+ AuthTelemetry .modifyConnection(
146+ action = " Load cache file" ,
147+ source = " loadClientRegistration" ,
148+ result = Result .Failed ,
149+ reason = " Failed to load Client Registration" ,
150+ reasonDesc = " Load Step:$stage failed. Cache file does not exist"
151+ )
152+ return null
121153 }
122154 }
123-
124- val inputStream = cacheFile.tryInputStreamIfExists()
125- if (inputStream == null ) {
126- val stage = LoadCredentialStage .ACCESS_FILE
127- LOG .info { " Failed to load Client Registration: cache file does not exist" }
128- AuthTelemetry .modifyConnection(
129- action = " Load cache file" ,
130- source = " loadClientRegistration" ,
131- result = Result .Failed ,
132- reason = " Failed to load Client Registration" ,
133- reasonDesc = " Load Step:$stage failed. Cache file does not exist"
134- )
135- return null
136- }
137- return loadClientRegistration(inputStream)
138155 }
139156
157+
140158 override fun saveClientRegistration (cacheKey : ClientRegistrationCacheKey , registration : ClientRegistration ) {
141159 LOG .info { " saveClientRegistration for $cacheKey " }
142160 val registrationCache = clientRegistrationCache(cacheKey)
@@ -182,18 +200,45 @@ class DiskCache(
182200 override fun loadAccessToken (cacheKey : AccessTokenCacheKey ): AccessToken ? {
183201 LOG .info { " loadAccessToken for $cacheKey " }
184202 val cacheFile = accessTokenCache(cacheKey)
185- // try InMemoryCacheFirst in case of stale token on full disk
186- InMemoryCache .get(cacheFile.toString())?.let { data ->
187- ByteArrayInputStream (data).use { memoryStream ->
188- return loadAccessToken(memoryStream)
203+ val diskData = cacheFile.tryInputStreamIfExists()?.use { it.readBytes() }
204+ val memoryData = InMemoryCache .get(cacheFile.toString())
205+
206+ when {
207+ diskData != null && memoryData != null -> {
208+ // Both disk and memory have data, compare them
209+ if (! diskData.contentEquals(memoryData)) {
210+ LOG .info { " Inconsistency detected between disk and memory cache" }
211+ // Prefer disk data as it's considered more up-to-date
212+ InMemoryCache .put(cacheFile.toString(), diskData)
213+ return loadAccessToken(ByteArrayInputStream (diskData))
214+ }
215+ // If they're the same, use memory data for performance
216+ return loadAccessToken(ByteArrayInputStream (memoryData))
217+ }
218+ diskData != null -> {
219+ // Only disk has data, update memory and use disk data
220+ InMemoryCache .put(cacheFile.toString(), diskData)
221+ return loadAccessToken(ByteArrayInputStream (diskData))
222+ }
223+ memoryData != null -> {
224+ // Only memory has data, unusual case, log warning and use memory data
225+ LOG .info { " Access Token found in memory but not on disk" }
226+ return loadAccessToken(ByteArrayInputStream (memoryData))
227+ }
228+ else -> {
229+ // Neither disk nor memory has data
230+ LOG .info { " Failed to load Access Token: not found in disk or memory cache" }
231+ val stage = LoadCredentialStage .ACCESS_FILE
232+ AuthTelemetry .modifyConnection(
233+ action = " Load cache file" ,
234+ source = " loadAccessToken" ,
235+ result = Result .Failed ,
236+ reason = " Failed to load Access Token" ,
237+ reasonDesc = " Load Step:$stage failed. Cache file does not exist"
238+ )
239+ return null
189240 }
190241 }
191-
192- val inputStream = cacheFile.tryInputStreamIfExists() ? : return null
193-
194- val token = loadAccessToken(inputStream)
195-
196- return token
197242 }
198243
199244 override fun saveAccessToken (cacheKey : AccessTokenCacheKey , accessToken : AccessToken ) {
0 commit comments