Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Iterate #78

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seriously?!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't notice this. I'll submit it.


### Usage

```go
import (
"fmt"
"github.com/patrickmn/go-cache"
"github.com/youjianglong/go-cache"
"time"
)

Expand Down Expand Up @@ -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)
31 changes: 31 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
tc := New(exp, 0)
keys := make([]string, n)
for i := 0; i < n; i++ {
k := "foo" + strconv.Itoa(n)
k := "foo" + strconv.Itoa(i)
keys[i] = k
tc.Set(k, "bar", DefaultExpiration)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module github.com/youjianglong/go-cache
2 changes: 1 addition & 1 deletion sharded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
tsc := unexportedNewSharded(exp, 0, 20)
keys := make([]string, n)
for i := 0; i < n; i++ {
k := "foo" + strconv.Itoa(n)
k := "foo" + strconv.Itoa(i)
keys[i] = k
tsc.Set(k, "bar", DefaultExpiration)
}
Expand Down