Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimize/ Limit size of cache Spring

I'm using @Cacheable annotation to store in cache some methods

<cache:annotation-driven />

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
             <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="method1" /> 
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="method2" />   
        </set>
    </property>
</bean>

But Once several users use the application 's cache is full thereby blocking the application. Is there any way to limit the size of cache and if yes do this may affect the application's data?

like image 379
user3816170 Avatar asked Apr 21 '16 13:04

user3816170


People also ask

What is CacheManager in Spring?

Interface CacheManagerSpring's central cache manager SPI. Allows for retrieving named Cache regions.

What is @EnableCaching in Spring boot?

The @EnableCaching annotation triggers a post-processor that inspects every Spring bean for the presence of caching annotations on public methods. If such an annotation is found, a proxy is automatically created to intercept the method call and handle the caching behavior accordingly.

What is JCache?

JCache is a de facto standard Java cache API for caching data. Also known as JSR 107 (i.e., a “Java Specification Request” from the “Java Community Process” [JCP]), this API implementation is intended to create a way for different technologies to provide a common caching interface.


1 Answers

Unfortunately ConcurrentMapCache which is produced by ConcurrentMapCacheFactoryBean doesn't allow to limit its size.

ConcurrentMapCache

Simple Cache implementation based on the core JDK java.util.concurrent package. Useful for testing or simple caching scenarios

I'd suggest using something more powerfull like EhCache-based Cache or Guava Cache (if you use Spring 4.0 +).

like image 176
Evgeny Avatar answered Oct 23 '22 13:10

Evgeny