{"id":20712990,"url":"https://github.com/thoughtstuff/thoughtstuff.caching","last_synced_at":"2025-08-10T21:06:51.059Z","repository":{"id":65510307,"uuid":"573059630","full_name":"ThoughtStuff/ThoughtStuff.Caching","owner":"ThoughtStuff","description":"Injects look-through cache for configured services","archived":false,"fork":false,"pushed_at":"2023-04-05T22:25:03.000Z","size":1265,"stargazers_count":18,"open_issues_count":4,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-20T11:19:34.419Z","etag":null,"topics":["cache","dotnet","memcache"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/ThoughtStuff.Caching","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ThoughtStuff.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}},"created_at":"2022-12-01T15:59:25.000Z","updated_at":"2023-06-24T02:21:20.000Z","dependencies_parsed_at":"2024-11-17T04:01:33.910Z","dependency_job_id":null,"html_url":"https://github.com/ThoughtStuff/ThoughtStuff.Caching","commit_stats":{"total_commits":48,"total_committers":1,"mean_commits":48.0,"dds":0.0,"last_synced_commit":"13bfb787ce055b42c280a92a716b29364fcf9f9b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.Caching","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.Caching/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.Caching/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThoughtStuff%2FThoughtStuff.Caching/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ThoughtStuff","download_url":"https://codeload.github.com/ThoughtStuff/ThoughtStuff.Caching/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250395253,"owners_count":21423395,"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","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","dotnet","memcache"],"created_at":"2024-11-17T02:22:51.708Z","updated_at":"2025-04-23T08:01:30.046Z","avatar_url":"https://github.com/ThoughtStuff.png","language":"C#","readme":"# ThoughtStuff.Caching\n\nInjects look-through cache for configured services\n\n## Quick Start\n\n```cs\n// Cache results returned from MySlowService in Mem Cache\nservices.AddMethodCaching()\n        .AddTransientWithCaching\u003cIMySlowService, MySlowService, MyResult\u003e();\n\nvar app = builder.Build();\n\n// Configure method caching policies\nvar methodCachePolicies = app.Services.GetRequiredService\u003cIMethodCacheOptionsLookup\u003e();\nmethodCachePolicies.AddRelativeExpiration\u003cIMySlowService\u003e(TimeSpan.FromSeconds(30));\n```\n\n## Details\n\nSuppose you have a slow service that fetches information on books by ISBN.\n\n```cs\npublic interface IBookInfoService\n{\n    BookInfo GetBookInfo(string isbn);\n}\n\nclass BookInfoService : IBookInfoService\n{\n    public BookInfo GetBookInfo(string isbn) { ... }\n}\n```\n\nThe service is slow and book information changes infrequently, so you would like to cache it.\nRather than registering `BookInfoService` with `.AddTransient` you can use `.AddTransientWithCaching`.\n\n```cs\nservices.AddMethodCaching()\n        .AddTransientWithCaching\u003cIBookInfoService, BookInfoService, BookInfo\u003e();\n```\n\nThe above will register a _caching proxy_ for `IBookInfoService` which will wrap `BookInfoService`. \nThe first call to get info on a particular book will use `BookInfoService`,\nbut subsequent calls to get info on the same book will return the cached result from the first call.\n\n```cs\nvoid Example([Inject] IBookInfoService bookInfoService)\n{\n    var info1 = bookInfoService.GetBookInfo(\"1234\");    // Slow. Saves cache entry using key \"GetBookInfo(1234)\"\n    var info2 = bookInfoService.GetBookInfo(\"1234\");    // Fast. Returns cache entry found using key \"GetBookInfo(1234)\"\n    Assert.Equals(info1, info2);\n}\n```\n\n## Current Limitations\n\n### Single Return Type per Interface\n\nOnly one return type can be cached per service interface. \nThis works well for services that implement a Single Responsibility, but not for larger classes. \nOften times simplified interface(s) can be created. \nThe simplified interfaces may only have 1 function each and all be implemented by the large class.\n\n### Transient Lifetime\n\nOnly the `Transient` lifetime is implemented.\n\n### Cache Key uses Interface Name + Method Name + Arguments\n\nA cache key is automatically generated from the method invocation based on the method name and passed arguments.\nFor example: `\"IMyBookService.GetBookInfo('The Great Gatsby')\"`.\nOther environment information is not included.\n\nBe careful in multi-user or multi-tenant environments not to cache user-specific or tenant-specific results.\nFor example, caching a method like `GetCurrentUserFavoriteColor()` would end up returning the same color for all users.\nTo make it amenable to method caching change it to `GetUserFavoriteColor(userId)`.\n\n---\n\n\u003e The programmer, like the poet, works only slightly removed from pure **thought-stuff**. \n\u003e He builds his castles in the air, from air, creating by exertion of the imagination. \n\u003e Few media of creation are so flexible, so easy to polish and rework, \n\u003e so readily capable of realizing grand conceptual structures.\n\n\u0026mdash; Fred P. Brooks\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthoughtstuff%2Fthoughtstuff.caching","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthoughtstuff%2Fthoughtstuff.caching","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthoughtstuff%2Fthoughtstuff.caching/lists"}