{"id":30041669,"url":"https://github.com/agkloop/go_memoize","last_synced_at":"2025-08-07T02:51:21.831Z","repository":{"id":279797729,"uuid":"939900384","full_name":"agkloop/go_memoize","owner":"agkloop","description":"Golang high performant functional Memoize","archived":false,"fork":false,"pushed_at":"2025-04-14T10:02:20.000Z","size":55,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-01T00:30:08.074Z","etag":null,"topics":["fnv-1a","functional","golang","high-performance","memoize","ttl-cache","zero-allocation"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/agkloop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-02-27T09:42:59.000Z","updated_at":"2025-07-12T19:42:10.000Z","dependencies_parsed_at":"2025-02-27T19:05:45.241Z","dependency_job_id":"3ba798b5-814a-4f05-8d74-f838d65befbb","html_url":"https://github.com/agkloop/go_memoize","commit_stats":null,"previous_names":["ahmedgoudaa/go-memoize","agkloop/go_memoize","ahmedgoudaa/go_memoize"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/agkloop/go_memoize","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agkloop%2Fgo_memoize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agkloop%2Fgo_memoize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agkloop%2Fgo_memoize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agkloop%2Fgo_memoize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/agkloop","download_url":"https://codeload.github.com/agkloop/go_memoize/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agkloop%2Fgo_memoize/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269189973,"owners_count":24375572,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-07T02:00:09.698Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fnv-1a","functional","golang","high-performance","memoize","ttl-cache","zero-allocation"],"created_at":"2025-08-07T02:51:16.020Z","updated_at":"2025-08-07T02:51:21.803Z","avatar_url":"https://github.com/agkloop.png","language":"Go","readme":"# go_memoize \n\n![Workflow Status](https://github.com/agkloop/go_memoize/actions/workflows/ci.yml/badge.svg)\n\n`go_memoize` package provides a set of functions to memoize the results of computations, allowing for efficient caching and retrieval of results based on input parameters. This can significantly improve performance for expensive or frequently called functions.\n\n## Features\n- Memoizes functions with TTL, supporting 0 to 7 comparable parameters. [List of Memoize Functions](https://github.com/agkloop/go_memoize/blob/main/memoize.go)\n- High performance, zero allocation, and zero dependencies.\n- Utilizes the FNV-1a hash algorithm for caching.\n- Thread-safe and concurrent-safe.\n\n## Installation\n\nTo install the package, use `go get`:\n\n```sh\ngo get github.com/agkloop/go_memoize\n```\n\n## Usage\n\n### Basic Memoization\n\nThe `Memoize` function can be used to memoize a function with no parameters:\n\n```go\ncomputeFn := func() int {\n    // Expensive computation\n    return 42\n}\n\nmemoizedFn := Memoize(computeFn, 10*time.Second)\nresult := memoizedFn()\n``` \nThe same for functions with Context:\n\n```go\ncomputeCtxFn := func(ctx context.Context) int {\n    // Expensive computation\n    return 42\n}\nmemoizedCtxFn := MemoizeCtx(computeCtxFn, 10*time.Second)\nresult := memoizedCtxFn(context.Background())\n```\n\n### Memoization with Parameters\n\nThe package provides functions to memoize functions with up to 7 parameters. Here are some examples:\n\n#### One Parameter\n\n```go\ncomputeFn := func(a int) int {\n    // Expensive computation\n    return a * 2\n}\n\nmemoizedFn := Memoize1(computeFn, 10*time.Second)\nresult := memoizedFn(5)\n```\n\nThe same for functions with Context:\n\n```go\ncomputeCtxFn := func(ctx context.Context, a int) int {\n    // Expensive computation\n    return a * 2\n}\n\nmemoizedCtxFn := MemoizeCtx1(computeCtxFn, 10*time.Second)\nresult := memoizedCtxFn(context.Background(), 5)\n```\n\n#### Two Parameters\n\n```go\ncomputeFn := func(a int, b string) string {\n    // Expensive computation\n    return fmt.Sprintf(\"%d-%s\", a, b)\n}\n\nmemoizedFn := Memoize2(computeFn, 10*time.Second)\nresult := memoizedFn(5, \"example\")\n```\n\nThe same for functions with Context:\n\n```go\ncomputeCtxFn := func(ctx context.Context, a int, b string) string {\n    // Expensive computation\n    return fmt.Sprintf(\"%d-%s\", a, b)\n}\n\nmemoizedCtxFn := MemoizeCtx2(computeCtxFn, 10*time.Second)\nresult := memoizedCtxFn(context.Background(), 5, \"example\")\n```\n\n#### Three Parameters\n\n```go\ncomputeFn := func(a int, b string, c float64) string {\n    // Expensive computation\n    return fmt.Sprintf(\"%d-%s-%f\", a, b, c)\n}\n\nmemoizedFn := Memoize3(computeFn, 10*time.Second)\nresult := memoizedFn(5, \"example\", 3.14)\n```\n\nThe same for functions with Context:\n\n```go\ncomputeCtxFn := func(ctx context.Context, a int, b string, c float64) string {\n    // Expensive computation\n    return fmt.Sprintf(\"%d-%s-%f\", a, b, c)\n}\n\nmemoizedCtxFn := MemoizeCtx3(computeCtxFn, 10*time.Second)\nresult := memoizedCtxFn(context.Background(), 5, \"example\", 3.14)\n```\n\n### Cache Management\n\nThe `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.\n\n## Example\n\nHere is a complete example of using the `memoize` package:\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"time\"\n    m \"github.com/agkloop/go_memoize\"\n)\n\nfunc main() {\n    computeFn := func(a int, b string) string {\n        // Simulate an expensive computation\n        time.Sleep(2 * time.Second)\n        return fmt.Sprintf(\"%d-%s\", a, b)\n    }\n\n    memoizedFn := m.Memoize2(computeFn, 10*time.Second)\n\n    // First call will compute the result\n    result := memoizedFn(5, \"example\")\n    fmt.Println(result) // Output: 5-example\n\n    // Subsequent calls within 10 seconds will use the cached result\n    result = memoizedFn(5, \"example\")\n    fmt.Println(result) // Output: 5-example\n}\n```\n\n## Functions \u0026 Usage Examples\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003e\u003ccode\u003eFunction\u003c/code\u003e\u003c/th\u003e\n    \u003cth\u003e\u003ccode\u003eDescription\u003c/code\u003e\u003c/th\u003e\n    \u003cth\u003e\u003ccode\u003eExample\u003c/code\u003e\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoize\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with no params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedFn := Memoize(func() int { return 1 }, time.Minute)\nresult := memoizedFn()\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoize1\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with 1 param\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedFn := Memoize1(func(a int) int { return a * 2 }, time.Minute)\nresult := memoizedFn(5)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoize2\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with 2 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedFn := Memoize2(func(a int, b string) string { return fmt.Sprintf(\"%d-%s\", a, b) }, time.Minute)\nresult := memoizedFn(5, \"example\")\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoize3\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with 3 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedFn := Memoize3(func(a int, b string, c float64) string { return fmt.Sprintf(\"%d-%s-%f\", a, b, c) }, time.Minute)\nresult := memoizedFn(5, \"example\", 3.14)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoize4\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with 4 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedFn := Memoize4(func(a, b, c, d int) int { return a + b + c + d }, time.Minute)\nresult := memoizedFn(1, 2, 3, 4)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoize5\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with 5 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedFn := Memoize5(func(a, b, c, d, e int) int { return a + b + c + d + e }, time.Minute)\nresult := memoizedFn(1, 2, 3, 4, 5)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoize6\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with 6 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedFn := Memoize6(func(a, b, c, d, e, f int) int { return a + b + c + d + e + f }, time.Minute)\nresult := memoizedFn(1, 2, 3, 4, 5, 6)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoize7\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with 7 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedFn := Memoize7(func(a, b, c, d, e, f, g int) int { return a + b + c + d + e + f + g }, time.Minute)\nresult := memoizedFn(1, 2, 3, 4, 5, 6, 7)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoizeCtx\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with context and no params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedCtxFn := MemoizeCtx(func(ctx context.Context) int { return 1 }, time.Minute)\nresult := memoizedCtxFn(context.Background())\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoizeCtx1\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with context and 1 param\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedCtxFn := MemoizeCtx1(func(ctx context.Context, a int) int { return a * 2 }, time.Minute)\nresult := memoizedCtxFn(context.Background(), 5)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoizeCtx2\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with context and 2 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedCtxFn := MemoizeCtx2(func(ctx context.Context, a int, b string) string { return fmt.Sprintf(\"%d-%s\", a, b) }, time.Minute)\nresult := memoizedCtxFn(context.Background(), 5, \"example\")\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoizeCtx3\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with context and 3 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedCtxFn := MemoizeCtx3(func(ctx context.Context, a int, b string, c float64) string { return fmt.Sprintf(\"%d-%s-%f\", a, b, c) }, time.Minute)\nresult := memoizedCtxFn(context.Background(), 5, \"example\", 3.14)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoizeCtx4\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with context and 4 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedCtxFn := MemoizeCtx4(func(ctx context.Context, a, b, c, d int) int { return a + b + c + d }, time.Minute)\nresult := memoizedCtxFn(context.Background(), 1, 2, 3, 4)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoizeCtx5\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with context and 5 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedCtxFn := MemoizeCtx5(func(ctx context.Context, a, b, c, d, e int) int { return a + b + c + d + e }, time.Minute)\nresult := memoizedCtxFn(context.Background(), 1, 2, 3, 4, 5)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoizeCtx6\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with context and 6 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedCtxFn := MemoizeCtx6(func(ctx context.Context, a, b, c, d, e, f int) int { return a + b + c + d + e + f }, time.Minute)\nresult := memoizedCtxFn(context.Background(), 1, 2, 3, 4, 5, 6)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ccode\u003eMemoizeCtx7\u003c/code\u003e\u003c/td\u003e\n    \u003ctd\u003eMemoizes a function with context and 7 params\u003c/td\u003e\n    \u003ctd\u003e\n      \u003cpre\u003e\u003ccode\u003e\nmemoizedCtxFn := MemoizeCtx7(func(ctx context.Context, a, b, c, d, e, f, g int) int { return a + b + c + d + e + f + g }, time.Minute)\nresult := memoizedCtxFn(context.Background(), 1, 2, 3, 4, 5, 6, 7)\n      \u003c/code\u003e\u003c/pre\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n### [For more benchmarking results please check this](https://github.com/agkloop/go_memoize/blob/bechmarking/benchmarks/README.md)\nDevice \"Apple M2 Pro\"\n\n```\ngoos: darwin\ngoarch: arm64\nBenchmarkDo0Mem-10 | 811289566 | 14.77 ns/op | 0 B/op | 0 allocs/op\nBenchmarkDo1Mem-10 | 676579908 | 18.26 ns/op | 0 B/op | 0 allocs/op\nBenchmarkDo2Mem-10 | 578134332 | 20.99 ns/op | 0 B/op | 0 allocs/op\nBenchmarkDo3Mem-10 | 533455237 | 22.67 ns/op | 0 B/op | 0 allocs/op\nBenchmarkDo4Mem-10 | 487471639 | 24.73 ns/op | 0 B/op | 0 allocs/op\n\n```\n\nThis project is licensed under the Apache License. See the [`LICENSE`](https://github.com/agkloop/go_memoize/blob/main/LICENSE) file for details.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagkloop%2Fgo_memoize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fagkloop%2Fgo_memoize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagkloop%2Fgo_memoize/lists"}