问题
I want to to configure my cache size. I am using @EnableCaching
. Here is my cached repository.
VendorRepository
public interface VendorRepository extends Repository<Vendor, Long> {
@Cacheable("vendorByUsername")
Vendor getVendorByUsername(String username);
@CacheEvict(value = {"vendorByUsername", "vendor", "vendors"}, allEntries = true)
Vendor save(Vendor vendor);
@Cacheable("vendor")
Vendor findOne(Long id);
@Cacheable("vendors")
List<Vendor> findAll();
}
It is working good right now but I want to set maximum cache size. How can I configure this in my main config file?
回答1:
@Jaiwo99 is correct.
Spring's Cache Abstraction does not deal with the particular semantics and "low-level" details of "managing" a cache's contents (such as as size, or similarly related, eviction/expiration). This is due in large part because these low-level management details vary greatly from 1 caching provider to the next.
For instance, some caching providers/implementations are highly distributed, with different policies for consistency, redundancy and mechanisms that control latency, and so on. As such, it would be very difficult to provide a consistent abstraction on top of these features given some provider don't even implement said features, or have very different "consistency" policies, etc.
Anyway, this section in the Spring Reference Guide probably sums it up best...
8.7. How can I Set the TTL/TTI/Eviction policy/XXX feature?
Directly through your cache provider. The cache abstraction is an abstraction, not a cache implementation. The solution you use might support various data policies and different topologies that other solutions do not support (for example, the JDK ConcurrentHashMap — exposing that in the cache abstraction would be useless because there would no backing support). Such functionality should be controlled directly through the backing cache (when configuring it) or through its native API.
https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache-specific-config
Cheers!
来源:https://stackoverflow.com/questions/37780249/spring-boot-cachable-cache-size