-
Notifications
You must be signed in to change notification settings - Fork 117
Implement persistent disk cache with EhCache for TypoDiagnostic with WordStatus enum, separated cache managers, and tested EhCacheAdapter component #3550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
4817799
2bfb4bb
44e01bb
8d9b280
1e26cd5
82b0f86
323c16c
aac548e
e8665c8
9f5cd94
889a4b9
c5d516a
bfef0d3
a438ecf
52800ab
b54a640
1c39d49
5a5aabe
d63d4fe
6804b8a
fdce260
158cb9f
b7ac4f1
2ce8ea2
467cfbc
28f2cc5
d36c235
d9e745e
104febc
3c93adf
d8552f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,3 +83,4 @@ bsl-language-server_*.zip | |
| *.log | ||
| *.hprof | ||
| /.idea/material_theme_project_new.xml | ||
| .bsl-ls-cache/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * This file is a part of BSL Language Server. | ||
| * | ||
| * Copyright (c) 2018-2025 | ||
| * Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> and contributors | ||
| * | ||
| * SPDX-License-Identifier: LGPL-3.0-or-later | ||
| * | ||
| * BSL Language Server is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3.0 of the License, or (at your option) any later version. | ||
| * | ||
| * BSL Language Server is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with BSL Language Server. | ||
| */ | ||
| package com.github._1c_syntax.bsl.languageserver.diagnostics.typo; | ||
|
|
||
| import org.springframework.cache.annotation.CachePut; | ||
| import org.springframework.cache.annotation.Cacheable; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * Component for managing persistent cache of checked words for typo diagnostic. | ||
| * Uses Spring Cache with EhCache for persistent disk storage. | ||
| */ | ||
| @Component | ||
| public class CheckedWordsHolder { | ||
|
|
||
| /** | ||
| * Get the status of a word from cache. | ||
| * | ||
| * @param lang language code ("en" or "ru") | ||
| * @param word the word to get status for | ||
| * @return WordStatus indicating if the word has an error, no error, or is missing from cache | ||
| */ | ||
| @Cacheable(value = "typoCache", key = "#lang + ':' + #word", cacheManager = "typoCacheManager") | ||
| public WordStatus getWordStatus(String lang, String word) { | ||
| return WordStatus.MISSING; | ||
| } | ||
|
|
||
| /** | ||
| * Store the status of a word in the cache. | ||
| * | ||
| * @param lang language code ("en" or "ru") | ||
| * @param word the word to store status for | ||
| * @param hasError true if the word has a typo, false otherwise | ||
| * @return the stored WordStatus | ||
| */ | ||
| @CachePut(value = "typoCache", key = "#lang + ':' + #word", cacheManager = "typoCacheManager") | ||
| public WordStatus putWordStatus(String lang, String word, boolean hasError) { | ||
| return hasError ? WordStatus.HAS_ERROR : WordStatus.NO_ERROR; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * This file is a part of BSL Language Server. | ||
| * | ||
| * Copyright (c) 2018-2025 | ||
| * Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> and contributors | ||
| * | ||
| * SPDX-License-Identifier: LGPL-3.0-or-later | ||
| * | ||
| * BSL Language Server is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3.0 of the License, or (at your option) any later version. | ||
| * | ||
| * BSL Language Server is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with BSL Language Server. | ||
| */ | ||
| package com.github._1c_syntax.bsl.languageserver.diagnostics.typo; | ||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * Status of a word's spell check result. | ||
| */ | ||
| public enum WordStatus implements Serializable { | ||
| /** | ||
| * Word has a spelling error. | ||
| */ | ||
| HAS_ERROR, | ||
|
|
||
| /** | ||
| * Word is spelled correctly. | ||
| */ | ||
| NO_ERROR, | ||
|
|
||
| /** | ||
| * Word has not been checked yet. | ||
| */ | ||
| MISSING | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,19 +22,44 @@ | |
| package com.github._1c_syntax.bsl.languageserver.infrastructure; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import com.github._1c_syntax.bsl.languageserver.diagnostics.typo.WordStatus; | ||
| import org.ehcache.config.builders.CacheConfigurationBuilder; | ||
| import org.ehcache.config.builders.CacheManagerBuilder; | ||
| import org.ehcache.config.builders.ResourcePoolsBuilder; | ||
| import org.ehcache.config.units.EntryUnit; | ||
| import org.ehcache.config.units.MemoryUnit; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.cache.Cache; | ||
| import org.springframework.cache.Cache.ValueRetrievalException; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.cache.annotation.EnableCaching; | ||
| import org.springframework.cache.caffeine.CaffeineCacheManager; | ||
| import org.springframework.cache.support.AbstractValueAdaptingCache; | ||
| import org.springframework.cache.support.SimpleCacheManager; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.Primary; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.concurrent.Callable; | ||
|
|
||
| /** | ||
| * Spring-конфигурация кэширования. | ||
| * <p> | ||
| * Для typoCache используется EhCache с персистентным хранилищем на диске. | ||
| * Для остальных кэшей (например, code lens) используется Caffeine с хранением в памяти. | ||
nixel2007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| @Configuration | ||
| @EnableCaching | ||
| public class CacheConfiguration { | ||
|
|
||
| /** | ||
| * Primary cache manager using Caffeine for in-memory caching. | ||
| * Used for all caches except typoCache. | ||
| */ | ||
| @Bean | ||
| @Primary | ||
| public CacheManager cacheManager(Caffeine<Object, Object> caffeine) { | ||
nixel2007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var caffeineCacheManager = new CaffeineCacheManager(); | ||
| caffeineCacheManager.setCaffeine(caffeine); | ||
|
|
@@ -45,4 +70,94 @@ public CacheManager cacheManager(Caffeine<Object, Object> caffeine) { | |
| public Caffeine<Object, Object> caffeineConfig() { | ||
| return Caffeine.newBuilder(); | ||
| } | ||
|
|
||
| /** | ||
| * Dedicated EhCache manager for typoCache with persistent disk storage. | ||
| * Configured programmatically without XML. | ||
| */ | ||
| @Bean(destroyMethod = "close") | ||
| public org.ehcache.CacheManager ehcacheManager( | ||
| @Value("${app.cache.path}") String cacheDirPath | ||
| ) { | ||
| var cacheDir = Path.of(cacheDirPath); | ||
|
|
||
nixel2007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Configure EhCache cache with disk persistence | ||
| var cacheConfig = CacheConfigurationBuilder | ||
| .newCacheConfigurationBuilder( | ||
| String.class, | ||
| WordStatus.class, | ||
| ResourcePoolsBuilder.newResourcePoolsBuilder() | ||
| .heap(100_000, EntryUnit.ENTRIES) | ||
| .disk(50, MemoryUnit.MB, true) | ||
| ) | ||
| .build(); | ||
|
|
||
| // Build native EhCache manager with persistence | ||
| return CacheManagerBuilder.newCacheManagerBuilder() | ||
| .with(CacheManagerBuilder.persistence(cacheDir.toFile())) | ||
| .withCache("typoCache", cacheConfig) | ||
| .build(true); | ||
| } | ||
|
|
||
| @Bean | ||
| public CacheManager typoCacheManager(org.ehcache.CacheManager ehcacheManager) { | ||
| var nativeCache = ehcacheManager.getCache("typoCache", String.class, WordStatus.class); | ||
|
|
||
| // Wrap the native cache with a custom Spring CacheManager | ||
| var simpleCacheManager = new SimpleCacheManager(); | ||
| simpleCacheManager.setCaches(List.of( | ||
| new AbstractValueAdaptingCache(false) { | ||
| @Override | ||
| protected Object lookup(Object key) { | ||
| return nativeCache.get((String) key); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "typoCache"; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getNativeCache() { | ||
| return nativeCache; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public <T> T get(Object key, Callable<T> valueLoader) { | ||
| var value = nativeCache.get((String) key); | ||
| if (value != null) { | ||
| return (T) value; | ||
| } | ||
| try { | ||
| T newValue = valueLoader.call(); | ||
| if (newValue != null) { | ||
| nativeCache.put((String) key, (WordStatus) newValue); | ||
| } | ||
| return newValue; | ||
| } catch (Exception e) { | ||
| throw new ValueRetrievalException(key, valueLoader, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void put(Object key, Object value) { | ||
| nativeCache.put((String) key, (WordStatus) value); | ||
| } | ||
|
|
||
| @Override | ||
| public void evict(Object key) { | ||
| nativeCache.remove((String) key); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| nativeCache.clear(); | ||
| } | ||
| } | ||
|
||
| )); | ||
| simpleCacheManager.afterPropertiesSet(); | ||
|
|
||
| return simpleCacheManager; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.