44 "time"
55)
66
7+ // Memoize returns a memoized version of the compute function with a specified TTL.
8+ // V is the type of the value returned by the compute function.
79func Memoize [V any ](computeFn func () V , ttl time.Duration ) func () V {
810 cache := NewCacheSized [uint64 , V ](1 , int64 (ttl .Seconds ()))
911 return func () V {
@@ -13,6 +15,8 @@ func Memoize[V any](computeFn func() V, ttl time.Duration) func() V {
1315 }
1416}
1517
18+ // Memoize1 returns a memoized version of the compute function with a single key and a specified TTL.
19+ // K is the type of the key, and V is the type of the value returned by the compute function.
1620func Memoize1 [K comparable , V any ](computeFn func (K ) V , ttl time.Duration ) func (K ) V {
1721 cache := NewCache [uint64 , V ](int64 (ttl .Seconds ()))
1822 return func (k K ) V {
@@ -22,6 +26,8 @@ func Memoize1[K comparable, V any](computeFn func(K) V, ttl time.Duration) func(
2226 }
2327}
2428
29+ // Memoize2 returns a memoized version of the compute function with two keys and a specified TTL.
30+ // K1 and K2 are the types of the keys, and V is the type of the value returned by the compute function.
2531func Memoize2 [K1 , K2 comparable , V any ](computeFn func (K1 , K2 ) V , ttl time.Duration ) func (K1 , K2 ) V {
2632 cache := NewCache [uint64 , V ](int64 (ttl .Seconds ()))
2733 return func (key1 K1 , key2 K2 ) V {
@@ -30,6 +36,9 @@ func Memoize2[K1, K2 comparable, V any](computeFn func(K1, K2) V, ttl time.Durat
3036 })
3137 }
3238}
39+
40+ // Memoize3 returns a memoized version of the compute function with three keys and a specified TTL.
41+ // K1, K2, and K3 are the types of the keys, and V is the type of the value returned by the compute function.
3342func Memoize3 [K1 , K2 , K3 comparable , V any ](computeFn func (K1 , K2 , K3 ) V , ttl time.Duration ) func (K1 , K2 , K3 ) V {
3443 cache := NewCache [uint64 , V ](int64 (ttl .Seconds ()))
3544 return func (key1 K1 , key2 K2 , key3 K3 ) V {
@@ -39,6 +48,8 @@ func Memoize3[K1, K2, K3 comparable, V any](computeFn func(K1, K2, K3) V, ttl ti
3948 }
4049}
4150
51+ // Memoize4 returns a memoized version of the compute function with four keys and a specified TTL.
52+ // K1, K2, K3, and K4 are the types of the keys, and V is the type of the value returned by the compute function.
4253func Memoize4 [K1 , K2 , K3 , K4 comparable , V any ](computeFn func (K1 , K2 , K3 , K4 ) V , ttl time.Duration ) func (K1 , K2 , K3 , K4 ) V {
4354 cache := NewCache [uint64 , V ](int64 (ttl .Seconds ()))
4455 return func (key1 K1 , key2 K2 , key3 K3 , key4 K4 ) V {
@@ -48,6 +59,8 @@ func Memoize4[K1, K2, K3, K4 comparable, V any](computeFn func(K1, K2, K3, K4) V
4859 }
4960}
5061
62+ // Memoize5 returns a memoized version of the compute function with five keys and a specified TTL.
63+ // K1, K2, K3, K4, and K5 are the types of the keys, and V is the type of the value returned by the compute function.
5164func Memoize5 [K1 , K2 , K3 , K4 , K5 comparable , V any ](computeFn func (K1 , K2 , K3 , K4 , K5 ) V , ttl time.Duration ) func (K1 , K2 , K3 , K4 , K5 ) V {
5265 cache := NewCache [uint64 , V ](int64 (ttl .Seconds ()))
5366 return func (key1 K1 , key2 K2 , key3 K3 , key4 K4 , key5 K5 ) V {
@@ -57,6 +70,8 @@ func Memoize5[K1, K2, K3, K4, K5 comparable, V any](computeFn func(K1, K2, K3, K
5770 }
5871}
5972
73+ // Memoize6 returns a memoized version of the compute function with six keys and a specified TTL.
74+ // K1, K2, K3, K4, K5, and K6 are the types of the keys, and V is the type of the value returned by the compute function.
6075func Memoize6 [K1 , K2 , K3 , K4 , K5 , K6 comparable , V any ](computeFn func (K1 , K2 , K3 , K4 , K5 , K6 ) V , ttl time.Duration ) func (K1 , K2 , K3 , K4 , K5 , K6 ) V {
6176 cache := NewCache [uint64 , V ](int64 (ttl .Seconds ()))
6277 return func (key1 K1 , key2 K2 , key3 K3 , key4 K4 , key5 K5 , key6 K6 ) V {
@@ -66,6 +81,8 @@ func Memoize6[K1, K2, K3, K4, K5, K6 comparable, V any](computeFn func(K1, K2, K
6681 }
6782}
6883
84+ // Memoize7 returns a memoized version of the compute function with seven keys and a specified TTL.
85+ // K1, K2, K3, K4, K5, K6, and K7 are the types of the keys, and V is the type of the value returned by the compute function.
6986func Memoize7 [K1 , K2 , K3 , K4 , K5 , K6 , K7 comparable , V any ](computeFn func (K1 , K2 , K3 , K4 , K5 , K6 , K7 ) V , ttl time.Duration ) func (K1 , K2 , K3 , K4 , K5 , K6 , K7 ) V {
7087 cache := NewCache [uint64 , V ](int64 (ttl .Seconds ()))
7188 return func (key1 K1 , key2 K2 , key3 K3 , key4 K4 , key5 K5 , key6 K6 , key7 K7 ) V {
0 commit comments