{"id":15074359,"url":"https://github.com/n-r-w/reqcache","last_synced_at":"2026-03-07T05:06:44.472Z","repository":{"id":254575855,"uuid":"846919064","full_name":"n-r-w/reqcache","owner":"n-r-w","description":"ReqCache is a Go package for caching data within a single request context, allowing pre-allocation of memory blocks to reduce garbage collector load and caching objects by a unique key.","archived":false,"fork":false,"pushed_at":"2025-11-27T15:59:58.000Z","size":47,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-30T04:25:12.282Z","etag":null,"topics":["cache","caching","grpc","http","memory-management","pool"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/n-r-w.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":"2024-08-24T10:22:37.000Z","updated_at":"2025-11-27T15:59:16.000Z","dependencies_parsed_at":"2024-08-24T13:41:49.255Z","dependency_job_id":"2d314d57-c9ad-46d6-90c8-1dfe235b0af6","html_url":"https://github.com/n-r-w/reqcache","commit_stats":{"total_commits":5,"total_committers":1,"mean_commits":5.0,"dds":0.0,"last_synced_commit":"fb6fb6fd2000bae180c034fea1a191fb9330383f"},"previous_names":["n-r-w/reqcache"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/n-r-w/reqcache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Freqcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Freqcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Freqcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Freqcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/n-r-w","download_url":"https://codeload.github.com/n-r-w/reqcache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Freqcache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30208730,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T03:24:23.086Z","status":"ssl_error","status_checked_at":"2026-03-07T03:23:11.444Z","response_time":53,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["cache","caching","grpc","http","memory-management","pool"],"created_at":"2024-09-25T03:32:32.972Z","updated_at":"2026-03-07T05:06:44.460Z","avatar_url":"https://github.com/n-r-w.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Go Reference](https://pkg.go.dev/badge/github.com/n-r-w/reqcache.svg)](https://pkg.go.dev/github.com/n-r-w/reqcache)\n[![Go Coverage](https://github.com/n-r-w/reqcache/wiki/coverage.svg)](https://raw.githack.com/wiki/n-r-w/reqcache/coverage.html)\n![CI Status](https://github.com/n-r-w/reqcache/actions/workflows/go.yml/badge.svg)\n[![Stability](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)\n[![Go Report](https://goreportcard.com/badge/github.com/n-r-w/reqcache)](https://goreportcard.com/badge/github.com/n-r-w/reqcache)\n\n# ReqCache\n\nReqCache is a Go package for caching data within a single request context.\nUsable in web servers, gRPC servers, and other applications with a request-response lifecycle.\nIt allows to:\n\n- Pre-allocate a single block of memory for creating multiple objects and reduce the load on the garbage collector\n- Cache objects by unique keys\n\n## Installation\n\nTo install the package, you need to run:\n\n```sh\ngo get -u github.com/n-r-w/reqcache@latest\n```\n\n## Benchmarks with and without batch allocation\n\n```sh\nBenchmarkWithoutBatchAllocation-32          747    1513720 ns/op 10240114 B/op    10002 allocs/op\nBenchmarkWithBatchAllocation-32            4189     251629 ns/op     2598 B/op        3 allocs/op\n```\n\n## Key Type Performance\n\nWhen choosing cache key types, string keys generally perform better than struct keys:\n\n```sh\nBenchmarkStringKey-16    \t   61869\t     18234 ns/op\nBenchmarkStructKey-16    \t   44949\t     26602 ns/op\n```\n\nString keys are ~31% faster due to more efficient hashing and comparison operations in Go's map implementation.\n\n## Usage\n\n### Create a reqcache object\n\nCache object can be shared between multiple requests. It is recommended to create a single cache object for each unique type of object and key.\n\n```go\nconst (\n    // number of pre-allocated objects. can be 0 if pre-allocation is not needed\n    preAllocatedObjects = 1000\n    // maximum number of objects in the cache. can be 0 if the cache not needed\n    maxCacheSize = 10000\n)\n\ncache, err := reqcache.New[KeyType, ObjectType](\n    preAllocatedObjects, maxCacheSize,    \n    reqcache.WithLogger(\"cache name\", logger), // used for logging/metrics pre-allocated memory overflow and cache hits   \n)\n```\n\n### Start a new session\n\nNewSession adds a new session key to the context. It must be called once at the beginning of the request processing.\n\n```go\nctx, err := reqcache.NewSession(ctx)\nif err != nil {\n    // handle error\n    panic(err)\n}\n```\n\n### End the session\n\nEndSession removes all cache data from the reqcache object, associated with the session key.\n\n```go\ndefer func() {\n    if err := cache.EndSession(ctx); err != nil {\n        // handle error\n        log.Printf(\"Error ending session: %v\", err)\n    }\n}()\n```\n\n### Create a new object\n\nNewObject takes a pointer to object from the pre-allocated memory.\nIf no pre-allocated memory is available (because too many objects have already been taken from the cache), a new object is created.\nIn case of an object pool overflow, the logger will be called.\n\n```go\nnewObj, err := cache.NewObject(ctx)\n```\n\n### Put an object into the cache\n\nPut adds an object to the cache by a unique key.\n\n```go\nif err := cache.Put(ctx, key, newObj); err != nil {\n    // handle error\n    panic(err)\n}\n```\n\n### Get an object from the cache\n\nGet returns an object from the cache by a unique key.\n\n```go\nobj, ok, err := cache.Get(ctx, key)\n```\n\n### Other methods\n\n- `Exists` checks if an object exists in the cache.\n- `Delete` removes an object from the cache.\n- `GetOrFetch` returns data from the cache or fetches it from the fetcher function (for example, from a database).\n- `GetOrNew` returns data from the cache or creates it and prepares with the prepare function.\n\n## Example\n\n```go\n//nolint:gochecknoglobals,revive // example\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"log\"\n    \"net/http\"\n\n    \"github.com/n-r-w/reqcache\"\n)\n\ntype MyCache = reqcache.ReqCache[myKey, myObject]\n\nfunc main() {\n    const (\n        objSize   = 5  // number of pre-allocated objects\n        cacheSize = 10 // maximum number of objects in the cache\n\n    )\n\n    cache, err := reqcache.New[myKey, myObject](\n        objSize, cacheSize,\n        reqcache.WithLogger(\"example\", \u0026myLogger{}), // logging and metrics for object pool overflows\n    )\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    http.Handle(\"/\", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n        // Prepare context for cache operations\n        ctx, err := reqcache.NewSession(r.Context())\n        if err != nil {\n            http.Error(w, \"Failed to create session\", http.StatusInternalServerError)\n            return\n        }\n\n        // clean up the cache data after the request\n        defer func() {\n            if err := cache.EndSession(ctx); err != nil {\n                log.Printf(\"Error ending session: %v\", err)\n            }\n        }()\n\n        // Simulate some data processing\n        if err := workFunc1(ctx, cache); err != nil {\n            http.Error(w, \"Processing error\", http.StatusInternalServerError)\n            return\n        }\n        if err := workFunc2(ctx, cache); err != nil {\n            http.Error(w, \"Processing error\", http.StatusInternalServerError)\n            return\n        }\n\n        _, _ = fmt.Fprintf(w, \"request processed\")\n\n        log.Println(\"Request processed\")\n    }))\n\n    log.Println(\"Server started at http://127.0.0.1:8080\")\n\n    if err := http.ListenAndServe(\":8080\", nil); err != nil { //nolint:gosec // no need for example\n        log.Fatal(err)\n    }\n}\n\nvar (\n    dataKey1 = myKey{Key1: \"some-key11\", Key2: \"some-key12\"}\n    dataKey2 = myKey{Key1: \"some-key21\", Key2: \"some-key22\"}\n    dataKey3 = myKey{Key1: \"some-key31\", Key2: \"some-key32\"}\n    dataKey4 = myKey{Key1: \"some-key41\", Key2: \"some-key42\"}\n)\n\nfunc workFunc1(ctx context.Context, cache *MyCache) error {\n    // Create a new object from the pre-allocated memory\n    newObj1, err := cache.NewObject(ctx)\n    if err != nil {\n        return err\n    }\n    // Set the value\n    newObj1.Value = \"Hello, World 1!\"\n\n    // Put the object into the cache\n    if err := cache.Put(ctx, dataKey1, newObj1); err != nil {\n        return err\n    }\n\n    // Create another object manually\n    newObj2 := \u0026myObject{Value: \"Hello, World 2!\"}\n\n    // Put the object into the cache\n    if err := cache.Put(ctx, dataKey2, newObj2); err != nil {\n        return err\n    }\n    \n    return nil\n}\n\nfunc workFunc2(ctx context.Context, cache *MyCache) error {\n    // obj1 is cached\n    obj1, found, err := cache.Get(ctx, dataKey1)\n    if err != nil {\n        return err\n    }\n    if found {\n        log.Println(\"obj1 is cached:\", obj1.Value)\n    }\n\n    // obj3 is not cached. will be fetched and cached\n    obj3, err := cache.GetOrFetch(ctx, dataKey3,\n        func(_ context.Context) (*myObject, error) {\n            // fetching data\n            return \u0026myObject{Value: \"Hello, World 3!\"}, nil\n        })\n    if err != nil {\n        return err\n    }\n    log.Println(\"obj3 is fetched:\", obj3.Value)\n\n    return workFunc3(ctx, cache)\n}\n\nfunc workFunc3(ctx context.Context, cache *MyCache) error {\n    // obj3 is cached\n    obj3, found, err := cache.Get(ctx, dataKey3)\n    if err != nil {\n        return err\n    }\n    if found {\n        log.Println(\"obj3 is cached:\", obj3.Value)\n    }\n\n    // obj4 is not cached. will be created by NewObject, prepared and cached\n    obj4, err := cache.GetOrNew(ctx, dataKey4,\n        func(_ context.Context, obj *myObject) error {\n            // preparing data\n            obj.Value = \"Hello, World 4!\"\n            return nil\n        })\n    if err != nil {\n        return err\n    }\n    log.Println(\"obj4 is created by NewObject and prepared:\", obj4.Value)\n    \n    return nil\n}\n\n// myLogger is a custom logger for ReqCache. It logs object pool overflows and cache hits.\n// Not required.\ntype myLogger struct{}\n\nfunc (m *myLogger) LogObjectPoolHitRatio(_ context.Context, name string, hit bool) {\n    log.Printf(\"Object pool hit: %s, hit: %v\", name, hit)\n}\n\nfunc (m *myLogger) LogCacheHitRatio(_ context.Context, name string, hit bool) {\n    log.Printf(\"Cache hit: %s, hit: %v\", name, hit)\n}\n\n// myKey is a custom key type for ReqCache.\ntype myKey struct {\n    Key1 string\n    Key2 string\n}\n\n// myObject is a custom object type for ReqCache.\ntype myObject struct {\n    Value string\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn-r-w%2Freqcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fn-r-w%2Freqcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn-r-w%2Freqcache/lists"}