This commit is contained in:
Jianglong 2019-12-02 11:27:33 -08:00 committed by GitHub
commit f15cd1b10b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 3 deletions

View File

@ -15,14 +15,14 @@ one) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats
### Installation
`go get github.com/patrickmn/go-cache`
`go get github.com/youjianglong/go-cache`
### Usage
```go
import (
"fmt"
"github.com/patrickmn/go-cache"
"github.com/youjianglong/go-cache"
"time"
)
@ -80,4 +80,4 @@ func main() {
### Reference
`godoc` or [http://godoc.org/github.com/patrickmn/go-cache](http://godoc.org/github.com/patrickmn/go-cache)
`godoc` or [http://godoc.org/github.com/youjianglong/go-cache](http://godoc.org/github.com/youjianglong/go-cache)

View File

@ -165,6 +165,37 @@ func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) {
return item.Object, time.Time{}, true
}
// Iterate every item by item handle items from cache,and if the handle returns to false,
// it will be interrupted and return false.
func (c *cache) Iterate(f func(key string, item Item) bool) bool {
now := time.Now().UnixNano()
c.mu.RLock()
keys := make([]string, len(c.items))
i := 0
for k, v := range c.items {
// "Inlining" of Expired
if v.Expiration > 0 && now > v.Expiration {
continue
}
keys[i] = k
i++
}
c.mu.RUnlock()
keys = keys[:i]
for _, key := range keys {
c.mu.RLock()
item, ok := c.items[key]
c.mu.RUnlock()
if !ok {
continue
}
if !f(key, item) {
return false
}
}
return true
}
func (c *cache) get(k string) (interface{}, bool) {
item, found := c.items[k]
if !found {

1
go.mod Normal file
View File

@ -0,0 +1 @@
module github.com/youjianglong/go-cache