|
| 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