EHCache 3 不作为地图工作
EHCache 3 not working as a Map
我正在为我的 Spring 应用程序使用 EHCache,但我认为它没有按预期工作
如果你在下面看到我正在将相同的数据添加到 hashmap 和 Ehcache .. 但是当我试图从 ehCache 中获取它时它打印 null
我初始化缓存的方式有什么问题还是我遗漏了什么。
我的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;
}
}
我的 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;
}
}
我找到问题了。
我已经用只有 100 个条目的堆初始化了缓存,因此它只需要前 100 个条目进入缓存。
已修复该问题,它按预期工作
我正在为我的 Spring 应用程序使用 EHCache,但我认为它没有按预期工作
如果你在下面看到我正在将相同的数据添加到 hashmap 和 Ehcache .. 但是当我试图从 ehCache 中获取它时它打印 null
我初始化缓存的方式有什么问题还是我遗漏了什么。
我的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;
}
}
我的 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;
}
}
我找到问题了。
我已经用只有 100 个条目的堆初始化了缓存,因此它只需要前 100 个条目进入缓存。
已修复该问题,它按预期工作