From c51eea6268c97e63d355638b0ed0bc1033bdb16f Mon Sep 17 00:00:00 2001
From: Him188 <Him188@mamoe.net>
Date: Thu, 11 Feb 2021 16:52:22 +0800
Subject: [PATCH] - Simplify ContactListCache configuration - Add
 kotlin.Duration support - Disable contact list cache by default for stability

---
 .../api/binary-compatibility-validator.api    |  8 ++-
 .../kotlin/utils/BotConfiguration.kt          | 60 +++++++++++++------
 2 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/binary-compatibility-validator/api/binary-compatibility-validator.api b/binary-compatibility-validator/api/binary-compatibility-validator.api
index 2bbdc0fe8..91edf6844 100644
--- a/binary-compatibility-validator/api/binary-compatibility-validator.api
+++ b/binary-compatibility-validator/api/binary-compatibility-validator.api
@@ -5356,10 +5356,10 @@ public class net/mamoe/mirai/utils/BotConfiguration {
 	public static final field Companion Lnet/mamoe/mirai/utils/BotConfiguration$Companion;
 	public fun <init> ()V
 	public final fun autoReconnectOnForceOffline ()V
+	public final synthetic fun contactListCache (Lkotlin/jvm/functions/Function1;)V
 	public final fun copy ()Lnet/mamoe/mirai/utils/BotConfiguration;
-	public final fun disableContactCaches ()V
-	public final fun disableFriendListCache ()V
-	public final fun disableGroupMemberListCache ()V
+	public final fun disableContactCache ()V
+	public final fun enableContactCache ()V
 	public final fun fileBasedDeviceInfo ()V
 	public final fun fileBasedDeviceInfo (Ljava/lang/String;)V
 	public static synthetic fun fileBasedDeviceInfo$default (Lnet/mamoe/mirai/utils/BotConfiguration;Ljava/lang/String;ILjava/lang/Object;)V
@@ -5436,9 +5436,11 @@ public final class net/mamoe/mirai/utils/BotConfiguration$ContactListCache {
 	public fun <init> ()V
 	public final fun getFriendListCacheEnabled ()Z
 	public final fun getGroupMemberListCacheEnabled ()Z
+	public final synthetic fun getSaveInterval-UwyO8pc ()D
 	public final fun getSaveIntervalMillis ()J
 	public final fun setFriendListCacheEnabled (Z)V
 	public final fun setGroupMemberListCacheEnabled (Z)V
+	public final synthetic fun setSaveInterval-LRDsOJo (D)V
 	public final fun setSaveIntervalMillis (J)V
 }
 
diff --git a/mirai-core-api/src/commonMain/kotlin/utils/BotConfiguration.kt b/mirai-core-api/src/commonMain/kotlin/utils/BotConfiguration.kt
index 878119de5..bc5f4e673 100644
--- a/mirai-core-api/src/commonMain/kotlin/utils/BotConfiguration.kt
+++ b/mirai-core-api/src/commonMain/kotlin/utils/BotConfiguration.kt
@@ -25,6 +25,9 @@ import java.io.File
 import kotlin.coroutines.CoroutineContext
 import kotlin.coroutines.EmptyCoroutineContext
 import kotlin.coroutines.coroutineContext
+import kotlin.time.Duration
+import kotlin.time.ExperimentalTime
+import kotlin.time.milliseconds
 
 /**
  * [Bot] 配置. 用于 [BotFactory.newBot]
@@ -396,6 +399,9 @@ public open class BotConfiguration { // open for Java
 
     /**
      * 联系人信息缓存配置
+     * @see contactListCache
+     * @see enableContactCache
+     * @see disableContactCache
      * @since 2.4
      */
     public class ContactListCache {
@@ -405,32 +411,39 @@ public open class BotConfiguration { // open for Java
         public var saveIntervalMillis: Long = 60_000
 
         /**
-         * 开启好友列表缓存
+         * 在有修改时自动保存间隔. 默认 60 秒. 在每次登录完成后有修改时都会立即保存一次.
          */
-        public var friendListCacheEnabled: Boolean = true
+        @ExperimentalTime
+        public inline var saveInterval: Duration
+            @JvmSynthetic inline get() = saveIntervalMillis.milliseconds
+            @JvmSynthetic inline set(v) {
+                saveIntervalMillis = v.toLongMilliseconds()
+            }
 
         /**
-         * 开启群成员列表缓存
+         * 开启好友列表缓存.
          */
-        public var groupMemberListCacheEnabled: Boolean = true
+        public var friendListCacheEnabled: Boolean = false
+
+        /**
+         * 开启群成员列表缓存.
+         */
+        public var groupMemberListCacheEnabled: Boolean = false
     }
 
     /**
-     * 禁用好友列表缓存.
+     * 配置 [ContactListCache]
+     * ```
+     * contactListCache {
+     *     saveIntervalMillis = 30_000
+     *     friendListCacheEnabled = true
+     * }
+     * ```
      * @since 2.4
      */
-    @ConfigurationDsl
-    public fun disableFriendListCache() {
-        contactListCache.friendListCacheEnabled = false
-    }
-
-    /**
-     * 禁用群成员列表缓存.
-     * @since 2.4
-     */
-    @ConfigurationDsl
-    public fun disableGroupMemberListCache() {
-        contactListCache.groupMemberListCacheEnabled = false
+    @JvmSynthetic
+    public inline fun contactListCache(action: ContactListCache.() -> Unit) {
+        action.invoke(this.contactListCache)
     }
 
     /**
@@ -438,15 +451,26 @@ public open class BotConfiguration { // open for Java
      * @since 2.4
      */
     @ConfigurationDsl
-    public fun disableContactCaches() {
+    public fun disableContactCache() {
         contactListCache.friendListCacheEnabled = false
         contactListCache.groupMemberListCacheEnabled = false
     }
 
+    /**
+     * 启用好友列表和群成员列表的缓存.
+     * @since 2.4
+     */
+    @ConfigurationDsl
+    public fun enableContactCache() {
+        contactListCache.friendListCacheEnabled = true
+        contactListCache.groupMemberListCacheEnabled = true
+    }
+
     ///////////////////////////////////////////////////////////////////////////
     // Misc
     ///////////////////////////////////////////////////////////////////////////
 
+    @Suppress("DuplicatedCode")
     public fun copy(): BotConfiguration {
         return BotConfiguration().also { new ->
             // To structural order