Skip to content

Commit be9fb88

Browse files
author
Sujit Majumdar
committed
Initial Commit.
1 parent 458410f commit be9fb88

File tree

10 files changed

+324
-0
lines changed

10 files changed

+324
-0
lines changed

build.gradle

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
dependencies {
6+
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE")
7+
}
8+
}
9+
10+
apply plugin: 'java'
11+
apply plugin: 'eclipse'
12+
apply plugin: 'org.springframework.boot'
13+
apply plugin: 'io.spring.dependency-management'
14+
15+
bootJar {
16+
baseName = 'gs-spring-boot'
17+
version = '0.1.0'
18+
}
19+
20+
repositories {
21+
mavenCentral()
22+
}
23+
24+
sourceCompatibility = 1.8
25+
targetCompatibility = 1.8
26+
27+
dependencies {
28+
compile("org.springframework.boot:spring-boot-starter-web")
29+
compile("org.springframework.boot:spring-boot-starter-jdbc")
30+
compile("mysql:mysql-connector-java")
31+
compile("net.spy:spymemcached:2.12.3")
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codestuff.springcache.config;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ComponentScan;
12+
13+
import net.spy.memcached.MemcachedClient;
14+
15+
@SpringBootApplication
16+
@ComponentScan(basePackages = { "com.codestuff.springcache.*" })
17+
public class Application {
18+
19+
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(Application.class, args);
23+
LOGGER.info("Application is ready.");
24+
}
25+
26+
@Bean
27+
public MemcachedClient cacheClient() {
28+
InetSocketAddress ia = new InetSocketAddress("localhost", 11211);
29+
MemcachedClient cacheClient = null;
30+
try {
31+
cacheClient = new MemcachedClient(ia);
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
LOGGER.info("Cache setup done.");
36+
return cacheClient;
37+
}
38+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.codestuff.springcache.controller;
2+
3+
import java.util.List;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.codestuff.springcache.dao.EmployeeDAO;
14+
import com.codestuff.springcache.pojo.Employee;
15+
import com.codestuff.springcache.service.EmployeeService;
16+
17+
18+
@RestController
19+
public class EmployeeController {
20+
21+
private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeController.class);
22+
23+
@Autowired
24+
private EmployeeDAO employeeDAO;
25+
26+
@Autowired
27+
private EmployeeService service;
28+
29+
@RequestMapping("/")
30+
public String index() {
31+
return "OK";
32+
}
33+
34+
@RequestMapping(path="/insert")
35+
public void insert(@RequestParam String name, @RequestParam String place) {
36+
Employee emp = new Employee();
37+
emp.setEmpId(""+System.currentTimeMillis());
38+
emp.setName(name);
39+
emp.setPlace(place);
40+
employeeDAO.insert(emp);
41+
LOGGER.info("Employee has been created. "+emp);
42+
}
43+
44+
@RequestMapping(path="/update")
45+
public void update(@RequestParam String empid, @RequestParam String name, @RequestParam String place) {
46+
Employee emp = new Employee();
47+
emp.setEmpId(empid);
48+
emp.setName(name);
49+
emp.setPlace(place);
50+
service.update(emp);
51+
LOGGER.info("Employee updated. "+emp);
52+
}
53+
54+
@RequestMapping(path="/fetchName")
55+
public String get(String empID) {
56+
return empID+" : "+service.getName(empID);
57+
}
58+
59+
@RequestMapping(path="/fetchAll",produces=MediaType.APPLICATION_JSON_VALUE)
60+
public List<Employee> getAll() {
61+
LOGGER.info("Fetching all employees.");
62+
return service.getAll();
63+
}
64+
65+
@RequestMapping(path="/flush")
66+
public void flushCache() {
67+
service.flushCache();
68+
LOGGER.info("All cache has been flushed.");
69+
}
70+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.codestuff.springcache.dao;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.stereotype.Component;
8+
9+
import com.codestuff.springcache.pojo.Employee;
10+
11+
@Component
12+
public class EmployeeDAO implements IEmployeeDAO {
13+
14+
private static final String INSERT = "INSERT INTO EMPLOYEE(EMP_ID,NAME,PLACE) VALUES(?,?,?)";
15+
private static final String FETCH = "SELECT * FROM EMPLOYEE WHERE EMP_ID=?";
16+
private static final String FETCH_ALL = "SELECT * FROM EMPLOYEE";
17+
private static final String UPDATE = "UPDATE EMPLOYEE SET NAME=?, PLACE=? WHERE EMP_ID=?";
18+
19+
@Autowired
20+
private JdbcTemplate jdbcTemplate;
21+
22+
@Override
23+
public void insert(Employee employee) {
24+
jdbcTemplate.update(INSERT,employee.getEmpId(),employee.getName(),employee.getPlace());
25+
26+
}
27+
28+
@Override
29+
public Employee get(String empID) {
30+
return jdbcTemplate.queryForObject(FETCH, new EmployeeMapper(),empID);
31+
}
32+
33+
@Override
34+
public List<Employee> getAll() {
35+
return jdbcTemplate.query(FETCH_ALL, new EmployeeMapper());
36+
}
37+
38+
@Override
39+
public void update(Employee employee) {
40+
jdbcTemplate.update(UPDATE,employee.getName(),employee.getPlace(),employee.getEmpId());
41+
}
42+
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codestuff.springcache.dao;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
6+
import org.springframework.jdbc.core.RowMapper;
7+
8+
import com.codestuff.springcache.pojo.Employee;
9+
10+
public class EmployeeMapper implements RowMapper<Employee> {
11+
12+
@Override
13+
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
14+
Employee employee = new Employee();
15+
employee.setEmpId(rs.getString("EMP_ID"));
16+
employee.setName(rs.getString("NAME"));
17+
employee.setPlace(rs.getString("PLACE"));
18+
return employee;
19+
}
20+
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codestuff.springcache.dao;
2+
3+
import java.util.List;
4+
5+
import org.springframework.stereotype.Repository;
6+
7+
import com.codestuff.springcache.pojo.Employee;
8+
9+
@Repository
10+
public interface IEmployeeDAO {
11+
12+
public void insert(Employee employee);
13+
public Employee get(String empID);
14+
public List<Employee> getAll();
15+
public void update(Employee employee);
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.codestuff.springcache.pojo;
2+
3+
public class Employee {
4+
5+
private String empId;
6+
private String name;
7+
private String place;
8+
9+
public String getEmpId() {
10+
return empId;
11+
}
12+
public void setEmpId(String empId) {
13+
this.empId = empId;
14+
}
15+
public String getName() {
16+
return name;
17+
}
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
public String getPlace() {
22+
return place;
23+
}
24+
public void setPlace(String place) {
25+
this.place = place;
26+
}
27+
28+
public String toString() {
29+
StringBuffer emp = new StringBuffer("EMPID=").append(this.empId).append(" ")
30+
.append("NAME=").append(this.name).append(" ")
31+
.append("PLACE=").append(this.place);
32+
33+
return emp.toString();
34+
}
35+
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.codestuff.springcache.service;
2+
3+
import java.util.List;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
10+
import com.codestuff.springcache.dao.EmployeeDAO;
11+
import com.codestuff.springcache.pojo.Employee;
12+
13+
import net.spy.memcached.MemcachedClient;
14+
15+
@Service
16+
public class EmployeeService {
17+
18+
private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeService.class);
19+
20+
@Autowired
21+
private EmployeeDAO employeeDAO;
22+
23+
@Autowired
24+
private MemcachedClient cacheClient;
25+
26+
public String getName(String empID) {
27+
String name = null;
28+
if(cacheClient.get(empID)==null) {
29+
LOGGER.info("Couldn't find EMPID in cache. Fetching from DB.");
30+
name = employeeDAO.get(empID).getName();
31+
cacheClient.add(empID, 0, name);
32+
} else {
33+
LOGGER.info("You are lucky, EMPID detected in cache.");
34+
name = (String) cacheClient.get(empID);
35+
}
36+
return name;
37+
}
38+
39+
public List<Employee> getAll() {
40+
return employeeDAO.getAll();
41+
}
42+
43+
public void update(Employee employee) {
44+
cacheClient.delete(employee.getEmpId());
45+
LOGGER.info("Cache deleted for "+employee.getEmpId());
46+
employeeDAO.update(employee);
47+
}
48+
49+
public void flushCache() {
50+
cacheClient.flush();
51+
}
52+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
server.contextPath=/springbootds
2+
spring.datasource.url=jdbc:mysql://localhost:3306/TEST?autoReconnect=true&useSSL=false
3+
spring.datasource.username=root
4+
spring.datasource.password=admin123
5+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

src/main/resources/logback.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%-5level] [%logger{30}] - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
<root level="info">
9+
<appender-ref ref="STDOUT" />
10+
</root>
11+
</configuration>

0 commit comments

Comments
 (0)