Skip to content

Commit 553b8db

Browse files
committed
feature: added MemoryCaches
1 parent fae9965 commit 553b8db

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed

src/main/kotlin/cn/imkarl/core/common/io/file/FileUtils.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ object FileUtils {
7777
return getClassRootDir()
7878
}
7979

80-
val resourceDir = File(getClassRootDir().toString().substringBeforeLast("/classes"), "processedResources/jvm/main")
80+
val classRootDir = getClassRootDir().absolutePath.substringBeforeLast("/classes")
81+
var resourceDir = File(classRootDir, "processedResources/jvm/main")
82+
if (resourceDir.exists()) {
83+
return resourceDir
84+
}
85+
86+
resourceDir = File(classRootDir, "resources")
8187
if (resourceDir.exists()) {
8288
return resourceDir
8389
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package cn.imkarl.core.common.memory
2+
3+
import kotlinx.coroutines.*
4+
import kotlin.reflect.KClass
5+
import kotlin.reflect.full.isSubclassOf
6+
import kotlin.time.Duration
7+
import kotlin.time.Duration.Companion.seconds
8+
9+
/**
10+
* 内存缓存
11+
*/
12+
object MemoryCaches {
13+
14+
private val scope by lazy { CoroutineScope(Dispatchers.IO + SupervisorJob()) }
15+
16+
private val memCache: MutableMap<String, Pair<Long, Any>> = java.util.HashMap()
17+
18+
init {
19+
// 定时清理过期的缓存数据
20+
scope.launch(Dispatchers.IO) {
21+
while (true) {
22+
delay(1.seconds)
23+
try {
24+
memCache.forEach { key, (expireTime, _) ->
25+
if (expireTime < System.currentTimeMillis()) {
26+
memCache.remove(key)
27+
}
28+
}
29+
} catch (_: Throwable) {
30+
}
31+
}
32+
}
33+
}
34+
35+
36+
/**
37+
* 存储K - V
38+
*/
39+
operator fun set(key: String, value: Any?): MemoryCaches {
40+
set(key, value, Long.MAX_VALUE)
41+
return this
42+
}
43+
44+
/**
45+
* 存储K - V
46+
* @param duration 存活时长 (毫秒)
47+
*/
48+
fun set(key: String, value: Any?, duration: Long): MemoryCaches {
49+
if (key.isBlank()) {
50+
return this
51+
}
52+
if (value == null) {
53+
memCache.remove(key)
54+
return this
55+
}
56+
memCache[key] = if (duration <= 0L || duration == Long.MAX_VALUE) {
57+
Pair(Long.MAX_VALUE, value)
58+
} else {
59+
Pair(System.currentTimeMillis() + duration, value)
60+
}
61+
return this
62+
}
63+
64+
/**
65+
* 存储K - V
66+
* @param duration 存活时长
67+
*/
68+
fun set(key: String, value: Any?, duration: Duration): MemoryCaches {
69+
set(key, value, duration.inWholeMilliseconds)
70+
return this
71+
}
72+
73+
74+
/**
75+
* 根据key取对应数据
76+
*/
77+
inline operator fun <reified T : Any> get(key: String): T? {
78+
return get(key, T::class)
79+
}
80+
81+
/**
82+
* 根据key取对应数据
83+
*/
84+
fun <T : Any> get(key: String, clazz: KClass<T>): T? {
85+
if (key.isBlank()) return null
86+
val pair = memCache[key]
87+
if (pair == null || pair.first < System.currentTimeMillis()) {
88+
memCache.remove(key)
89+
return null
90+
}
91+
if (pair.second.javaClass != clazz.java
92+
&& !clazz.java.isInstance(pair.second)
93+
&& !pair.second::class.isSubclassOf(clazz)) {
94+
return null
95+
}
96+
return pair.second as? T
97+
}
98+
99+
100+
/**
101+
* 根据key删除对应缓存
102+
*/
103+
fun delete(key: String): MemoryCaches {
104+
if (key.isNotBlank()) {
105+
memCache.remove(key)
106+
}
107+
return this
108+
}
109+
110+
/**
111+
* 批量删除
112+
*/
113+
fun delete(vararg keys: String): MemoryCaches {
114+
keys.forEach { key ->
115+
memCache.remove(key)
116+
}
117+
return this
118+
}
119+
120+
121+
122+
/**
123+
* 刷新已存储的Key的存活时间 (秒)
124+
*/
125+
fun updateExpire(key: String, expire: Long): MemoryCaches {
126+
if (key.isBlank()) {
127+
return this
128+
}
129+
val pair = memCache[key] ?: return this
130+
memCache.remove(key)
131+
if (pair.first >= System.currentTimeMillis()) {
132+
set(key, pair.second, expire)
133+
}
134+
return this
135+
}
136+
137+
/**
138+
* 判断 key 是否存在有效缓存
139+
*/
140+
fun exists(key: String): Boolean {
141+
if (key.isBlank()) return false
142+
val pair = memCache[key] ?: return false
143+
if (pair.first < System.currentTimeMillis()) {
144+
memCache.remove(key)
145+
return false
146+
}
147+
return true
148+
}
149+
150+
}

0 commit comments

Comments
 (0)