我在为ehcache ( Spring 1.5.2 )正确设置缓存时遇到了问题
@EnableCaching
@EnableScheduling
@SpringBootApplication
public class CacheTestConfig extends SpringBootServletInitializer {
@Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
factory.setConfigLocation(new ClassPathResource("ehcache.xml"));
factory.setShared(true);
return factory;
}
}ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir" />
<cache name="pujcka"
maxEntriesLocalHeap="100"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>PujckaCachingRepository .java
public interface PujckaCachingRepository extends CrudRepository<Pujcka, Integer> {
static final String CACHE_MAME = "pujcka";
@Override
@Cacheable(cacheNames = CACHE_MAME, key = "#id", condition = "#id != null") // each calling select into DB - not cached
//@Cacheable(cacheNames = CACHE_MAME, key = "#id", condition = "#id == null") //IllegalArgumentException Popis: Null key returned for cache operation
//@Cacheable(cacheNames = CACHE_MAME)
Pujcka findOne(Integer id);
@Override
@Cacheable(cacheNames = CACHE_MAME)
Iterable<Pujcka> findAll();
@Override
//@CachePut(cacheNames = CACHE_MAME, key = "#pujcka.id") //URL: http://localhost:8081/pujcka/nova Vyjimka: SpelEvaluationException Popis: EL1007E: Property or field 'id' cannot be found on null
@CachePut(cacheNames = CACHE_MAME)
<S extends Pujcka> S save(S pujcka);
@CacheEvict(cacheNames = CACHE_MAME)
default void refreshAll() {
//This method will remove all 'products' from cache, say as a result of flush API.
} }
Pujcka.java
@Entity
@ToString
@Getter @Setter
public class Pujcka implements Serializable{
private static final long serialVersionUID = -1385887062145410511L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;PujckaController.java
@Controller
@RequestMapping("pujcka")
public class PujckaController extends RootController {
@Autowired
private PujckaCachingRepository pujckaCachingRepository;
@RequestMapping(value = "nova", method = RequestMethod.POST)
public String ulozPujcku(@Valid Pujcka pujcka, BindingResult bindingResult, Model model) {
pujckaCachingRepository.save(pujcka);
pujckaCachingRepository.refreshAll();
return "redirect:/pujcka/seznam";
}
@RequestMapping(value = "seznam", method = RequestMethod.GET)
public String seznam(Model model) {
model.addAttribute("pujckaList", pujckaCachingRepository.findAll());
return getRequestPath();
}
@RequestMapping(value = "edit/id/{id}", method = RequestMethod.GET)
public String edit(@PathVariable Integer id, Model model) {
Pujcka pujcka = pujckaCachingRepository.findOne(id);
model.addAttribute("pujcka", pujcka);
return "pujcka/nova";
}当我在第一次调用时刷新http://localhost:8081/pujcka/seznam时,功能数据来自DB,第二次调用则使用缓存中的这些数据。这很好用。
当我刷新http://localhost:8081/pujcka/edit/id/1时,每次都调用DB,缓存就不能工作了。--为什么?-我做错什么了?
当我编辑并保存数据时,选择all被触发并更新list,然后从缓存填充下一次刷新--这很好
使用此设置时使用缓存,但缓存中有旧值。
//@Cacheable(cacheNames = CACHE_MAME)
Pujcka findOne(Integer id);谢谢你的帮助
更新1-这很有帮助,谢谢jmw5598
@Cacheable(cacheNames = CACHE_MAME, key = "#p0")
Pujcka findOne(Integer id);
@CachePut(cacheNames = CACHE_MAME, key="#result.id")
<S extends Pujcka> S save(S pujcka);
@CacheEvict(cacheNames = CACHE_MAME)
default void refreshAll() {
}
@Cacheable(cacheNames = CACHE_MAME)
Iterable<Pujcka> findAll();在控制器中
pujckaCachingRepository.save(pujcka);
pujckaCachingRepository.refreshAll();我删除了refreshAll()方法,并尝试编辑一些行。对于调用findOne(id),缓存工作正常,但是findAll()没有更新,所以我返回refreshAll()方法。
这是正确的,还是在编辑项和调用保存(Pujcka)之后,如何更新findAll()中的缓存?
发布于 2017-03-30 22:33:15
由于您使用的是Spring,所以我不认为您可以通过参数的名称直接引用它们。不过,您可以使用SpEL元数据来引用参数。
@Override
@Cacheable(cacheNames = CACHE_MAME, key = "#p0", condition = "#p0 != null")
Pujcka findOne(Integer id);另外,对于@CachePut,您可以使用结果元数据项引用结果对象id (或您想要用作键的任何字段)。
@Override
@CachePut(cacheNames = CACHE_MAME, key="#result.id")
<S extends Pujcka> S save(S pujcka);https://stackoverflow.com/questions/43116443
复制相似问题