[英]EHCache 3 not working as a Map
I'm using EHCache for my Spring application and I don't think its working as expected 我正在为我的Spring应用程序使用EHCache,但我认为它无法按预期工作
If you see below I'm adding the same data to a hashmap and also to Ehcache .. but when I tried to get it from the ehCache it prints null 如果您在下面看到,我正在将相同的数据添加到哈希图以及Ehcache中。.但是当我尝试从ehCache中获取它时,它会输出null
Is there anything wrong in the way I have initialized the Cache or something that I have missed. 我初始化缓存的方式有什么问题还是错过了什么?
My class 我的课
@Named
public class Myclass extends DataLoader {
private static final Logger LOGGER = Logger.getLogger(Myclass.class.getName());
@Inject
private DictionaryInitializer dictionaryLoader;
@Inject
@Named("fileLoader")
private FileLoader fileLoader;
private Cache<String,Object> dictionary;
@PostConstruct
@Override
public void loadResource() {
List<String> spellTerms = null;
dictionary=dictionaryLoader.getCache();
String fileName = "C:\\spelling.txt";
spellTerms = fileLoader.loadResource(fileName);
HashMap<String,Object> dictData = new HashMap<>();
for(String line:spellTerms)
{
for (String key : parseWords(line))
{
HashMap<String,Object> tempMap = dictionaryUtil.indexWord(key);
if(tempMap!=null && !tempMap.isEmpty())
{
Set<Map.Entry<String, Object>> entries = tempMap.entrySet();
for(Map.Entry<String, Object> entry:entries)
{
dictionary.put(entry.getKey(), entry.getValue());
}
}
dictData.putAll(tempMap);
}
}
System.out.println(dictData.get("urcle")); //prints 45670
System.out.println(dictionary.get("urcle")); // prints null
}
private static Iterable<String> parseWords(String text)
{
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("[\\w-[\\d_]]+").matcher(text.toLowerCase());
while (m.find()) {
allMatches.add(m.group());
}
return allMatches;
}
}
My DictionaryInitializer is like below 我的DictionaryInitializer如下所示
public class DictionaryInitializer {
private static final Logger LOGGER = Logger.getLogger(DictionaryInitializer.class.getName());
private Cache<String,Object> dictionary;
@PostConstruct
public void initializeCache() {
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.build(true);
this.dictionary = cacheManager.createCache("myCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, Object.class,
ResourcePoolsBuilder.heap(100)).build());
LOGGER.info("Dictionary loaded");
}
public Cache<String,Object> getCache(){
return dictionary;
}
public void setCache(Cache<String,Object> dictionary) {
this.dictionary=dictionary;
}
}
I have found the issue. 我发现了问题。
I have initialized the Cache with a heap of just 100 entry and hence it takes only the fist 100 entries into the cache. 我已经使用仅100个条目的堆初始化了Cache,因此仅将第一个100个条目带入了缓存。
Fixed that and it works as expected 修复了它,并且按预期工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.