diff --git a/cache/cache.go b/cache/cache.go index cf523f231..3fb7cd695 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -6,6 +6,7 @@ type Cache interface { ContainsKey(key interface{}) bool Put(key, value interface{}) PutIfAbsent(key, value interface{}) interface{} + Remove(key interface{}) Clear() } diff --git a/cache/lru/lru.go b/cache/lru/lru.go index d0c27ee65..e5bb052f3 100644 --- a/cache/lru/lru.go +++ b/cache/lru/lru.go @@ -41,6 +41,10 @@ func (c *LRUCache) Clear() { c.Cache.Purge() } +func (c *LRUCache) Remove(key interface{}) { + c.Cache.Remove(key) +} + // // Creates a LRU cache // size is the maximum size of the cache, -1 if it is unlimited diff --git a/cache/lru/lru_test.go b/cache/lru/lru_test.go index d37cc0935..6ed351bad 100644 --- a/cache/lru/lru_test.go +++ b/cache/lru/lru_test.go @@ -66,3 +66,20 @@ func TestPurge(t *testing.T){ assert.False(t, cache.ContainsKey("key2")) } + +func TestRemove(t *testing.T){ + + cache := New(3); + + cache.Put("key1", "value1"); + cache.Put("key2", "value2"); + + assert.Equal(t, "value1", cache.Get("key1")) + assert.Equal(t, "value2", cache.Get("key2")) + + cache.Remove("key2") + + assert.True(t, cache.ContainsKey("key1")) + assert.False(t, cache.ContainsKey("key2")) + +} diff --git a/proxy/local.go b/proxy/local.go index 0821b19cf..3224aa559 100644 --- a/proxy/local.go +++ b/proxy/local.go @@ -40,7 +40,7 @@ func (s localDnsSolver) Solve(ctx context.Context, question dns.Question) (*dns. hostname,_ = activeEnv.GetHostname(key) if hostname != nil { ttl = int64(hostname.Ttl) } val := s.Cache.PutIfAbsent(key, timed.NewTimedValue(hostname, time.Now(), time.Duration(ttl) * time.Second)); - LOGGER.Debugf("status=put, key=%s, value=%v", key, val) + LOGGER.Debugf("status=put, key=%s, value=%v, ttl=%d", key, val, ttl) } if hostname != nil { @@ -63,11 +63,15 @@ func NewLocalDNSSolver(c cache.Cache) *localDnsSolver { func (s localDnsSolver) ContainsKey(key interface{}) (interface{}, bool) { if !s.Cache.ContainsKey(key) { + LOGGER.Debugf("status=notfound, key=%v", key) return nil, false } if v := s.Cache.Get(key).(timed.TimedValue); v.IsValid(time.Now()) { + LOGGER.Debugf("status=fromcache, key=%v", key) return v.Value(), true } + LOGGER.Debugf("status=expired, key=%v", key) + s.Cache.Remove(key) return nil, false; } diff --git a/proxy/local_test.go b/proxy/local_test.go index 7b12f97fc..e0634d5b7 100644 --- a/proxy/local_test.go +++ b/proxy/local_test.go @@ -146,7 +146,7 @@ func TestLocalDnsSolver_Solve_CacheExpiration(t *testing.T) { solver := NewLocalDNSSolver(mockCache) // we ask for the same host 5 times but it must load from file just once - for i:=3; i > 0; i-- { + for i:=4; i > 0; i-- { time.Sleep(time.Duration(int64(1100)) * time.Millisecond)