Browse Source

fix cache cleaner event cnannel

0x4a52466c696e74 2 years ago
parent
commit
829b699e96
2 changed files with 23 additions and 2 deletions
  1. 4 1
      cache/cachemap.go
  2. 19 1
      cache/z_test.go

+ 4 - 1
cache/cachemap.go

@@ -67,7 +67,10 @@ func (s *cacheMap) runCleaner() {
 					}
 				}
 				if len(cleaned) > 0 {
-					s.cleanedEventChan <- cleaned
+					select {
+					case s.cleanedEventChan <- cleaned:
+					default:
+					}
 				}
 				if len(s.items) == 0 {
 					s.cleanerWork = false

+ 19 - 1
cache/z_test.go

@@ -30,7 +30,7 @@ func TestMap(t *testing.T) {
 }
 
 func TestMapCheck(t *testing.T) {
-	m := NewMap(time.Hour, 0)
+	m := NewMap(time.Second*20, 0)
 
 	m.Set("key", 10)
 
@@ -48,6 +48,24 @@ func TestMapCheck(t *testing.T) {
 	log.Println(m.GetCreateNew("key", func(key interface{}) (rKey, val interface{}, created bool) {
 		return "oooone", 1000, true
 	}))
+
+	/*
+		// test cleaner
+			for {
+				log.Println("enter")
+				m.GetCheck("key", func(key, value interface{}, exists bool) (rKey, rVal interface{}, needUpdate bool) {
+					log.Println("Program", key, value, exists)
+					if exists {
+						needUpdate = false
+					}
+					rKey = key
+					rVal = 100
+					needUpdate = true
+					return
+				})
+				time.Sleep(time.Second * 10)
+			}
+	*/
 }
 
 func TestMapEach(t *testing.T) {