Skip to content

Commit

Permalink
refactor(cache): rename package name & refactor get method of ttl map
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf-joe committed Mar 13, 2020
1 parent 2cf81f9 commit 37f4e95
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
3 changes: 2 additions & 1 deletion TSDNS/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"github.com/wolf-joe/ts-dns/GFWList"
"github.com/wolf-joe/ts-dns/Hosts"
ipset "github.com/wolf-joe/ts-dns/IPSet"
"github.com/wolf-joe/ts-dns/cache"
"strings"
)

type Config struct {
Cache *DNSCache
Cache *cache.DNSCache
Listen string
GFWChecker *GFWList.DomainChecker
HostsReaders []Hosts.Reader
Expand Down
11 changes: 5 additions & 6 deletions TSDNS/cache.go → cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package TSDNS
package cache

import (
"fmt"
"github.com/miekg/dns"
"github.com/wolf-joe/ts-dns/TTLMap"
"strconv"
"time"
)
Expand All @@ -26,7 +25,7 @@ func getSubnet(extra []dns.RR) string {
}

type DNSCache struct {
ttlMap *TTLMap.TTLMap
ttlMap *TTLMap
size int
minTTL time.Duration
maxTTL time.Duration
Expand Down Expand Up @@ -65,8 +64,8 @@ func (cache *DNSCache) Set(request *dns.Msg, r *dns.Msg) {
cache.ttlMap.Set(cacheKey, r, ex)
}

func NewDNSCache(size int, minTTL, maxTTL time.Duration) (cache *DNSCache) {
cache = &DNSCache{size: size, minTTL: minTTL, maxTTL: maxTTL}
cache.ttlMap = TTLMap.NewMap(time.Minute)
func NewDNSCache(size int, minTTL, maxTTL time.Duration) (c *DNSCache) {
c = &DNSCache{size: size, minTTL: minTTL, maxTTL: maxTTL}
c.ttlMap = NewTTLMap(time.Minute)
return
}
2 changes: 1 addition & 1 deletion TSDNS/cache_test.go → cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TSDNS
package cache

import (
"github.com/miekg/dns"
Expand Down
34 changes: 19 additions & 15 deletions TTLMap/ttlmap.go → cache/ttlmap.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TTLMap
package cache

import (
"sync"
Expand All @@ -19,30 +19,34 @@ type TTLMap struct {
mux *sync.RWMutex
}

func (ttlMap *TTLMap) Set(key string, value interface{}, ex time.Duration) {
ttlMap.mux.Lock()
defer ttlMap.mux.Unlock()
ttlMap.itemMap[key] = &item{value: value, expire: time.Now().Add(ex).UnixNano()}
func (m *TTLMap) Set(key string, value interface{}, ex time.Duration) {
m.mux.Lock()
defer m.mux.Unlock()
m.itemMap[key] = &item{value: value, expire: time.Now().Add(ex).UnixNano()}
}

func (ttlMap *TTLMap) Get(key string) (interface{}, bool) {
ttlMap.mux.Lock()
defer ttlMap.mux.Unlock()
value, ok := ttlMap.itemMap[key]
func (m *TTLMap) Get(key string) (interface{}, bool) {
// get item, using read lock
m.mux.RLock()
value, ok := m.itemMap[key]
m.mux.RUnlock()
if !ok || time.Now().UnixNano() >= value.expire {
delete(ttlMap.itemMap, key)
// delete item, use write lock
m.mux.Lock()
delete(m.itemMap, key)
m.mux.Unlock()
return nil, false
}
return value.value, true
}

func (ttlMap TTLMap) Len() int {
ttlMap.mux.RLock()
defer ttlMap.mux.RUnlock()
return len(ttlMap.itemMap)
func (m TTLMap) Len() int {
m.mux.RLock()
defer m.mux.RUnlock()
return len(m.itemMap)
}

func NewMap(cleanTick time.Duration) (m *TTLMap) {
func NewTTLMap(cleanTick time.Duration) (m *TTLMap) {
if cleanTick < MinCleanTick {
cleanTick = MinCleanTick
}
Expand Down
4 changes: 2 additions & 2 deletions TTLMap/ttlmap_test.go → cache/ttlmap_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TTLMap
package cache

import (
"github.com/stretchr/testify/assert"
Expand All @@ -7,7 +7,7 @@ import (
)

func TestNewTTLMap(t *testing.T) {
ttlMap := NewMap(time.Millisecond * 500)
ttlMap := NewTTLMap(time.Millisecond * 500)
ttlMap.Set("key1", "value1", time.Millisecond*500)
ttlMap.Set("key2", "value2", time.Millisecond*500)
val, ok := ttlMap.Get("key1")
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/wolf-joe/ts-dns/Hosts"
ipset "github.com/wolf-joe/ts-dns/IPSet"
"github.com/wolf-joe/ts-dns/TSDNS"
"github.com/wolf-joe/ts-dns/cache"
"golang.org/x/net/proxy"
"log"
"os"
Expand Down Expand Up @@ -166,7 +167,7 @@ func initConfig() (config *TSDNS.Config) {
if maxTTL < minTTL {
maxTTL = minTTL
}
config.Cache = TSDNS.NewDNSCache(cacheSize, minTTL, maxTTL)
config.Cache = cache.NewDNSCache(cacheSize, minTTL, maxTTL)
// 检测配置有效性
if len(config.GroupMap) <= 0 || len(config.GroupMap["clean"].Callers) <= 0 || len(config.GroupMap["dirty"].Callers) <= 0 {
log.Fatalln("[CRITICAL] DNS of clean/dirty group cannot be empty")
Expand Down

0 comments on commit 37f4e95

Please sign in to comment.