Skip to content

Commit

Permalink
fix 容量超1的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
vividwei committed Nov 15, 2022
1 parent e484282 commit 8475601
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Badge](https://img.shields.io/badge/link-996.icu-%23FF4D5B.svg?style=flat-square)](https://996.icu/#/zh_CN)
[![Go](https://github.com/WeiJiadong/gocache/workflows/Go/badge.svg?branch=master)](https://github.com/WeiJiadong/gocache/actions)
[![Go Report Card](https://img.shields.io/badge/go%20report-A+-brightgreen.svg?style=flat)](https://goreportcard.com/report/github.com/WeiJiadong/gocache)
[![GoDoc](https://godoc.org/github.com/WeiJiadong/gocache?status.svg)](https://pkg.go.dev/github.com/WeiJiadong/[email protected].2)
[![GoDoc](https://godoc.org/github.com/WeiJiadong/gocache?status.svg)](https://pkg.go.dev/github.com/WeiJiadong/[email protected].7)
[![Latest](https://img.shields.io/badge/latest-v1.1.2-blue.svg)](https://github.com/WeiJiadong/gocache/tree/v1.1.2)
[![codecov](https://codecov.io/gh/WeiJiadong/gocache/branch/master/graph/badge.svg?token=6RG0W91RF2)](https://codecov.io/gh/WeiJiadong/gocache)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
Expand Down
8 changes: 4 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ func (c *Cache) Set(key, val interface{}) error {
oldVal.Value.(*elem).expire = time.Now().Add(c.opts.expire).UnixNano()
return nil
}
// 2 key不存在,则直接在最前面插入一个值,并更新map
c.data[key] = c.lru.PushFront(&elem{key: key, value: val, expire: time.Now().Add(c.opts.expire).UnixNano()})
// 3 判断是否到达容量限制,若达到限制,则进行LRU剔除
if c.opts.keyCnt > 0 && c.lru.Len() > c.opts.keyCnt {
// 2 先判断是否到达容量限制,若达到限制,则进行LRU剔除
if c.opts.keyCnt > 0 && c.lru.Len() >= c.opts.keyCnt {
if lruVal := c.lru.Back(); lruVal != nil {
c.lru.Remove(lruVal)
v := lruVal.Value.(*elem)
delete(c.data, v.key)
}
}
// 3 key不存在,则直接在最前面插入一个值,并更新map
c.data[key] = c.lru.PushFront(&elem{key: key, value: val, expire: time.Now().Add(c.opts.expire).UnixNano()})
return nil
}

Expand Down

0 comments on commit 8475601

Please sign in to comment.