Skip to content

Commit

Permalink
Export concrete types (#22)
Browse files Browse the repository at this point in the history
* Export concrete types

* Add zero value Cache test
  • Loading branch information
erni27 committed Apr 12, 2023
1 parent c109d14 commit 3ae5464
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 235 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (
)

func main() {
// Create a new Cache instance with default configuration.
c := imcache.New[int32, string]()
// Zero value Cache is a valid non-sharded cache
// with no expiration, no sliding expiration and no eviction callback.
var c imcache.Cache[uint32, string]
// Set a new value with no expiration time.
c.Set(1, "one", imcache.WithNoExpiration())
// Get the value for the key 1.
Expand Down Expand Up @@ -55,12 +56,12 @@ c.Set(3, "three", imcache.WithExpiration(time.Second))
If you want to use default expiration time for the given cache instance, you can use the `WithDefaultExpiration` `Expiration` option. By default the default expiration time is set to no expiration. You can set the default expiration time when creating a new `Cache` instance.

```go
// Create a new Cache instance with default absolute expiration time equal to 1 second.
// Create a new non-sharded cache instance with default absolute expiration time equal to 1 second.
c1 := imcache.New(imcache.WithDefaultExpirationOption[int32, string](time.Second))
// Set a new value with default expiration time (absolute).
c1.Set(1, "one", imcache.WithDefaultExpiration())

// Create a new Cache instance with default sliding expiration time equal to 1 second.
// Create a new non-sharded cache instance with default sliding expiration time equal to 1 second.
c2 := imcache.New(imcache.WithDefaultSlidingExpirationOption[int32, string](time.Second))
// Set a new value with default expiration time (sliding).
c2.Set(1, "one", imcache.WithDefaultExpiration())
Expand All @@ -73,13 +74,13 @@ c2.Set(1, "one", imcache.WithDefaultExpiration())
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.

```go
c := imcache.New[string, string]()
var c imcache.Cache[string, string]
// Start Cleaner which will remove expired entries every 5 minutes.
_ = c.StartCleaner(5 * time.Minute)
defer c.StopCleaner()
```

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` instance.
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.

```go
package main
Expand Down Expand Up @@ -113,14 +114,14 @@ func main() {

### Sharding

`imcache` supports sharding. It's possible to create a new `Cache` instance with a given number of shards. Each shard is a separate `Cache` instance. A shard for a given key is selected by computing the hash of the key and taking the modulus of the number of shards. `imcache` exposes the `Hasher64` interface that wraps `Sum64` accepting a key and returning a 64-bit hash of the input key. It can be used to implement custom sharding algorithms.
`imcache` supports sharding. It's possible to create a new cache instance with a given number of shards. Each shard is a separate `Cache` instance. A shard for a given key is selected by computing the hash of the key and taking the modulus of the number of shards. `imcache` exposes the `Hasher64` interface that wraps `Sum64` accepting a key and returning a 64-bit hash of the input key. It can be used to implement custom sharding algorithms.

Currently, `imcache` provides only one implementation of the `Hasher64` interface: `DefaultStringHasher64`. It uses the FNV-1a hash function.

A sharded `Cache` instance can be created by calling the `NewSharded` method. It accepts the number of shards, `Hasher64` interface and optional configuration `Option`s as arguments.
A `Sharded` instance can be created by calling the `NewSharded` method. It accepts the number of shards, `Hasher64` interface and optional configuration `Option`s as arguments.

```go
c := imcache.NewSharded[string, string](4, imcache.DefaultStringHasher64{})
```

All previous examples apply to sharded `Cache` instances as well.
All previous examples apply to `Sharded` type as well.

0 comments on commit 3ae5464

Please sign in to comment.