Home  >  Spring Core

ConcurrentMap based Cache in Spring

By Arvind Rai, August 03, 2018
Spring provides Java based cache management using ConcurrentMap for simple functionalities. Spring provides ConcurrentMapCacheFactoryBean that comes under the package org.springframework.cache.concurrent. SimpleCacheManager is used to provide the cache manager. ConcurrentMap based Cache is simple to use and very high in performance but we can not use it for cache eviction. Overall we don't have very much control over cache if we use ConcurrentMap based Cache. So normally ConcurrentMap based Cache is used for basic functionalities, testing purpose. Find the complete example.
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.support.SimpleCacheManager">
	    <property name="caches">
	    <set>
	        <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="emp"/>
	    </set>
	    </property>
	</bean>
</beans> 
Employee.java
package com.concretepage;
import org.springframework.cache.annotation.Cacheable;
public class Employee {
	@Cacheable("emp") 
	public String getEmployee(int empId){
		System.out.println("execute getEmployee method..");
		if(empId==1){			
			return "Atul";
		}else{
			return "Sudhir";
		}
	}
} 
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));

     } 
} 
Output
execute getEmployee method..
Atul
Atul 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us