我有问题为ehcache设置正确的缓存,Spring boot 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,缓存不起作用。 ---为什么???? ---我做错了什么?
当我编辑数据并保存它时,选择全部被激活并更新列表,下一次刷新从缓存中填充 - 这工作正常
使用此设置使用缓存,但在缓存中是旧值
//@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()方法。
这是正确的还是在编辑项目并调用save(pujcka)后如何在findAll()中更新缓存?
答案 0 :(得分:0)
由于您使用的是Spring Data JPA,我不相信您可以直接通过名称引用这些参数。您可以使用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);