Skip to content

Commit c7bfb5b

Browse files
Copilotnixel2007
andcommitted
Replace custom ConcurrentHashMap cache with Caffeine via CacheManager
Co-authored-by: nixel2007 <[email protected]>
1 parent dd1767d commit c7bfb5b

File tree

1 file changed

+67
-15
lines changed

1 file changed

+67
-15
lines changed

src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/SemanticTokensProvider.java

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626
import com.github._1c_syntax.bsl.languageserver.context.events.ServerContextDocumentRemovedEvent;
2727
import com.github._1c_syntax.bsl.languageserver.semantictokens.SemanticTokenEntry;
2828
import com.github._1c_syntax.bsl.languageserver.semantictokens.SemanticTokensSupplier;
29+
import com.github.benmanes.caffeine.cache.Cache;
2930
import lombok.RequiredArgsConstructor;
3031
import org.eclipse.lsp4j.SemanticTokens;
3132
import org.eclipse.lsp4j.SemanticTokensDelta;
3233
import org.eclipse.lsp4j.SemanticTokensDeltaParams;
3334
import org.eclipse.lsp4j.SemanticTokensEdit;
3435
import org.eclipse.lsp4j.SemanticTokensParams;
3536
import org.eclipse.lsp4j.jsonrpc.messages.Either;
37+
import org.springframework.cache.CacheManager;
38+
import org.springframework.cache.annotation.CacheConfig;
3639
import org.springframework.context.event.EventListener;
3740
import org.springframework.stereotype.Component;
3841

@@ -42,10 +45,8 @@
4245
import java.util.Comparator;
4346
import java.util.HashSet;
4447
import java.util.List;
45-
import java.util.Map;
4648
import java.util.Set;
4749
import java.util.UUID;
48-
import java.util.concurrent.ConcurrentHashMap;
4950

5051
/**
5152
* Провайдер для предоставления семантических токенов.
@@ -56,15 +57,16 @@
5657
*/
5758
@Component
5859
@RequiredArgsConstructor
60+
@CacheConfig(cacheNames = SemanticTokensProvider.CACHE_NAME)
5961
public class SemanticTokensProvider {
6062

61-
private final List<SemanticTokensSupplier> suppliers;
62-
6363
/**
64-
* Cache for storing previous token data by resultId.
65-
* Key: resultId, Value: token data list
64+
* Имя кэша для хранения данных семантических токенов.
6665
*/
67-
private final Map<String, CachedTokenData> tokenCache = new ConcurrentHashMap<>();
66+
public static final String CACHE_NAME = "semanticTokens";
67+
68+
private final List<SemanticTokensSupplier> suppliers;
69+
private final CacheManager cacheManager;
6870

6971
/**
7072
* Cached semantic token data associated with a document.
@@ -114,7 +116,7 @@ public Either<SemanticTokens, SemanticTokensDelta> getSemanticTokensFullDelta(
114116
SemanticTokensDeltaParams params
115117
) {
116118
String previousResultId = params.getPreviousResultId();
117-
CachedTokenData previousData = tokenCache.get(previousResultId);
119+
CachedTokenData previousData = getCachedTokenData(previousResultId);
118120

119121
// Calculate current tokens
120122
List<SemanticTokenEntry> entries = suppliers.stream()
@@ -140,7 +142,7 @@ public Either<SemanticTokens, SemanticTokensDelta> getSemanticTokensFullDelta(
140142
cacheTokenData(resultId, documentContext.getUri(), currentData);
141143

142144
// Remove the old cached data
143-
tokenCache.remove(previousResultId);
145+
evictFromCache(previousResultId);
144146

145147
var delta = new SemanticTokensDelta();
146148
delta.setResultId(resultId);
@@ -178,21 +180,71 @@ public void handleDocumentRemoved(ServerContextDocumentRemovedEvent event) {
178180
* @param uri URI документа, для которого нужно очистить кэш
179181
*/
180182
protected void clearCache(URI uri) {
181-
tokenCache.entrySet().removeIf(entry -> entry.getValue().uri().equals(uri));
183+
var cache = getNativeCache();
184+
if (cache != null) {
185+
cache.asMap().entrySet().removeIf(entry -> entry.getValue().uri().equals(uri));
186+
}
182187
}
183188

184189
/**
185-
* Generate a unique result ID for caching.
190+
* Получить нативный Caffeine кэш для семантических токенов.
191+
*
192+
* @return нативный Caffeine кэш или null, если кэш не найден
186193
*/
187-
private static String generateResultId() {
188-
return UUID.randomUUID().toString();
194+
@SuppressWarnings("unchecked")
195+
private Cache<String, CachedTokenData> getNativeCache() {
196+
var springCache = cacheManager.getCache(CACHE_NAME);
197+
if (springCache != null) {
198+
return (Cache<String, CachedTokenData>) springCache.getNativeCache();
199+
}
200+
return null;
189201
}
190202

191203
/**
192-
* Cache token data with the given resultId.
204+
* Получить кэшированные данные токенов по resultId.
205+
*
206+
* @param resultId идентификатор результата
207+
* @return кэшированные данные или null, если не найдены
208+
*/
209+
private CachedTokenData getCachedTokenData(String resultId) {
210+
var cache = getNativeCache();
211+
if (cache != null) {
212+
return cache.getIfPresent(resultId);
213+
}
214+
return null;
215+
}
216+
217+
/**
218+
* Сохранить данные токенов в кэш.
219+
*
220+
* @param resultId идентификатор результата
221+
* @param uri URI документа
222+
* @param data данные токенов
193223
*/
194224
private void cacheTokenData(String resultId, URI uri, List<Integer> data) {
195-
tokenCache.put(resultId, new CachedTokenData(uri, data));
225+
var cache = getNativeCache();
226+
if (cache != null) {
227+
cache.put(resultId, new CachedTokenData(uri, data));
228+
}
229+
}
230+
231+
/**
232+
* Удалить данные из кэша по resultId.
233+
*
234+
* @param resultId идентификатор результата для удаления
235+
*/
236+
private void evictFromCache(String resultId) {
237+
var cache = getNativeCache();
238+
if (cache != null) {
239+
cache.invalidate(resultId);
240+
}
241+
}
242+
243+
/**
244+
* Generate a unique result ID for caching.
245+
*/
246+
private static String generateResultId() {
247+
return UUID.randomUUID().toString();
196248
}
197249

198250
/**

0 commit comments

Comments
 (0)