|
| 1 | +# Memcached with Spring Boot [](https://raw.githubusercontent.com/CodeStuff-Repo/Spring_Memcached/master/LICENSE) |
| 2 | +## Why Memcached ? |
| 3 | +##### *Are you interacting with Database for each transaction?* |
| 4 | +Then you must apply a cache mechanism to avoid multiple database calls and faster response. |
| 5 | +## [Memcached](https://memcached.org/) |
| 6 | +A free, opensource, in-memory caching system to speedup application by reducing database load. |
| 7 | +## Memcached with Spring |
| 8 | +In this project you will find basic usage of memcached in spring **REST** application. |
| 9 | +## How to use? |
| 10 | +### Add dependency |
| 11 | +// For Gradle project add following dependency. |
| 12 | + |
| 13 | +```groovy |
| 14 | +compile("net.spy:spymemcached:2.12.3") |
| 15 | +``` |
| 16 | +### Setup Memcached |
| 17 | +Add ***MemcachedClient*** bean in your springboot config file. |
| 18 | + |
| 19 | +```java |
| 20 | +// Connecting to memcached |
| 21 | +// [host = localhost ,PORT = 11211] |
| 22 | +// Initializing MemcachedClient. |
| 23 | +@Bean |
| 24 | + public MemcachedClient cacheClient() { |
| 25 | + InetSocketAddress ia = new InetSocketAddress("localhost", 11211); |
| 26 | + MemcachedClient cacheClient = null; |
| 27 | + try { |
| 28 | + cacheClient = new MemcachedClient(ia); |
| 29 | + } catch (IOException e) { |
| 30 | + e.printStackTrace(); |
| 31 | + } |
| 32 | + LOGGER.info("Cache setup done."); |
| 33 | + return cacheClient; |
| 34 | + } |
| 35 | +``` |
| 36 | +#### Done !!!! |
| 37 | +Now you are ready to use memcache hosted on your local machine. |
| 38 | +Its simple, right? |
| 39 | + |
| 40 | +### Add something to cache |
| 41 | +```java |
| 42 | +@Autowired |
| 43 | +private MemcachedClient memcachedClient; |
| 44 | + |
| 45 | +memcachedClient.add(key, 0, value); |
| 46 | +``` |
| 47 | +### Fetch from cache |
| 48 | +```java |
| 49 | +String value = (String) memcachedClient.get(key); |
| 50 | +``` |
| 51 | + |
| 52 | +### Delete from cache |
| 53 | +```java |
| 54 | +memcachedClient.delete(key); |
| 55 | +``` |
| 56 | + |
| 57 | +### Flush everything |
| 58 | +```java |
| 59 | +memcachedClient.flush(); |
| 60 | +``` |
| 61 | +## Want more? |
| 62 | +Memcached provides lot more features for caching. |
| 63 | +Still confused ? then, |
| 64 | +### Fork it and explore !!!! |
| 65 | + |
| 66 | +## Technology Stacks |
| 67 | +| Stack | Detail | |
| 68 | +| ------ | ------ | |
| 69 | +| Java | [Java 8](https://www.oracle.com/technetwork/java/javase/overview/java8-2100321.html) | |
| 70 | +|Spring boot| [2.0.3.RELEASE](https://github.com/spring-projects/spring-boot) | |
| 71 | +|Database|MySql| |
0 commit comments