Skip to content

Commit

Permalink
Update to go 1.22
Browse files Browse the repository at this point in the history
  • Loading branch information
kofalt committed May 6, 2024
1 parent c2dc1e3 commit 5c7b9f7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Nathaniel Kofalt
Copyright (c) 2024 Nathaniel Kofalt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/kofalt/go-memoize

go 1.19
go 1.22

require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/smartystreets/assertions v1.2.0
github.com/smartystreets/gunit v1.4.2
golang.org/x/sync v0.0.0-20220907140024-f12130a52804
golang.org/x/sync v0.7.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
github.com/smartystreets/gunit v1.4.2 h1:tyWYZffdPhQPfK5VsMQXfauwnJkqg7Tv5DLuQVYxq3Q=
github.com/smartystreets/gunit v1.4.2/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak=
golang.org/x/sync v0.0.0-20220907140024-f12130a52804 h1:0SH2R3f1b1VmIMG7BXbEZCBUu2dKmHschSmjqGUrW8A=
golang.org/x/sync v0.0.0-20220907140024-f12130a52804/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
4 changes: 2 additions & 2 deletions memoize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (t *F) TestBasic() {
expensiveCalls := 0

// Function tracks how many times its been called
expensive := func() (interface{}, error) {
expensive := func() (any, error) {
expensiveCalls++
return expensiveCalls, nil
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func (t *F) TestFailure() {
calls := 0

// This function will fail IFF it has not been called before.
twoForTheMoney := func() (interface{}, error) {
twoForTheMoney := func() (any, error) {
calls++

if calls == 1 {
Expand Down
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Any expensive call that you wish to cache
expensive := func() (interface{}, error) {
expensive := func() (any, error) {
time.Sleep(3 * time.Second)
return "some data", nil
}
Expand All @@ -48,13 +48,13 @@ In the example above, `result` is:
1. the return value from your function if `cached` is false, or
1. a previously stored value if `cached` is true.

All the hard stuff is punted to patrickmn's [go-cache](https://github.com/patrickmn/go-cache) and the Go team's [x/sync/singleflight](https://github.com/golang/sync), I just lashed them together.
All the hard stuff is punted to [go-cache](https://github.com/patrickmn/go-cache) and the [x/sync/singleflight](https://github.com/golang/sync), I just lashed them together.<br/>
Note that `cache.Storage` is exported, so you can use underlying features such as [Flush](https://godoc.org/github.com/patrickmn/go-cache#Cache.Flush) or [SaveFile](https://godoc.org/github.com/patrickmn/go-cache#Cache.SaveFile).

Also note that `cache.Storage` is exported, so you can use the underlying cache features - such as [Flush](https://godoc.org/github.com/patrickmn/go-cache#Cache.Flush) or [SaveFile](https://godoc.org/github.com/patrickmn/go-cache#Cache.SaveFile).
### Type safety

### Generics

You can also use generics. The same example as above
The default usage stores and returns an `any` type.<br/>
If you wants to store & retrieve a specific type, use `Call` instead:

```golang
import (
Expand All @@ -63,13 +63,13 @@ import (
"github.com/kofalt/go-memoize"
)

// Any expensive call that you wish to cache
// Same example as above, but this func returns a string!
expensive := func() (string, error) {
time.Sleep(3 * time.Second)
return "some data", nil
}

// Cache expensive calls in memory for 90 seconds, purging old entries every 10 minutes.
// Same as before
cache := memoize.NewMemoizer(90*time.Second, 10*time.Minute)

// This will call the expensive func, and return a string.
Expand Down

0 comments on commit 5c7b9f7

Please sign in to comment.