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.
In this project you will find basic usage of memcached in spring REST application.
// For Gradle project add following dependency.
compile("net.spy:spymemcached:2.12.3")Add MemcachedClient bean in your springboot config file.
// Connecting to memcached// [host = localhost ,PORT = 11211]// Initializing MemcachedClient.@BeanpublicMemcachedClientcacheClient() {
InetSocketAddressia = newInetSocketAddress("localhost", 11211);
MemcachedClientcacheClient = null;
try {
cacheClient = newMemcachedClient(ia);
} catch (IOExceptione) {
e.printStackTrace();
}
LOGGER.info("Cache setup done.");
returncacheClient;
}Now you are ready to use memcache hosted on your local machine.
Its simple, right?
@AutowiredprivateMemcachedClientmemcachedClient;
memcachedClient.add(key, 0, value);Stringvalue = (String) memcachedClient.get(key);memcachedClient.delete(key);memcachedClient.flush();Memcached provides lot more features for caching.
Still confused ? then,
| Stack | Detail |
|---|---|
| Java | Java 8 |
| Spring boot | 2.0.3.RELEASE |
| Database | MySql |