Skip to content

Commit

Permalink
feat: add scheduled cache cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Samarium150 committed Nov 21, 2021
1 parent 85b185c commit 389754a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
insert_final_newline = true

[*.{kt, kts}]
max_line_length = 120
tab_width = 4
ij_continuation_indent_size = 4
indent_size = 4
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.github.samarium150"
version = "1.0.0"
version = "1.1.0"

repositories {
mavenLocal()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/MiraiConsoleThrowIt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object MiraiConsoleThrowIt: KotlinPlugin(
JvmPluginDescription(
id = "com.github.samarium150.mirai-console-throw-it",
name = "mirai-console-throw-it",
version = "1.0.0"
version = "1.1.0"
) {
author("Samarium150")
},
Expand All @@ -50,4 +50,4 @@ object MiraiConsoleThrowIt: KotlinPlugin(
Clean.unregister()
logger.info("Plugin mirai-console-throw-it unloaded")
}
}
}
12 changes: 11 additions & 1 deletion src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import java.awt.geom.Ellipse2D
import java.awt.image.BufferedImage
import java.io.File
import java.net.URL
import java.time.ZoneId
import java.util.*
import javax.imageio.ImageIO

Expand Down Expand Up @@ -71,4 +72,13 @@ object Utils {
}
}
}
}

fun getMidnight(): Date {
val date = Date.from(
Date().toInstant().atZone(ZoneId.systemDefault())
.toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant()
)
date.time += 24 * 60 * 60 * 1000
return date
}
}
32 changes: 29 additions & 3 deletions src/main/kotlin/command/Clean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import java.io.File
import java.util.*

object Clean: SimpleCommand(
MiraiConsoleThrowIt,
Expand All @@ -34,11 +35,36 @@ object Clean: SimpleCommand(
@ExperimentalCommandDescriptors
override val prefixOptional: Boolean = true

private val cacheDir = File(MiraiConsoleThrowIt.dataPath)

private val scheduler = Timer()

private val task = object : TimerTask() {
override fun run() {
Utils.cleanupDirectory(cacheDir)
.onFailure {
MiraiConsoleThrowIt.logger.error("定时清理缓存失败", it)
}
.onSuccess {
MiraiConsoleThrowIt.logger.info("定时清理缓存成功")
}
}
}

init {
scheduler.scheduleAtFixedRate(task, Utils.getMidnight(), 24 * 60 * 60 * 1000)
}

@Suppress("unused")
fun finalize() {
task.cancel()
scheduler.cancel()
}

@Suppress("unused")
@Handler
suspend fun CommandSender.handle() {
val dir = File(MiraiConsoleThrowIt.dataPath)
Utils.cleanupDirectory(dir)
Utils.cleanupDirectory(cacheDir)
.onFailure { exception ->
run {
MiraiConsoleThrowIt.logger.error(exception)
Expand All @@ -49,4 +75,4 @@ object Clean: SimpleCommand(
sendMessage("清理成功")
}
}
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/command/Throw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.github.samarium150.command

import com.github.samarium150.MiraiConsoleThrowIt
import com.github.samarium150.Utils
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.CommandSenderOnMessage
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
Expand Down Expand Up @@ -53,7 +53,7 @@ object Throw: SimpleCommand(

@Suppress("unused")
@Handler
suspend fun CommandSender.handle(target: User) {
suspend fun CommandSenderOnMessage<*>.handle(target: User) {
val resultPath = MiraiConsoleThrowIt.dataPath + target.id + ".png"
val result = File(resultPath)
if (!result.exists()) {
Expand All @@ -70,4 +70,4 @@ object Throw: SimpleCommand(
sendMessage(target.uploadImage(result))
}
}
}
}

0 comments on commit 389754a

Please sign in to comment.