Skip to content

Commit 33e1f7d

Browse files
committed
add fucntions with Ctx
1 parent badb55b commit 33e1f7d

File tree

3 files changed

+503
-4
lines changed

3 files changed

+503
-4
lines changed

README.md

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
- Utilizes the FNV-1a hash algorithm for caching.
1111
- Thread-safe and concurrent-safe.
1212

13-
1413
## Installation
1514

1615
To install the package, use `go get`:
@@ -33,6 +32,16 @@ computeFn := func() int {
3332

3433
memoizedFn := Memoize(computeFn, 10*time.Second)
3534
result := memoizedFn()
35+
```
36+
The same for functions with Context:
37+
38+
```go
39+
computeCtxFn := func(ctx context.Context) int {
40+
// Expensive computation
41+
return 42
42+
}
43+
memoizedCtxFn := MemoizeCtx(computeCtxFn, 10*time.Second)
44+
result := memoizedCtxFn(context.Background())
3645
```
3746

3847
### Memoization with Parameters
@@ -51,6 +60,18 @@ memoizedFn := Memoize1(computeFn, 10*time.Second)
5160
result := memoizedFn(5)
5261
```
5362

63+
The same for functions with Context:
64+
65+
```go
66+
computeCtxFn := func(ctx context.Context, a int) int {
67+
// Expensive computation
68+
return a * 2
69+
}
70+
71+
memoizedCtxFn := MemoizeCtx1(computeCtxFn, 10*time.Second)
72+
result := memoizedCtxFn(context.Background(), 5)
73+
```
74+
5475
#### Two Parameters
5576

5677
```go
@@ -63,6 +84,18 @@ memoizedFn := Memoize2(computeFn, 10*time.Second)
6384
result := memoizedFn(5, "example")
6485
```
6586

87+
The same for functions with Context:
88+
89+
```go
90+
computeCtxFn := func(ctx context.Context, a int, b string) string {
91+
// Expensive computation
92+
return fmt.Sprintf("%d-%s", a, b)
93+
}
94+
95+
memoizedCtxFn := MemoizeCtx2(computeCtxFn, 10*time.Second)
96+
result := memoizedCtxFn(context.Background(), 5, "example")
97+
```
98+
6699
#### Three Parameters
67100

68101
```go
@@ -75,6 +108,18 @@ memoizedFn := Memoize3(computeFn, 10*time.Second)
75108
result := memoizedFn(5, "example", 3.14)
76109
```
77110

111+
The same for functions with Context:
112+
113+
```go
114+
computeCtxFn := func(ctx context.Context, a int, b string, c float64) string {
115+
// Expensive computation
116+
return fmt.Sprintf("%d-%s-%f", a, b, c)
117+
}
118+
119+
memoizedCtxFn := MemoizeCtx3(computeCtxFn, 10*time.Second)
120+
result := memoizedCtxFn(context.Background(), 5, "example", 3.14)
121+
```
122+
78123
### Cache Management
79124

80125
The `Cache` struct is used internally to manage the cached entries. It supports setting, getting, and deleting entries, as well as computing new values if they are not already cached or have expired.
@@ -199,11 +244,90 @@ result := memoizedFn(1, 2, 3, 4, 5, 6, 7)
199244
</code></pre>
200245
</td>
201246
</tr>
247+
<tr>
248+
<td><code>MemoizeCtx</code></td>
249+
<td>Memoizes a function with context and no params</td>
250+
<td>
251+
<pre><code>
252+
memoizedCtxFn := MemoizeCtx(func(ctx context.Context) int { return 1 }, time.Minute)
253+
result := memoizedCtxFn(context.Background())
254+
</code></pre>
255+
</td>
256+
</tr>
257+
<tr>
258+
<td><code>MemoizeCtx1</code></td>
259+
<td>Memoizes a function with context and 1 param</td>
260+
<td>
261+
<pre><code>
262+
memoizedCtxFn := MemoizeCtx1(func(ctx context.Context, a int) int { return a * 2 }, time.Minute)
263+
result := memoizedCtxFn(context.Background(), 5)
264+
</code></pre>
265+
</td>
266+
</tr>
267+
<tr>
268+
<td><code>MemoizeCtx2</code></td>
269+
<td>Memoizes a function with context and 2 params</td>
270+
<td>
271+
<pre><code>
272+
memoizedCtxFn := MemoizeCtx2(func(ctx context.Context, a int, b string) string { return fmt.Sprintf("%d-%s", a, b) }, time.Minute)
273+
result := memoizedCtxFn(context.Background(), 5, "example")
274+
</code></pre>
275+
</td>
276+
</tr>
277+
<tr>
278+
<td><code>MemoizeCtx3</code></td>
279+
<td>Memoizes a function with context and 3 params</td>
280+
<td>
281+
<pre><code>
282+
memoizedCtxFn := MemoizeCtx3(func(ctx context.Context, a int, b string, c float64) string { return fmt.Sprintf("%d-%s-%f", a, b, c) }, time.Minute)
283+
result := memoizedCtxFn(context.Background(), 5, "example", 3.14)
284+
</code></pre>
285+
</td>
286+
</tr>
287+
<tr>
288+
<td><code>MemoizeCtx4</code></td>
289+
<td>Memoizes a function with context and 4 params</td>
290+
<td>
291+
<pre><code>
292+
memoizedCtxFn := MemoizeCtx4(func(ctx context.Context, a, b, c, d int) int { return a + b + c + d }, time.Minute)
293+
result := memoizedCtxFn(context.Background(), 1, 2, 3, 4)
294+
</code></pre>
295+
</td>
296+
</tr>
297+
<tr>
298+
<td><code>MemoizeCtx5</code></td>
299+
<td>Memoizes a function with context and 5 params</td>
300+
<td>
301+
<pre><code>
302+
memoizedCtxFn := MemoizeCtx5(func(ctx context.Context, a, b, c, d, e int) int { return a + b + c + d + e }, time.Minute)
303+
result := memoizedCtxFn(context.Background(), 1, 2, 3, 4, 5)
304+
</code></pre>
305+
</td>
306+
</tr>
307+
<tr>
308+
<td><code>MemoizeCtx6</code></td>
309+
<td>Memoizes a function with context and 6 params</td>
310+
<td>
311+
<pre><code>
312+
memoizedCtxFn := MemoizeCtx6(func(ctx context.Context, a, b, c, d, e, f int) int { return a + b + c + d + e + f }, time.Minute)
313+
result := memoizedCtxFn(context.Background(), 1, 2, 3, 4, 5, 6)
314+
</code></pre>
315+
</td>
316+
</tr>
317+
<tr>
318+
<td><code>MemoizeCtx7</code></td>
319+
<td>Memoizes a function with context and 7 params</td>
320+
<td>
321+
<pre><code>
322+
memoizedCtxFn := MemoizeCtx7(func(ctx context.Context, a, b, c, d, e, f, g int) int { return a + b + c + d + e + f + g }, time.Minute)
323+
result := memoizedCtxFn(context.Background(), 1, 2, 3, 4, 5, 6, 7)
324+
</code></pre>
325+
</td>
326+
</tr>
202327
</table>
203328

204-
205329
### [For more benchmarking results please check this](https://github.com/AhmedGoudaa/go_memoize/blob/bechmarking/benchmarks/README.md)
206-
Device "Apple M2 Pro"
330+
Device "Apple M2 Pro"
207331

208332
```
209333
goos: darwin
@@ -216,4 +340,4 @@ BenchmarkDo4Mem-10 | 487471639 | 24.73 ns/op | 0 B/op | 0 allocs/op
216340
217341
```
218342

219-
This project is licensed under the Apache License. See the [`LICENSE`](https://github.com/AhmedGoudaa/go_memoize/blob/main/LICENSE) file for details.
343+
This project is licensed under the Apache License. See the [`LICENSE`](https://github.com/AhmedGoudaa/go_memoize/blob/main/LICENSE) file for details.

memoize_ctx.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package go_memoize
2+
3+
import (
4+
"context"
5+
"time"
6+
)
7+
8+
// MemoizeCtx returns a memoized version of the compute function with a specified TTL.
9+
func MemoizeCtx[V any](computeFn func(context.Context) V, ttl time.Duration) func(context.Context) V {
10+
cache := NewCacheSized[uint64, V](1, int64(ttl.Seconds()))
11+
return func(ctx context.Context) V {
12+
return cache.GetOrCompute(0, func() V {
13+
return computeFn(ctx)
14+
})
15+
}
16+
}
17+
18+
// MemoizeCtx1 returns a memoized version of the compute function with a single key and a specified TTL.
19+
func MemoizeCtx1[K comparable, V any](computeFn func(context.Context, K) V, ttl time.Duration) func(context.Context, K) V {
20+
cache := NewCache[uint64, V](int64(ttl.Seconds()))
21+
return func(ctx context.Context, k K) V {
22+
return cache.GetOrCompute(hash1(k), func() V {
23+
return computeFn(ctx, k)
24+
})
25+
}
26+
}
27+
28+
// MemoizeCtx2 returns a memoized version of the compute function with two keys and a specified TTL.
29+
func MemoizeCtx2[K1, K2 comparable, V any](computeFn func(context.Context, K1, K2) V, ttl time.Duration) func(context.Context, K1, K2) V {
30+
cache := NewCache[uint64, V](int64(ttl.Seconds()))
31+
return func(ctx context.Context, key1 K1, key2 K2) V {
32+
return cache.GetOrCompute(hash2(key1, key2), func() V {
33+
return computeFn(ctx, key1, key2)
34+
})
35+
}
36+
}
37+
38+
// MemoizeCtx3 returns a memoized version of the compute function with three keys and a specified TTL.
39+
func MemoizeCtx3[K1, K2, K3 comparable, V any](computeFn func(context.Context, K1, K2, K3) V, ttl time.Duration) func(context.Context, K1, K2, K3) V {
40+
cache := NewCache[uint64, V](int64(ttl.Seconds()))
41+
return func(ctx context.Context, key1 K1, key2 K2, key3 K3) V {
42+
return cache.GetOrCompute(hash3(key1, key2, key3), func() V {
43+
return computeFn(ctx, key1, key2, key3)
44+
})
45+
}
46+
}
47+
48+
// MemoizeCtx4 returns a memoized version of the compute function with four keys and a specified TTL.
49+
func MemoizeCtx4[K1, K2, K3, K4 comparable, V any](computeFn func(context.Context, K1, K2, K3, K4) V, ttl time.Duration) func(context.Context, K1, K2, K3, K4) V {
50+
cache := NewCache[uint64, V](int64(ttl.Seconds()))
51+
return func(ctx context.Context, key1 K1, key2 K2, key3 K3, key4 K4) V {
52+
return cache.GetOrCompute(hash4(key1, key2, key3, key4), func() V {
53+
return computeFn(ctx, key1, key2, key3, key4)
54+
})
55+
}
56+
}
57+
58+
// MemoizeCtx5 returns a memoized version of the compute function with five keys and a specified TTL.
59+
func MemoizeCtx5[K1, K2, K3, K4, K5 comparable, V any](computeFn func(context.Context, K1, K2, K3, K4, K5) V, ttl time.Duration) func(context.Context, K1, K2, K3, K4, K5) V {
60+
cache := NewCache[uint64, V](int64(ttl.Seconds()))
61+
return func(ctx context.Context, key1 K1, key2 K2, key3 K3, key4 K4, key5 K5) V {
62+
return cache.GetOrCompute(hash5(key1, key2, key3, key4, key5), func() V {
63+
return computeFn(ctx, key1, key2, key3, key4, key5)
64+
})
65+
}
66+
}
67+
68+
// MemoizeCtx6 returns a memoized version of the compute function with six keys and a specified TTL.
69+
func MemoizeCtx6[K1, K2, K3, K4, K5, K6 comparable, V any](computeFn func(context.Context, K1, K2, K3, K4, K5, K6) V, ttl time.Duration) func(context.Context, K1, K2, K3, K4, K5, K6) V {
70+
cache := NewCache[uint64, V](int64(ttl.Seconds()))
71+
return func(ctx context.Context, key1 K1, key2 K2, key3 K3, key4 K4, key5 K5, key6 K6) V {
72+
return cache.GetOrCompute(hash6(key1, key2, key3, key4, key5, key6), func() V {
73+
return computeFn(ctx, key1, key2, key3, key4, key5, key6)
74+
})
75+
}
76+
}
77+
78+
// MemoizeCtx7 returns a memoized version of the compute function with seven keys and a specified TTL.
79+
func MemoizeCtx7[K1, K2, K3, K4, K5, K6, K7 comparable, V any](computeFn func(context.Context, K1, K2, K3, K4, K5, K6, K7) V, ttl time.Duration) func(context.Context, K1, K2, K3, K4, K5, K6, K7) V {
80+
cache := NewCache[uint64, V](int64(ttl.Seconds()))
81+
return func(ctx context.Context, key1 K1, key2 K2, key3 K3, key4 K4, key5 K5, key6 K6, key7 K7) V {
82+
return cache.GetOrCompute(hash7(key1, key2, key3, key4, key5, key6, key7), func() V {
83+
return computeFn(ctx, key1, key2, key3, key4, key5, key6, key7)
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)