github.com/vicanso/lru-ttl@v1.5.1/README.md (about)

     1  # lru-ttl
     2  
     3  [![Build Status](https://github.com/vicanso/lru-ttl/workflows/Test/badge.svg)](https://github.com/vicanso/lru-ttl/actions)
     4  
     5  LRU cache with ttl. It's useful for short ttl cache. 
     6  L2Cache use lru cache as first cache, and slow cache as second cache. Lru cache should be set max entries for less memory usage but faster, slow cache is slower but larger capacity.
     7  
     8  ## LRU TTL
     9  
    10  
    11  ```go
    12  cache := lruttl.New(1000, 60 * time.Second)
    13  cache.Add("tree.xie", "my data")
    14  data, ok := cache.Get("tree.xie")
    15  cache.Remove("tree.xie")
    16  cache.Add("tree.xie", "my data", time.Second)
    17  ```
    18  
    19  ## L2Cache
    20  
    21  ```go
    22  // redisCache
    23  l2 := lruttl.NewL2Cache(redisCache, 200, 10 * time.Minute)
    24  ctx := context.Background()
    25  err := l2.Set(ctx, "key", &map[string]string{
    26      "name": "test",
    27  })
    28  fmt.Println(err)
    29  m := make(map[string]string)
    30  err = l2.Get(ctx, "key", &m)
    31  fmt.Println(err)
    32  fmt.Println(m)
    33  ```
    34  
    35  ## Ring
    36  
    37  ```go
    38  ringCache := lruttl.NewRing(lruttl.RingCacheParams{
    39      Size:       10,
    40      MaxEntries: 1000,
    41      DefaultTTL: time.Minute,
    42  })
    43  lruCache := ringCache("key")
    44  ```