07-06-Melvin-Gorutine/enrollment.go
2025-10-01 06:42:15 -04:00

34 lines
563 B
Go

package main
import (
"fmt"
"sync"
"time"
)
var (
IdempotencyStore = make(map[string]string)
mu sync.Mutex
)
func EnrollCourseIdempotent(key string) string {
mu.Lock()
if val, ok := IdempotencyStore[key]; ok {
mu.Unlock()
return fmt.Sprintf("Hasil cache: %s", val)
}
mu.Unlock()
// proses baru
fmt.Println("Memproses enrollment baru...")
time.Sleep(1500 * time.Millisecond)
result := "Enrollment berhasil untuk " + key
mu.Lock()
IdempotencyStore[key] = result
mu.Unlock()
return result
}