Skip to content

CodeStuff-Repo/Spring_Memcached

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memcached with Spring Boot MIT license Build Status

Why Memcached ?

Are you interacting with Database for each transaction?

Then you must apply a cache mechanism to avoid multiple database calls and faster response.

A free, opensource, in-memory caching system to speedup application by reducing database load.

Memcached with Spring

In this project you will find basic usage of memcached in spring REST application.

How to use?

Add dependency

// For Gradle project add following dependency.

compile("net.spy:spymemcached:2.12.3")

Setup Memcached

Add MemcachedClient bean in your springboot config file.

// Connecting to memcached
// [host = localhost ,PORT = 11211]
// Initializing MemcachedClient.
@Bean
	public MemcachedClient cacheClient() {
		InetSocketAddress ia = new InetSocketAddress("localhost", 11211);
		MemcachedClient cacheClient = null;
		try {
		 cacheClient = new MemcachedClient(ia);
		} catch (IOException e) {
			e.printStackTrace();
		}
		LOGGER.info("Cache setup done.");
		return cacheClient;
	}

Done !!!!

Now you are ready to use memcache hosted on your local machine.
Its simple, right?

Add something to cache

@Autowired
private MemcachedClient memcachedClient;

memcachedClient.add(key, 0, value);

Fetch from cache

String value = (String) memcachedClient.get(key);

Delete from cache

memcachedClient.delete(key);

Flush everything

memcachedClient.flush();

Want more?

Memcached provides lot more features for caching.
Still confused ? then,

Fork it and explore !!!!

Technology Stacks

Stack Detail
Java Java 8
Spring boot 2.0.3.RELEASE
Database MySql