Monday 27 June 2022

Ehcache: Hello World application

Step 1: Get an instance of CacheManager.

CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);

 

Step 2: Get an instance of Cache.

Cache<Long, String> myCache1 = cacheManager.createCache("myCache1", CacheConfigurationBuilder
				.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(100)).build());

 

Step 3: Add and retrieve the elements from Cache.

myCache1.put(11L, "Ram,45,11");
String value1 = myCache1.get(11L);

 


Find the below working application.

 

HelloWorld.java

package com.sample.app;

import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;

public class HelloWorld {

	public static void main(String[] args) {

		CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);

		Cache<Long, String> myCache1 = cacheManager.createCache("myCache1", CacheConfigurationBuilder
				.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(100)).build());

		myCache1.put(11L, "Ram,45,11");
		String value1 = myCache1.get(11L);

		System.out.println("value1 : " + value1);

		cacheManager.close();

	}

}

Output

value1 : Ram,45,11



 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment