Skip to content

Instantly share code, notes, and snippets.

@ngm9
Created February 25, 2026 10:57
Show Gist options
  • Select an option

  • Save ngm9/afcf2fd4dea66f008a29e762f58e3909 to your computer and use it in GitHub Desktop.

Select an option

Save ngm9/afcf2fd4dea66f008a29e762f58e3909 to your computer and use it in GitHub Desktop.
Code diff for UtkrushtApps/optimized-task-tracker-api-namanatodyssey - Task Session cab05039-0ed7-458b-889a-e0424cfdd4c7
diff --git a/.dockerignore b/.dockerignore
index 9cad6d9..93c3467 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,6 +1,10 @@
target/
.git/
-*.log
+.gitignore
*.md
+Dockerfile
+docker-compose.yml
.idea/
+*.iml
.vscode/
+*.sh
diff --git a/Dockerfile b/Dockerfile
index 757a4fb..378b7b9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,11 +1,17 @@
-# Use Maven base image to build the app
-FROM maven:3.9.5-eclipse-temurin-11 AS builder
+# Build stage - use Alpine-based Maven image (smaller)
+FROM maven:3.9.5-eclipse-temurin-11-alpine AS builder
WORKDIR /app
-COPY . .
-RUN mvn clean package -DskipTests
-# Runtime stage
-FROM eclipse-temurin:11-jre
+# Copy only pom.xml first to cache dependencies
+COPY pom.xml .
+RUN mvn dependency:go-offline -B
+
+# Now copy source and build (dependencies are cached)
+COPY src ./src
+RUN mvn clean package -DskipTests -o
+
+# Runtime stage - use Alpine JRE (much smaller ~150MB vs ~300MB)
+FROM eclipse-temurin:11-jre-alpine
WORKDIR /app
COPY --from=builder /app/target/task-tracker-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
diff --git a/src/main/java/com/example/tasktracker/Application.java b/src/main/java/com/example/tasktracker/Application.java
index 323a91d..fcfae81 100644
--- a/src/main/java/com/example/tasktracker/Application.java
+++ b/src/main/java/com/example/tasktracker/Application.java
@@ -1,6 +1,7 @@
package com.example.tasktracker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+
@SpringBootApplication
public class Application {
public static void main(String[] args) {
diff --git a/src/main/java/com/example/tasktracker/controller/TaskController.java b/src/main/java/com/example/tasktracker/controller/TaskController.java
index cee37e3..7ba848a 100644
--- a/src/main/java/com/example/tasktracker/controller/TaskController.java
+++ b/src/main/java/com/example/tasktracker/controller/TaskController.java
@@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.CompletableFuture;
@RestController
@RequestMapping("/api/tasks")
@@ -13,7 +14,7 @@ public class TaskController {
private TaskService taskService;
@GetMapping("")
- public List<Task> list() {
+ public CompletableFuture<List<Task>> list() {
return taskService.getAll();
}
@PostMapping("")
@@ -21,8 +22,8 @@ public Task add(@RequestBody Task t) {
return taskService.add(t);
}
@GetMapping("/stats")
- public Map<String, Object> stats() {
- // Synchronously computes stats (inefficient; will be optimized to async)
+ public CompletableFuture<Map<String, Object>> stats() {
+ // Async: request thread is released while stats are being computed
return taskService.calculateStats();
}
}
diff --git a/src/main/java/com/example/tasktracker/service/TaskService.java b/src/main/java/com/example/tasktracker/service/TaskService.java
index a5452b9..06413e6 100644
--- a/src/main/java/com/example/tasktracker/service/TaskService.java
+++ b/src/main/java/com/example/tasktracker/service/TaskService.java
@@ -2,16 +2,34 @@
import org.springframework.stereotype.Service;
import com.example.tasktracker.model.Task;
import java.util.*;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
@Service
public class TaskService {
private List<Task> tasks = new ArrayList<>();
private List<Task> completedTasks = new ArrayList<>();
+ private final ExecutorService executorService = Executors.newCachedThreadPool();
- public List<Task> getAll() {
- return new ArrayList<>(tasks);
+ public CompletableFuture<List<Task>> getAll() {
+ return CompletableFuture.supplyAsync(() -> {
+ return new ArrayList<>(tasks);
+ }, executorService);
}
public Task add(Task t) {
+ // Check if task with this ID already exists
+ if (t.getId() != null) {
+ for (int i = 0; i < tasks.size(); i++) {
+ if (t.getId().equals(tasks.get(i).getId())) {
+ // Update existing task
+ tasks.set(i, t);
+ return t;
+ }
+ }
+ }
+
+ // New task - generate ID and add
t.setId(UUID.randomUUID().toString());
tasks.add(t);
if (t.isCompleted()) {
@@ -20,16 +38,20 @@ public Task add(Task t) {
}
return t;
}
- public Map<String, Object> calculateStats() {
- // Inefficient: calc done synchronously, iterates over entire list
- int total = tasks.size();
- int completed = 0;
- for (Task t : tasks) {
- if (t.isCompleted()) completed++;
- }
- Map<String, Object> res = new HashMap<>();
- res.put("total", total);
- res.put("completed", completed);
- return res;
+
+ public CompletableFuture<Map<String, Object>> calculateStats() {
+ return CompletableFuture.supplyAsync(() -> {
+ // Runs on ExecutorService thread pool, freeing up the request thread
+ int total = tasks.size();
+ int completed = 0;
+ for (Task t : tasks) {
+ if (t.isCompleted()) completed++;
+ }
+
+ Map<String, Object> res = new HashMap<>();
+ res.put("total", total);
+ res.put("completed", completed);
+ return res;
+ }, executorService);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment