mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2025-02-23 07:03:01 +08:00
FEAT: Add ZoneCache primitive (#3365)
This commit is contained in:
parent
556926a2f7
commit
5c9b17039e
1 changed files with 86 additions and 0 deletions
86
pkg/zoneCache/zoneCache.go
Normal file
86
pkg/zoneCache/zoneCache.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package zoneCache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func New[Zone any](fetchAll func() (map[string]Zone, error)) ZoneCache[Zone] {
|
||||
return ZoneCache[Zone]{fetchAll: fetchAll}
|
||||
}
|
||||
|
||||
var ErrZoneNotFound = errors.New("zone not found")
|
||||
|
||||
type ZoneCache[Zone any] struct {
|
||||
mu sync.Mutex
|
||||
cached bool
|
||||
cache map[string]Zone
|
||||
fetchAll func() (map[string]Zone, error)
|
||||
}
|
||||
|
||||
func (c *ZoneCache[Zone]) ensureCached() error {
|
||||
if c.cached {
|
||||
return nil
|
||||
}
|
||||
zones, err := c.fetchAll()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c.cache == nil {
|
||||
c.cache = make(map[string]Zone, len(zones))
|
||||
}
|
||||
for name, z := range zones {
|
||||
c.cache[name] = z
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ZoneCache[Zone]) HasZone(name string) (bool, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if err := c.ensureCached(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
_, ok := c.cache[name]
|
||||
return ok, nil
|
||||
}
|
||||
|
||||
func (c *ZoneCache[Zone]) GetZone(name string) (Zone, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if err := c.ensureCached(); err != nil {
|
||||
var z Zone
|
||||
return z, err
|
||||
}
|
||||
z, ok := c.cache[name]
|
||||
if !ok {
|
||||
return z, ErrZoneNotFound
|
||||
}
|
||||
return z, nil
|
||||
}
|
||||
|
||||
func (c *ZoneCache[Zone]) GetZoneNames() ([]string, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if err := c.ensureCached(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := make([]string, 0, len(c.cache))
|
||||
for name := range c.cache {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func (c *ZoneCache[Zone]) SetZone(name string, z Zone) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if c.cache == nil {
|
||||
c.cache = make(map[string]Zone, 1)
|
||||
}
|
||||
c.cache[name] = z
|
||||
}
|
Loading…
Reference in a new issue