@@ -12,19 +12,26 @@ import (
1212 "time"
1313)
1414
15+ func (c * Client ) hasPersistableLocalDNSCache () bool {
16+ return c != nil &&
17+ c .localDNSCache != nil &&
18+ c .localDNSCachePersist &&
19+ c .localDNSCachePath != ""
20+ }
21+
1522func (c * Client ) persistResolvedLocalDNSCacheEntry (cacheKey []byte , domain string , qType uint16 , qClass uint16 , rawResponse []byte , now time.Time ) {
1623 if c == nil || c .localDNSCache == nil {
1724 return
1825 }
1926
2027 c .localDNSCache .SetReady (cacheKey , domain , qType , qClass , rawResponse , now )
21- if c .cfg . LocalDNSCachePersist {
28+ if c .hasPersistableLocalDNSCache () {
2229 c .flushLocalDNSCache ()
2330 }
2431}
2532
2633func (c * Client ) ensureLocalDNSCacheLoaded () {
27- if c == nil {
34+ if ! c . hasPersistableLocalDNSCache () {
2835 return
2936 }
3037 c .localDNSCacheLoadOnce .Do (func () {
@@ -33,7 +40,7 @@ func (c *Client) ensureLocalDNSCacheLoaded() {
3340}
3441
3542func (c * Client ) ensureLocalDNSCachePersistence (ctx context.Context ) {
36- if c == nil {
43+ if ! c . hasPersistableLocalDNSCache () {
3744 return
3845 }
3946 c .ensureLocalDNSCacheLoaded ()
@@ -43,11 +50,11 @@ func (c *Client) ensureLocalDNSCachePersistence(ctx context.Context) {
4350}
4451
4552func (c * Client ) loadLocalDNSCache () {
46- if c == nil || ! c .cfg . LocalDNSCachePersist || c . localDNSCache == nil {
53+ if ! c .hasPersistableLocalDNSCache () {
4754 return
4855 }
4956
50- loaded , err := c .localDNSCache .LoadFromFile (c .cfg . LocalDNSCachePath () , c .now ())
57+ loaded , err := c .localDNSCache .LoadFromFile (c .localDNSCachePath , c .now ())
5158 if err != nil {
5259 if c .log != nil {
5360 c .log .Warnf ("\U0001F4BE <cyan>Local DNS Cache</cyan> <magenta>|</magenta> <red>Load Failed</red> <magenta>|</magenta> <yellow>%v</yellow>" , err )
@@ -60,11 +67,11 @@ func (c *Client) loadLocalDNSCache() {
6067}
6168
6269func (c * Client ) flushLocalDNSCache () {
63- if c == nil || ! c .cfg . LocalDNSCachePersist || c . localDNSCache == nil {
70+ if ! c .hasPersistableLocalDNSCache () {
6471 return
6572 }
6673
67- saved , err := c .localDNSCache .SaveToFile (c .cfg . LocalDNSCachePath () , c .now ())
74+ saved , err := c .localDNSCache .SaveToFile (c .localDNSCachePath , c .now ())
6875 if err != nil {
6976 if c .log != nil {
7077 c .log .Warnf ("\U0001F4BE <cyan>Local DNS Cache</cyan> <magenta>|</magenta> <red>Flush Failed</red> <magenta>|</magenta> <yellow>%v</yellow>" , err )
@@ -77,16 +84,11 @@ func (c *Client) flushLocalDNSCache() {
7784}
7885
7986func (c * Client ) runLocalDNSCacheFlushLoop (ctx context.Context ) {
80- if c == nil || ! c .cfg . LocalDNSCachePersist || c . localDNSCache == nil {
87+ if ! c .hasPersistableLocalDNSCache () {
8188 return
8289 }
8390
84- interval := time .Duration (c .cfg .LocalDNSCacheFlushSec * float64 (time .Second ))
85- if interval <= 0 {
86- interval = time .Minute
87- }
88-
89- ticker := time .NewTicker (interval )
91+ ticker := time .NewTicker (c .localDNSCacheFlushTick )
9092 defer ticker .Stop ()
9193 defer c .flushLocalDNSCache ()
9294
0 commit comments