-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from cssxsh/re
Restructure
- Loading branch information
Showing
76 changed files
with
3,082 additions
and
3,834 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ | |
/test/ | ||
/out/ | ||
/run/ | ||
*.log | ||
*.log | ||
|
||
debug-sandbox |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package xyz.cssxsh.arknights | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.engine.okhttp.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.plugins.compression.* | ||
import io.ktor.client.plugins.cookies.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.utils.io.jvm.javaio.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.sync.* | ||
import kotlinx.serialization.* | ||
import java.io.File | ||
|
||
public abstract class CacheDataHolder<K : CacheKey, R : CacheInfo> { | ||
protected open val mutex: Mutex = Mutex() | ||
protected open val http: HttpClient = HttpClient(OkHttp) { | ||
BrowserUserAgent() | ||
ContentEncoding() | ||
expectSuccess = true | ||
install(HttpTimeout) { | ||
socketTimeoutMillis = 30_000 | ||
connectTimeoutMillis = 30_000 | ||
requestTimeoutMillis = null | ||
} | ||
install(HttpCookies) { | ||
storage = AcceptAllCookiesStorage() | ||
} | ||
} | ||
public abstract val folder: File | ||
|
||
protected abstract val cache: MutableMap<K, *> | ||
|
||
public abstract suspend fun load(key: K) | ||
|
||
public abstract suspend fun raw(key: K): List<R> | ||
|
||
public abstract suspend fun clear() | ||
|
||
protected abstract val ignore: suspend (exception: Throwable) -> Boolean | ||
|
||
protected val CacheKey.file: File get() = folder.resolve(filename) | ||
|
||
protected suspend inline fun <reified T> K.read(): T = mutex.withLock { | ||
return CustomJson.decodeFromString(file.readText()) | ||
} | ||
|
||
protected suspend inline fun <reified T> K.write(data: T): Unit = mutex.withLock { | ||
file.writeText(CustomJson.encodeToString(data)) | ||
} | ||
|
||
protected suspend fun HttpStatement.copyTo(target: File): Unit = supervisorScope { | ||
while (isActive) { | ||
try { | ||
execute { response -> | ||
target.outputStream().use { output -> | ||
val channel = response.bodyAsChannel() | ||
|
||
while (!channel.isClosedForRead) channel.copyTo(output) | ||
} | ||
} | ||
break | ||
} catch (cause: Throwable) { | ||
if (ignore(cause).not()) throw cause | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package xyz.cssxsh.arknights | ||
|
||
import java.time.OffsetDateTime | ||
|
||
public interface CacheInfo { | ||
public val created: OffsetDateTime | ||
public val url: String | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package xyz.cssxsh.arknights | ||
|
||
|
||
public interface CacheKey { | ||
public val filename: String | ||
public val url: String | ||
} |
Oops, something went wrong.