Files
FireflyGo_Proxy/cache.go
T
2026-06-22 11:23:22 +07:00

37 lines
625 B
Go

package main
import (
"crypto/tls"
"sync"
)
type CertStorage struct {
certs map[string]*tls.Certificate
mtx sync.RWMutex
}
func (cs *CertStorage) Fetch(hostname string, gen func() (*tls.Certificate, error)) (*tls.Certificate, error) {
cs.mtx.RLock()
cert, ok := cs.certs[hostname]
cs.mtx.RUnlock()
if ok {
return cert, nil
}
cert, err := gen()
if err != nil {
return nil, err
}
cs.mtx.Lock()
cs.certs[hostname] = cert
cs.mtx.Unlock()
return cert, nil
}
func NewCertStorage() *CertStorage {
return &CertStorage{
certs: make(map[string]*tls.Certificate),
}
}