OSGi 環境で Spring Cache を動作させるのに問題があります。多分あなたは私が欠けているものを見せてくれるでしょう。
次のようなテスト中にSpring Cacheが正常に機能するように構成しました
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-test.xml"})
public class CacheDictTest {
@Autowired
Dictionary dictionary;
@Test
public void getDict5Times() {
for (int i = 0; i < 5; i++) {
System.out.println(dictionary.getSourceDomains());
}
Assert.assertTrue(true);
}
}
選択が 1 回実行されると、5 つの素敵なプリントが作成されます。
ただし、バンドルで機能させることはできません
Cacheable アノテーションは無視されているようです。クエリは、dictionary.getSourceDomains() を呼び出すたびに実行されます。コンテナーとして ServiceMix 5.3.0 を使用します。
私の構成:
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="dictionary"/>
</set>
</property>
</bean>
辞書:
public class DictionaryImpl implements Dictionary {
private DictionaryDao repository;
public DictionaryDao getRepository() {
return repository;
}
public void setRepository(DictionaryDao repository) {
this.repository = repository;
}
@Override
public List<String> getSourceDomains() {
List<DictEntry> entries = repository.getDictionary(DictTypeEnum.SOURCE_DOMAIN);
List<String> domains = new ArrayList<>();
for(DictEntry entry : entries) {
domains.add(entry.getKey());
}
return domains;
}
}
そしてダオ
public class DictionaryDaoImpl extends BaseDaoImpl implements DictionaryDao {
private static final Logger LOG = LoggerFactory.getLogger(DictionaryDaoImpl.class);
@Override
@Cacheable(value="dictionary", key="#type")
public List<DictEntry> getDictionary(DictTypeEnum type) {
LOG.info("Loading {}", type);
Query q = getSession().createQuery("from DictEntry where type=:type");
q.setParameter("type", new DictType(type.getTypeId()));
List results = q.list();
LOG.debug("Results {}", results);
return results;
}
}
私が試したこと
- @Cacheable アノテーションを DictionaryDao (インターフェース)、DictionaryImpl または Dictionary (インターフェース) に移動 - 効果なし。
- 別のキャッシュ実装 (JDK ConcurrentMap ベースのキャッシュの代わりに ehcache) を使用 - 効果なし