Skip to content

Commit

Permalink
modify disk cashe synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhirkevich Alexander Y authored and Zhirkevich Alexander Y committed Jul 29, 2024
1 parent b2bd596 commit c835ee6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private var _useStableWasmMemoryManagement : Boolean = false
* It is disabled by default. Turn this on if you have problems with dotLottie decompression on wasm
* */
@ExperimentalCompottieApi
public var Compottie.useStableWasmMemoryManagement by ::_useStableWasmMemoryManagement
public var Compottie.useStableWasmMemoryManagement: Boolean by ::_useStableWasmMemoryManagement

/**
* [LottieComposition] from a dotLottie zip archive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ public interface DiskCache {

/** Close the snapshot to allow editing. */
override fun close()

/** Close the snapshot and call [openEditor] for this entry atomically. */
public fun closeAndOpenEditor(): Editor?
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.alexzhirkevich.compottie


import androidx.compose.ui.util.fastForEach
import io.github.alexzhirkevich.compottie.DiskLruCache.Editor
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized
import kotlinx.coroutines.CoroutineDispatcher
Expand Down Expand Up @@ -131,7 +133,7 @@ internal class DiskLruCache(
private var operationsSinceRewrite = 0
private var journalWriter: BufferedSink? = null
private var hasJournalErrors = false
private var initialized = false
private val initialized = atomic(false)
private var closed = false
private var mostRecentTrimFailed = false
private var mostRecentRebuildFailed = false
Expand All @@ -144,45 +146,46 @@ internal class DiskLruCache(
}
}

fun initialize() = synchronized(lock) {
if (initialized) return
fun initialize() {
if (initialized.compareAndSet(false, true)) {

// If a temporary file exists, delete it.
fileSystem.delete(journalFileTmp)
// If a temporary file exists, delete it.
fileSystem.delete(journalFileTmp)

// If a backup file exists, use it instead.
if (fileSystem.exists(journalFileBackup)) {
// If journal file also exists just delete backup file.
if (fileSystem.exists(journalFile)) {
fileSystem.delete(journalFileBackup)
} else {
fileSystem.atomicMove(journalFileBackup, journalFile)
// If a backup file exists, use it instead.
if (fileSystem.exists(journalFileBackup)) {
// If journal file also exists just delete backup file.
if (fileSystem.exists(journalFile)) {
fileSystem.delete(journalFileBackup)
} else {
fileSystem.atomicMove(journalFileBackup, journalFile)
}
}
}

// Prefer to pick up where we left off.
if (fileSystem.exists(journalFile)) {
try {
readJournal()
processJournal()
initialized = true
return
} catch (_: IOException) {
// The journal is corrupt.
}
// Prefer to pick up where we left off.
if (fileSystem.exists(journalFile)) {
try {
readJournal()
processJournal()
initialized.value = true
return
} catch (_: IOException) {
// The journal is corrupt.
}

// The cache is corrupted; attempt to delete the contents of the directory.
// This can throw and we'll let that propagate out as it likely means there
// is a severe filesystem problem.
try {
delete()
} finally {
closed = false
// The cache is corrupted; attempt to delete the contents of the directory.
// This can throw and we'll let that propagate out as it likely means there
// is a severe filesystem problem.
try {
delete()
} finally {
closed = false
}
}
}

writeJournal()
initialized = true
writeJournal()
initialized.value = true
}
}

/**
Expand Down Expand Up @@ -342,7 +345,7 @@ internal class DiskLruCache(
* Returns a snapshot of the entry named [key], or null if it doesn't exist or is not currently
* readable. If a value is returned, it is moved to the head of the LRU queue.
*/
operator fun get(key: String): Snapshot? = synchronized(lock) {
operator fun get(key: String): Snapshot? {
checkNotClosed()
validateKey(key)
initialize()
Expand Down Expand Up @@ -555,7 +558,7 @@ internal class DiskLruCache(

/** Closes this cache. Stored values will remain on the filesystem. */
override fun close() = synchronized(lock) {
if (!initialized || closed) {
if (!initialized.value || closed) {
closed = true
return
}
Expand All @@ -574,7 +577,7 @@ internal class DiskLruCache(
}

fun flush() = synchronized(lock) {
if (!initialized) return
if (!initialized.value) return

checkNotClosed()
trimToSize()
Expand Down Expand Up @@ -628,7 +631,7 @@ internal class DiskLruCache(
private fun launchCleanup() {
cleanupScope.launch {
synchronized(lock) {
if (!initialized || closed) return@launch
if (initialized.value || closed) return@launch
try {
trimToSize()
} catch (_: IOException) {
Expand Down Expand Up @@ -673,13 +676,6 @@ internal class DiskLruCache(
}
}
}

fun closeAndEdit(): Editor? {
synchronized(lock) {
close()
return edit(entry.key)
}
}
}

/** Edits the values for an entry. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ internal class RealDiskCache(
override val data get() = snapshot.file(ENTRY_DATA)

override fun close() = snapshot.close()
override fun closeAndOpenEditor() = snapshot.closeAndEdit()?.let(RealDiskCache::RealEditor)
}

private class RealEditor(private val editor: DiskLruCache.Editor) : DiskCache.Editor {
Expand Down

0 comments on commit c835ee6

Please sign in to comment.