Home  >  Spring Core

Example of @CacheEvict in Spring

By Arvind Rai, August 03, 2018
@CacheEvict annotation in spring is used to evict cache value. @CacheEvict is applied at method level. @Cacheable sets the value in cache and on the contrary @CacheEvict evicts the cache value. At any method we can apply @Cacheable to cache result and at any other method we can apply @CacheEvict to evict cache. @CacheEvict has an attribute allEntries. By default its value is false but if true then it will evict the cache wide.

We can also put the values in cache using @CachePut. Find the link for @CachePut.

Now find the complete example step by step for @CacheEvict.
Employee.java
package com.concretepage;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
public class Employee {
	@CacheEvict(value = "emp", allEntries=true)
	public void setEmployee(int empId){
		System.out.println("execute setEmployee method..");
	}
	@Cacheable("emp") 
	public String getEmployee(int empId){
		System.out.println("execute getEmployee method..");
		if(empId==1){			
			return "Atul";
		}else{
			return "Sudhir";
		}
	}
}  
In the given class Employee.java we have two methods setEmployee() and getEmployee(). We have annotated getEmployee() with @Cacheable and it will set result in cache. setEmployee() method has been annotated with @CacheEvict and it will evict values from cache.
Find other files to run the demo application.
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
	<defaultCache eternal="true" maxElementsInMemory="100"
		overflowToDisk="false" />
	<cache name="emp" maxElementsInMemory="10000" eternal="true"
		overflowToDisk="false" />
</ehcache> 
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/cache
           http://www.springframework.org/schema/cache/spring-cache.xsd">
 
        <cache:annotation-driven />
	<bean id="employee" class="com.concretepage.Employee">
	</bean>
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
	<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml"/>

</beans> 
Now run the application.
SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
    public static void main(String... args) {
    	AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
            Employee employee=(Employee) context.getBean("employee");
            
            //calling getEmployee method first time.
            System.out.println(employee.getEmployee(1));
            
            //calling getEmployee method second time. This time, method will not execute.
            System.out.println(employee.getEmployee(1));
            
            //calling setEmployee method to evict the cache value 
            employee.setEmployee(1);
            
            //calling getEmployee method third time. This time, method will  execute again.
            System.out.println(employee.getEmployee(1));
     } 
} 
In the above code, we have called getEmployee() method many times. First time getEmployee() method runs and sets result in cache named emp. Second time getEmployee() does not run because there is value in cache. Now call setEmployee()() to evict the cache value. Again call getEmployee() method and this time it will run because cache value is empty. Find the output.
Output
execute getEmployee method..
Atul
Atul
execute setEmployee method..
execute getEmployee method..
Atul 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us