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.
@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;
}
Now you are ready to use memcache hosted on your local machine.
Its simple, right?
@Autowired
private MemcachedClient memcachedClient;
memcachedClient.add(key, 0, value);
String value = (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 |