gofmt and redundant 'if it was enabled'

master
Patrick Mylund Nielsen 13 years ago
parent ac5b195364
commit ca7e0d4f78

@ -126,8 +126,8 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
e = &t e = &t
} }
c.Items[k] = &Item{ c.Items[k] = &Item{
Object: x, Object: x,
Expires: expires, Expires: expires,
Expiration: e, Expiration: e,
} }
} }
@ -235,7 +235,6 @@ func (c *cache) Decrement(k string, n int64) error {
return c.Increment(k, n*-1) return c.Increment(k, n*-1)
} }
// Deletes an item from the cache. Does nothing if the item does not exist in the cache. // Deletes an item from the cache. Does nothing if the item does not exist in the cache.
func (c *cache) Delete(k string) { func (c *cache) Delete(k string) {
c.mu.Lock() c.mu.Lock()
@ -272,7 +271,6 @@ func (i *Item) Expired() bool {
return i.Expiration.Before(time.Now()) return i.Expiration.Before(time.Now())
} }
func (j *janitor) Run(c *cache) { func (j *janitor) Run(c *cache) {
j.stop = make(chan bool) j.stop = make(chan bool)
tick := time.Tick(j.Interval) tick := time.Tick(j.Interval)
@ -305,8 +303,8 @@ func New(de, ci time.Duration) *Cache {
} }
c := &cache{ c := &cache{
DefaultExpiration: de, DefaultExpiration: de,
Items: map[string]*Item{}, Items: map[string]*Item{},
mu: &sync.Mutex{}, mu: &sync.Mutex{},
} }
if ci > 0 { if ci > 0 {
j := &janitor{ j := &janitor{
@ -316,9 +314,9 @@ func New(de, ci time.Duration) *Cache {
go j.Run(c) go j.Run(c)
} }
// This trick ensures that the janitor goroutine (which--granted it was enabled--is // This trick ensures that the janitor goroutine (which--granted it was enabled--is
// running DeleteExpired on c forever, if it was enabled) does not keep the returned C // running DeleteExpired on c forever) does not keep the returned C object from being
// object from being garbage collected. When it is garbage collected, the finalizer stops // garbage collected. When it is garbage collected, the finalizer stops the janitor
// the janitor goroutine, after which c is collected. // goroutine, after which c is collected.
C := &Cache{c} C := &Cache{c}
if ci > 0 { if ci > 0 {
runtime.SetFinalizer(C, stopJanitor) runtime.SetFinalizer(C, stopJanitor)

@ -33,7 +33,7 @@ func TestCache(t *testing.T) {
} }
if x == nil { if x == nil {
t.Error("x for a is nil") t.Error("x for a is nil")
} else if a2 := x.(int); a2 + 2 != 3 { } else if a2 := x.(int); a2+2 != 3 {
t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2) t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2)
} }
@ -43,7 +43,7 @@ func TestCache(t *testing.T) {
} }
if x == nil { if x == nil {
t.Error("x for b is nil") t.Error("x for b is nil")
} else if b2 := x.(string); b2 + "B" != "bB" { } else if b2 := x.(string); b2+"B" != "bB" {
t.Error("b2 (which should be b) plus B does not equal bB; value:", b2) t.Error("b2 (which should be b) plus B does not equal bB; value:", b2)
} }
@ -53,7 +53,7 @@ func TestCache(t *testing.T) {
} }
if x == nil { if x == nil {
t.Error("x for c is nil") t.Error("x for c is nil")
} else if c2 := x.(float64); c2 + 1.2 != 4.7 { } else if c2 := x.(float64); c2+1.2 != 4.7 {
t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2) t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2)
} }
} }
@ -67,13 +67,13 @@ func TestCacheTimes(t *testing.T) {
tc.Set("c", 3, 20*time.Millisecond) tc.Set("c", 3, 20*time.Millisecond)
tc.Set("d", 4, 70*time.Millisecond) tc.Set("d", 4, 70*time.Millisecond)
<-time.After(25*time.Millisecond) <-time.After(25 * time.Millisecond)
_, found = tc.Get("c") _, found = tc.Get("c")
if found { if found {
t.Error("Found c when it should have been automatically deleted") t.Error("Found c when it should have been automatically deleted")
} }
<-time.After(30*time.Millisecond) <-time.After(30 * time.Millisecond)
_, found = tc.Get("a") _, found = tc.Get("a")
if found { if found {
t.Error("Found a when it should have been automatically deleted") t.Error("Found a when it should have been automatically deleted")
@ -89,7 +89,7 @@ func TestCacheTimes(t *testing.T) {
t.Error("Did not find d even though it was set to expire later than the default") t.Error("Did not find d even though it was set to expire later than the default")
} }
<-time.After(20*time.Millisecond) <-time.After(20 * time.Millisecond)
_, found = tc.Get("d") _, found = tc.Get("d")
if found { if found {
t.Error("Found d when it should have been automatically deleted (later than the default)") t.Error("Found d when it should have been automatically deleted (later than the default)")

Loading…
Cancel
Save