Skip to content

Commit a62e178

Browse files
committed
Add 1 Hour TTL Cache - Closes #40
1 parent d299925 commit a62e178

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

internal/api/endpoint.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"net/http"
66
"sync"
7+
"time"
78

89
"certui/internal/certificate"
910
"certui/internal/domain"
@@ -16,8 +17,24 @@ type EndpointDetails struct {
1617
SSL *certificate.SSLDetails
1718
}
1819

20+
const endpointCacheTTL = time.Hour
21+
22+
var endpointCache sync.Map // map[domain.Domain]endpointCacheEntry
23+
24+
type endpointCacheEntry struct {
25+
details *EndpointDetails
26+
expiresAt time.Time
27+
}
28+
1929
// fetchEndpointDetails fetches certificate, domain, and WHOIS details concurrently for a single endpoint
2030
func fetchEndpointDetails(client *http.Client, endpoint domain.Domain) *EndpointDetails {
31+
if v, ok := endpointCache.Load(endpoint); ok {
32+
entry := v.(endpointCacheEntry)
33+
if time.Now().Before(entry.expiresAt) {
34+
return entry.details
35+
}
36+
}
37+
2138
var ssl *certificate.SSLDetails
2239
var domainDetails domain.DomainDetails
2340
var whoisDetails *domain.WhoisDetails
@@ -46,5 +63,10 @@ func fetchEndpointDetails(client *http.Client, endpoint domain.Domain) *Endpoint
4663
}()
4764
wg.Wait()
4865

49-
return &EndpointDetails{Domain: domainDetails, Whois: whoisDetails, SSL: ssl}
66+
details := &EndpointDetails{Domain: domainDetails, Whois: whoisDetails, SSL: ssl}
67+
endpointCache.Store(endpoint, endpointCacheEntry{
68+
details: details,
69+
expiresAt: time.Now().Add(endpointCacheTTL),
70+
})
71+
return details
5072
}

0 commit comments

Comments
 (0)