sm
/
cache
1
0
Fork 0

gofmt and redundant 'if it was enabled'

This commit is contained in:
Patrick Mylund Nielsen 2012-01-02 14:11:17 +01:00
parent ac5b195364
commit ca7e0d4f78
2 changed files with 13 additions and 15 deletions

View File

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

View File

@ -33,7 +33,7 @@ func TestCache(t *testing.T) {
}
if x == 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)
}
@ -43,7 +43,7 @@ func TestCache(t *testing.T) {
}
if x == 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)
}
@ -53,7 +53,7 @@ func TestCache(t *testing.T) {
}
if x == 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)
}
}
@ -67,13 +67,13 @@ func TestCacheTimes(t *testing.T) {
tc.Set("c", 3, 20*time.Millisecond)
tc.Set("d", 4, 70*time.Millisecond)
<-time.After(25*time.Millisecond)
<-time.After(25 * time.Millisecond)
_, found = tc.Get("c")
if found {
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")
if found {
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")
}
<-time.After(20*time.Millisecond)
<-time.After(20 * time.Millisecond)
_, found = tc.Get("d")
if found {
t.Error("Found d when it should have been automatically deleted (later than the default)")