Skip to content

Commit

Permalink
Add Close method (#37)
Browse files Browse the repository at this point in the history
* Add Close method

* Bump coverage

* Improve godoc

* Improve godoc

* Improve godoc
  • Loading branch information
erni27 committed Apr 30, 2023
1 parent f714bf9 commit b1f3d7b
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 211 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![GoDoc](https://pkg.go.dev/badge/mod/github.com/erni27/imcache)](https://pkg.go.dev/mod/github.com/erni27/imcache)
[![Coverage Status](https://codecov.io/gh/erni27/imcache/branch/master/graph/badge.svg)](https://codecov.io/gh/erni27/imcache)

`imcache` is a generic in-memory cache Go library.
`imcache` is a zero-dependency generic in-memory cache Go library.

It supports absolute expiration, sliding expiration, max entries limit, eviction callbacks and sharding. It's safe for concurrent use by multiple goroutines.

Expand Down Expand Up @@ -83,13 +83,13 @@ c2.Set(1, "one", imcache.WithDefaultExpiration())

`imcache` follows very naive and simple eviction approach. If an expired entry is accessed by any `Cache` method, it is removed from the cache. The exception is the max entries limit. If the max entries limit is set, the cache evicts the least recently used entry when the max entries limit is reached regardless of its expiration time. More on limiting the max number of entries in the cache can be found in the [Max entries limit](#max-entries-limit) section.

It is possible to use the `Cleaner` to periodically remove expired entries from the cache. The `Cleaner` is a background goroutine that periodically removes expired entries from the cache. The `Cleaner` is disabled by default. You can enable it by calling the `StartCleaner` method. The `Cleaner` can be stopped by calling the `StopCleaner` method.
It is possible to use the `Cleaner` to periodically remove expired entries from the cache. The `Cleaner` is a background goroutine that periodically removes expired entries from the cache. The `Cleaner` is disabled by default. You can enable it when creating a new `Cache` or `Sharded` instance. `Cleaner` is stopped when the cache is closed.

```go
var c imcache.Cache[string, string]
// Start Cleaner which will remove expired entries every 5 minutes.
_ = c.StartCleaner(5 * time.Minute)
defer c.StopCleaner()
// Create a new Cache with a Cleaner which will remove expired entries every 5 minutes.
c := imcache.New[string, string](imcache.WithCleanerOption[string, string](5 * time.Minute))
// Close the Cache. This will stop the Cleaner if it is running.
defer c.Close()
```

To be notified when an entry is evicted from the cache, you can use the `EvictionCallback`. It's a function that accepts the key and value of the evicted entry along with the reason why the entry was evicted. `EvictionCallback` can be configured when creating a new `Cache` or `Sharded` instance.
Expand Down
5 changes: 4 additions & 1 deletion eviction.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,7 @@ func (*nopq[K]) Add(*node[K]) {}

func (*nopq[K]) Remove(*node[K]) {}

func (*nopq[K]) Pop() *node[K] { return nil }
func (*nopq[K]) Pop() *node[K] {
// imcache is carefully designed to never call Pop on a NOP queue.
panic("imcache: Pop called on a NOP queue")
}

0 comments on commit b1f3d7b

Please sign in to comment.