Пример #1
0
func Test_FIFO(t *testing.T) {
	fmt.Println("[Test] FIFO")

	queue := New_FIFO(3)
	queue.Insert(link.New_Node("a", "av", 0))
	queue.Insert(link.New_Node("b", "bv", 0))
	queue.Insert(link.New_Node("c", "cv", 0))
	queue.Insert(link.New_Node("d", "dv", 0))

	if !queue.Equal([]string{"d", "c", "b"}) {
		t.Error("fifo should be d c b ")
	}
}
Пример #2
0
func Test_LRU(t *testing.T) {
	fmt.Println("[Test] LRU")

	queue := New_LRU(3)
	queue.Insert(link.New_Node("a", "av", 0))
	queue.Insert(link.New_Node("b", "bv", 0))
	node3 := queue.Insert(link.New_Node("c", "cv", 0))

	queue.Update(node3)

	if !queue.Equal([]string{"c", "b", "a"}) {
		t.Error("node3 shoud be in the head")
	}

	queue.Insert(link.New_Node("d", "dv", 0))

	if !queue.Equal([]string{"d", "c", "b"}) {
		t.Error("node4 shoud be in the head")
	}
}
Пример #3
0
//insert an item to the cache, replacing any existing item
//If the expire <= 0, the item will never expires
func (cache *Cache) Set(key string, value interface{}, expire time.Duration) {
	cache.Lock()
	defer cache.Unlock()

	nodeMap := cache.nodeMap
	queue := cache.queue
	newNode := link.New_Node(key, value, expire)

	node, ok := nodeMap[key]

	if ok {
		node.Value = newNode.Value
		node.Expire = newNode.Expire
	} else {
		inserNode := queue.Insert(newNode)
		nodeMap[key] = inserNode
	}
}