Skip to content

Commit

Permalink
Chore: remove old cache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Aug 17, 2022
1 parent 3946d77 commit 6e058f8
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 205 deletions.
106 changes: 0 additions & 106 deletions common/cache/cache.go

This file was deleted.

70 changes: 0 additions & 70 deletions common/cache/cache_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions common/cache/lrucache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type LruCache struct {
onEvict EvictCallback
}

// NewLRUCache creates an LruCache
func NewLRUCache(options ...Option) *LruCache {
// New creates an LruCache
func New(options ...Option) *LruCache {
lc := &LruCache{
lru: list.New(),
cache: make(map[any]*list.Element),
Expand Down
20 changes: 10 additions & 10 deletions common/cache/lrucache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var entries = []struct {
}

func TestLRUCache(t *testing.T) {
c := NewLRUCache()
c := New()

for _, e := range entries {
c.Set(e.key, e.value)
Expand All @@ -45,7 +45,7 @@ func TestLRUCache(t *testing.T) {
}

func TestLRUMaxAge(t *testing.T) {
c := NewLRUCache(WithAge(86400))
c := New(WithAge(86400))

now := time.Now().Unix()
expected := now + 86400
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestLRUMaxAge(t *testing.T) {
}

func TestLRUpdateOnGet(t *testing.T) {
c := NewLRUCache(WithAge(86400), WithUpdateAgeOnGet())
c := New(WithAge(86400), WithUpdateAgeOnGet())

now := time.Now().Unix()
expires := now + 86400/2
Expand All @@ -103,7 +103,7 @@ func TestLRUpdateOnGet(t *testing.T) {
}

func TestMaxSize(t *testing.T) {
c := NewLRUCache(WithSize(2))
c := New(WithSize(2))
// Add one expired entry
c.Set("foo", "bar")
_, ok := c.Get("foo")
Expand All @@ -117,7 +117,7 @@ func TestMaxSize(t *testing.T) {
}

func TestExist(t *testing.T) {
c := NewLRUCache(WithSize(1))
c := New(WithSize(1))
c.Set(1, 2)
assert.True(t, c.Exist(1))
c.Set(2, 3)
Expand All @@ -130,15 +130,15 @@ func TestEvict(t *testing.T) {
temp = key.(int) + value.(int)
}

c := NewLRUCache(WithEvict(evict), WithSize(1))
c := New(WithEvict(evict), WithSize(1))
c.Set(1, 2)
c.Set(2, 3)

assert.Equal(t, temp, 3)
}

func TestSetWithExpire(t *testing.T) {
c := NewLRUCache(WithAge(1))
c := New(WithAge(1))
now := time.Now().Unix()

tenSecBefore := time.Unix(now-10, 0)
Expand All @@ -152,7 +152,7 @@ func TestSetWithExpire(t *testing.T) {
}

func TestStale(t *testing.T) {
c := NewLRUCache(WithAge(1), WithStale(true))
c := New(WithAge(1), WithStale(true))
now := time.Now().Unix()

tenSecBefore := time.Unix(now-10, 0)
Expand All @@ -165,11 +165,11 @@ func TestStale(t *testing.T) {
}

func TestCloneTo(t *testing.T) {
o := NewLRUCache(WithSize(10))
o := New(WithSize(10))
o.Set("1", 1)
o.Set("2", 2)

n := NewLRUCache(WithSize(2))
n := New(WithSize(2))
n.Set("3", 3)
n.Set("4", 4)

Expand Down
2 changes: 1 addition & 1 deletion component/fakeip/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func New(options Options) (*Pool, error) {
}
} else {
pool.store = &memoryStore{
cache: cache.NewLRUCache(cache.WithSize(options.Size * 2)),
cache: cache.New(cache.WithSize(options.Size * 2)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion dns/enhancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewEnhancer(cfg Config) *ResolverEnhancer {

if cfg.EnhancedMode != C.DNSNormal {
fakePool = cfg.Pool
mapping = cache.NewLRUCache(cache.WithSize(4096), cache.WithStale(true))
mapping = cache.New(cache.WithSize(4096), cache.WithStale(true))
}

return &ResolverEnhancer{
Expand Down
4 changes: 2 additions & 2 deletions dns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,13 @@ type Config struct {
func NewResolver(config Config) *Resolver {
defaultResolver := &Resolver{
main: transform(config.Default, nil),
lruCache: cache.NewLRUCache(cache.WithSize(4096), cache.WithStale(true)),
lruCache: cache.New(cache.WithSize(4096), cache.WithStale(true)),
}

r := &Resolver{
ipv6: config.IPv6,
main: transform(config.Main, defaultResolver),
lruCache: cache.NewLRUCache(cache.WithSize(4096), cache.WithStale(true)),
lruCache: cache.New(cache.WithSize(4096), cache.WithStale(true)),
hosts: config.Hosts,
}

Expand Down
11 changes: 5 additions & 6 deletions listener/http/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"net/http"
"strings"
"time"

"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/common/cache"
Expand All @@ -15,7 +14,7 @@ import (
"github.com/Dreamacro/clash/log"
)

func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache) {
client := newClient(c.RemoteAddr(), in)
defer client.CloseIdleConnections()

Expand Down Expand Up @@ -99,7 +98,7 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
conn.Close()
}

func authenticate(request *http.Request, cache *cache.Cache) *http.Response {
func authenticate(request *http.Request, cache *cache.LruCache) *http.Response {
authenticator := authStore.Authenticator()
if authenticator != nil {
credential := parseBasicProxyAuthorization(request)
Expand All @@ -109,11 +108,11 @@ func authenticate(request *http.Request, cache *cache.Cache) *http.Response {
return resp
}

var authed any
if authed = cache.Get(credential); authed == nil {
authed, exist := cache.Get(credential)
if !exist {
user, pass, err := decodeBasicProxyAuthorization(credential)
authed = err == nil && authenticator.Verify(user, pass)
cache.Put(credential, authed, time.Minute)
cache.Set(credential, authed)
}
if !authed.(bool) {
log.Infoln("Auth failed from %s", request.RemoteAddr)
Expand Down
5 changes: 2 additions & 3 deletions listener/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package http

import (
"net"
"time"

"github.com/Dreamacro/clash/common/cache"
C "github.com/Dreamacro/clash/constant"
Expand Down Expand Up @@ -40,9 +39,9 @@ func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool
return nil, err
}

var c *cache.Cache
var c *cache.LruCache
if authenticate {
c = cache.New(time.Second * 30)
c = cache.New(cache.WithAge(30))
}

hl := &Listener{
Expand Down
Loading

0 comments on commit 6e058f8

Please sign in to comment.