Skip to content

Commit 0b24989

Browse files
committed
feat: implement query caching system with cache configuration, event listener, and stats monitoring
1 parent c41fc13 commit 0b24989

19 files changed

+1492
-51
lines changed

api/build.gradle.kts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ dependencies {
2727

2828
implementation(rootProject.libs.adventure.api)
2929
implementation(rootProject.libs.adventure.gson)
30+
implementation(rootProject.libs.caffeine)
3031
}
3132

3233
tasks.named<ShadowJar>("shadowJar") {
3334
mergeServiceFiles()
3435

35-
val versionPath = project.version.toString().replace(".", "_").replace("-", "_")
36-
37-
relocate("io.nats", "app.simplecloud.api.shaded.v${versionPath}.nats")
38-
relocate("com.google", "app.simplecloud.api.shaded.v${versionPath}.google")
39-
relocate("build.buf", "app.simplecloud.api.shaded.v${versionPath}.buf")
40-
relocate("okhttp3", "app.simplecloud.api.shaded.v${versionPath}.okhttp3")
41-
relocate("okio", "app.simplecloud.api.shaded.v${versionPath}.okio")
42-
relocate("io.gsonfire", "app.simplecloud.api.shaded.v${versionPath}.gsonfire")
43-
relocate("org.bouncycastle", "app.simplecloud.api.shaded.v${versionPath}.bouncycastle")
44-
relocate("org.intellij", "app.simplecloud.api.shaded.v${versionPath}.intellij")
45-
relocate("org.jetbrains", "app.simplecloud.api.shaded.v${versionPath}.jetbrains")
46-
relocate("kotlin", "app.simplecloud.api.shaded.v${versionPath}.kotlin")
47-
48-
relocate("google", "app.simplecloud.api.shaded.v${versionPath}.google")
49-
relocate("native", "app.simplecloud.api.shaded.v${versionPath}.native")
50-
relocate("core", "app.simplecloud.api.shaded.v${versionPath}.core")
36+
relocate("io.nats", "app.simplecloud.api.shaded.nats")
37+
relocate("com.google", "app.simplecloud.api.shaded.google")
38+
relocate("build.buf", "app.simplecloud.api.shaded.buf")
39+
relocate("okhttp3", "app.simplecloud.api.shaded.okhttp3")
40+
relocate("okio", "app.simplecloud.api.shaded.okio")
41+
relocate("io.gsonfire", "app.simplecloud.api.shaded.gsonfire")
42+
relocate("org.bouncycastle", "app.simplecloud.api.shaded.bouncycastle")
43+
relocate("org.intellij", "app.simplecloud.api.shaded.intellij")
44+
relocate("org.jetbrains", "app.simplecloud.api.shaded.jetbrains")
45+
relocate("kotlin", "app.simplecloud.api.shaded.kotlin")
46+
relocate("com.github.benmanes.caffeine", "app.simplecloud.api.shaded.caffeine")
47+
48+
relocate("google", "app.simplecloud.api.shaded.google")
49+
relocate("native", "app.simplecloud.api.shaded.native")
50+
relocate("core", "app.simplecloud.api.shaded.core")
5151

5252
archiveClassifier.set("")
5353
}

api/src/main/java/app/simplecloud/api/CloudApi.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package app.simplecloud.api;
22

3+
import app.simplecloud.api.cache.QueryCache;
4+
import app.simplecloud.api.cache.QueryKey;
35
import app.simplecloud.api.event.EventApi;
46
import app.simplecloud.api.group.GroupApi;
57
import app.simplecloud.api.internal.CloudApiImpl;
@@ -120,5 +122,25 @@ static CloudApi create(CloudApiOptions options) {
120122
*/
121123
String getNetworkId();
122124

125+
/**
126+
* Returns the query cache for manual cache operations.
127+
*
128+
* <p>Use this for manual invalidation or to inspect cache state:
129+
* <pre>{@code
130+
* // Invalidate a specific server
131+
* cloudApi.cache().invalidate(QueryKey.of("server", serverId));
132+
*
133+
* // Invalidate all servers
134+
* cloudApi.cache().invalidateAll(QueryKey.of("servers"));
135+
*
136+
* // Get cache statistics
137+
* CacheStats stats = cloudApi.cache().getStats();
138+
* System.out.println("Cache hit rate: " + stats.getHitRate());
139+
* }</pre>
140+
*
141+
* @return the query cache
142+
*/
143+
QueryCache cache();
144+
123145
}
124146

api/src/main/java/app/simplecloud/api/CloudApiOptions.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package app.simplecloud.api;
22

3+
import app.simplecloud.api.cache.CacheConfig;
4+
35
public class CloudApiOptions {
46

57
public final static CloudApiOptions DEFAULT = new Builder().build();
@@ -8,12 +10,14 @@ public class CloudApiOptions {
810
private final String controllerUrl;
911
private final String networkId;
1012
private final String networkSecret;
13+
private final CacheConfig cacheConfig;
1114

1215
private CloudApiOptions(Builder builder) {
1316
this.natsUrl = builder.natsUrl;
1417
this.controllerUrl = builder.controllerUrl;
1518
this.networkId = builder.networkId;
1619
this.networkSecret = builder.networkSecret;
20+
this.cacheConfig = builder.cacheConfig;
1721
}
1822

1923
public String getNatsUrl() {
@@ -32,6 +36,15 @@ public String getNetworkSecret() {
3236
return networkSecret;
3337
}
3438

39+
/**
40+
* Returns the cache configuration.
41+
*
42+
* @return the cache configuration
43+
*/
44+
public CacheConfig getCacheConfig() {
45+
return cacheConfig;
46+
}
47+
3548
public static Builder builder() {
3649
return new Builder();
3750
}
@@ -41,6 +54,7 @@ public static class Builder {
4154
private String controllerUrl;
4255
private String networkId;
4356
private String networkSecret;
57+
private CacheConfig cacheConfig = CacheConfig.DEFAULT;
4458

4559
public Builder() {
4660
this.natsUrl = System.getenv().getOrDefault("SIMPLECLOUD_NATS_URL", "nats://platform.simplecloud.app:4222");
@@ -69,6 +83,31 @@ public Builder networkSecret(String networkSecret) {
6983
return this;
7084
}
7185

86+
/**
87+
* Sets the cache configuration.
88+
*
89+
* <p>By default, caching is enabled with sensible defaults.
90+
* Use {@link CacheConfig#DISABLED} to disable caching entirely.
91+
*
92+
* @param cacheConfig the cache configuration
93+
* @return this builder
94+
*/
95+
public Builder cache(CacheConfig cacheConfig) {
96+
this.cacheConfig = cacheConfig;
97+
return this;
98+
}
99+
100+
/**
101+
* Disables caching entirely.
102+
* Shorthand for {@code cache(CacheConfig.DISABLED)}.
103+
*
104+
* @return this builder
105+
*/
106+
public Builder disableCache() {
107+
this.cacheConfig = CacheConfig.DISABLED;
108+
return this;
109+
}
110+
72111
public CloudApiOptions build() {
73112
return new CloudApiOptions(this);
74113
}

0 commit comments

Comments
 (0)