Skip to content

Commit

Permalink
#42 fixing item being expired forever
Browse files Browse the repository at this point in the history
  • Loading branch information
mageddo committed Oct 14, 2017
1 parent 4b0c735 commit fdb5801
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Cache interface {
ContainsKey(key interface{}) bool
Put(key, value interface{})
PutIfAbsent(key, value interface{}) interface{}
Remove(key interface{})
Clear()

}
4 changes: 4 additions & 0 deletions cache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions cache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

}
6 changes: 5 additions & 1 deletion proxy/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

2 changes: 1 addition & 1 deletion proxy/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit fdb5801

Please sign in to comment.