github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/commons/caches/lru/lru_test.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package lru_test
    19  
    20  import (
    21  	"github.com/aacfactory/fns/commons/caches/lru"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func TestNew(t *testing.T) {
    27  	cache := lru.NewWithExpire[int, int](5, 10*time.Millisecond, func(key int, value int) {
    28  		t.Log("evict:", key, value)
    29  	})
    30  	cache.Add(1, 1)
    31  	cache.Add(2, 2)
    32  	t.Log("len:", cache.Len())
    33  	t.Log(cache.Get(1))
    34  	t.Log(cache.Get(2))
    35  	cache.Remove(2)
    36  	t.Log(cache.Get(2))
    37  	time.Sleep(1 * time.Second)
    38  	t.Log(cache.Get(1))
    39  }
    40  
    41  func TestNewWithNoExpire(t *testing.T) {
    42  	cache := lru.New[int, int](5, func(key int, value int) {
    43  		t.Log("evict:", key, value)
    44  	})
    45  	cache.Add(1, 1)
    46  	cache.Add(2, 2)
    47  	t.Log("len:", cache.Len())
    48  	t.Log(cache.Get(1))
    49  	t.Log(cache.Get(2))
    50  	cache.Remove(2)
    51  	t.Log(cache.Get(2))
    52  	time.Sleep(1 * time.Second)
    53  	t.Log(cache.Get(1))
    54  }