{"id":17182345,"url":"https://github.com/simoncropp/replicant","last_synced_at":"2025-04-13T16:12:29.141Z","repository":{"id":42576044,"uuid":"341836189","full_name":"SimonCropp/Replicant","owner":"SimonCropp","description":"A wrapper for HttpClient that caches to disk. Cached files, over the max specified, are deleted based on the last access times.","archived":false,"fork":false,"pushed_at":"2025-04-09T00:44:14.000Z","size":877,"stargazers_count":533,"open_issues_count":3,"forks_count":14,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-13T16:12:20.862Z","etag":null,"topics":["cache","httpcache","httpclient"],"latest_commit_sha":null,"homepage":"","language":"C#","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/SimonCropp.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"license.txt","code_of_conduct":"code_of_conduct.md","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},"funding":{"github":"SimonCropp"}},"created_at":"2021-02-24T08:50:38.000Z","updated_at":"2025-04-11T16:54:16.000Z","dependencies_parsed_at":"2024-03-07T11:42:19.884Z","dependency_job_id":"e5b47a82-88a2-4d5e-9c3a-29066993b8d6","html_url":"https://github.com/SimonCropp/Replicant","commit_stats":{"total_commits":618,"total_committers":4,"mean_commits":154.5,"dds":0.3268608414239482,"last_synced_commit":"1f00a8df978fe202e8b238b6285ee8ad4ec6ce8f"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonCropp%2FReplicant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonCropp%2FReplicant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonCropp%2FReplicant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonCropp%2FReplicant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SimonCropp","download_url":"https://codeload.github.com/SimonCropp/Replicant/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741195,"owners_count":21154255,"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","httpcache","httpclient"],"created_at":"2024-10-15T00:36:51.050Z","updated_at":"2025-04-13T16:12:29.135Z","avatar_url":"https://github.com/SimonCropp.png","language":"C#","readme":"# \u003cimg src=\"/src/icon.png\" height=\"30px\"\u003e Replicant\n\n[![Build status](https://ci.appveyor.com/api/projects/status/2t806jcx34s3r796/branch/main?svg=true)](https://ci.appveyor.com/project/SimonCropp/Replicant)\n[![NuGet Status](https://img.shields.io/nuget/v/Replicant.svg)](https://www.nuget.org/packages/Replicant/)\n\nA wrapper for HttpClient that caches to disk. Cached files, over the max specified, are deleted based on the last access times.\n\n**See [Milestones](../../milestones?state=closed) for release notes.**\n\nHeaders/Responses respected in caching decisions:\n\n * [Expires](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires)\n * [Cache-Control max-age](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#expiration)\n * [Cache-Control no-store](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#cacheability)\n * [Cache-Control no-cache](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#cacheability)\n * [Last-Modified](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified)\n * [ETag](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)\n\n\n## NuGet package\n\nhttps://nuget.org/packages/Replicant/\n\n\n## Usage\n\n\n### Default instance\n\nThere is a default static instance:\n\n\u003c!-- snippet: DefaultInstance --\u003e\n\u003ca id='snippet-DefaultInstance'\u003e\u003c/a\u003e\n```cs\nvar content = await HttpCache.Default.DownloadAsync(\"https://httpbin.org/status/200\");\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L94-L98' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-DefaultInstance' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\nThis caches to `{Temp}/Replicant`.\n\n\n### Construction\n\nAn instance of HttpCache should be long running.\n\n\u003c!-- snippet: Construction --\u003e\n\u003ca id='snippet-Construction'\u003e\u003c/a\u003e\n```cs\nvar httpCache = new HttpCache(\n    cacheDirectory,\n    // omit for default new HttpClient()\n    new HttpClient\n    {\n        Timeout = TimeSpan.FromSeconds(30)\n    },\n    // omit for the default of 1000\n    maxEntries: 10000);\n\n// Dispose when finished\nawait httpCache.DisposeAsync();\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L36-L51' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-Construction' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Dependency injection\n\nAdd HttpCache as a singleton when using dependency injection.\n\n\u003c!-- snippet: DependencyInjection --\u003e\n\u003ca id='snippet-DependencyInjection'\u003e\u003c/a\u003e\n```cs\nvar services = new ServiceCollection();\nservices.AddSingleton(_ =\u003e new HttpCache(cachePath));\n\nusing var provider = services.BuildServiceProvider();\nvar httpCache = provider.GetRequiredService\u003cHttpCache\u003e();\nClassicAssert.NotNull(httpCache);\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L57-L66' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-DependencyInjection' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\nUsing HttpClient with [HttpClientFactory](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests).\n\n\u003c!-- snippet: DependencyInjectionWithHttpFactory --\u003e\n\u003ca id='snippet-DependencyInjectionWithHttpFactory'\u003e\u003c/a\u003e\n```cs\nServiceCollection services = new();\nservices.AddHttpClient();\nservices.AddSingleton(\n    _ =\u003e\n    {\n        var clientFactory = _.GetRequiredService\u003cIHttpClientFactory\u003e();\n        return new HttpCache(cachePath, () =\u003e clientFactory.CreateClient());\n    });\n\nusing var provider = services.BuildServiceProvider();\nvar httpCache = provider.GetRequiredService\u003cHttpCache\u003e();\nClassicAssert.NotNull(httpCache);\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L72-L87' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-DependencyInjectionWithHttpFactory' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Get a string\n\n\u003c!-- snippet: string --\u003e\n\u003ca id='snippet-string'\u003e\u003c/a\u003e\n```cs\nvar content = await httpCache.StringAsync(\"https://httpbin.org/json\");\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L260-L264' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-string' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003ca id='snippet-string-1'\u003e\u003c/a\u003e\n```cs\nvar lines = new List\u003cstring\u003e();\nawait foreach (var line in httpCache.LinesAsync(\"https://httpbin.org/json\"))\n{\n    lines.Add(line);\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L272-L280' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-string-1' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Get bytes\n\n\u003c!-- snippet: bytes --\u003e\n\u003ca id='snippet-bytes'\u003e\u003c/a\u003e\n```cs\nvar bytes = await httpCache.BytesAsync(\"https://httpbin.org/json\");\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L288-L292' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-bytes' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Get a stream\n\n\u003c!-- snippet: stream --\u003e\n\u003ca id='snippet-stream'\u003e\u003c/a\u003e\n```cs\nusing var stream = await httpCache.StreamAsync(\"https://httpbin.org/json\");\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L300-L304' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-stream' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Download to a file\n\n\u003c!-- snippet: ToFile --\u003e\n\u003ca id='snippet-ToFile'\u003e\u003c/a\u003e\n```cs\nawait httpCache.ToFileAsync(\"https://httpbin.org/json\", targetFile);\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L315-L319' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-ToFile' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Download to a stream\n\n\u003c!-- snippet: ToStream --\u003e\n\u003ca id='snippet-ToStream'\u003e\u003c/a\u003e\n```cs\nawait httpCache.ToStreamAsync(\"https://httpbin.org/json\", targetStream);\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L334-L338' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-ToStream' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Manually add an item to the cache\n\n\u003c!-- snippet: AddItem --\u003e\n\u003ca id='snippet-AddItem'\u003e\u003c/a\u003e\n```cs\nusing var response = new HttpResponseMessage(HttpStatusCode.OK)\n{\n    Content = new StringContent(\"the content\")\n};\nawait httpCache.AddItemAsync(uri, response);\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L391-L399' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-AddItem' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Use stale item on error\n\nIf an error occurs when re-validating a potentially stale item, then the cached item can be used as a fallback.\n\n\u003c!-- snippet: staleIfError --\u003e\n\u003ca id='snippet-staleIfError'\u003e\u003c/a\u003e\n```cs\nvar content = httpCache.StringAsync(uri, staleIfError: true);\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L436-L440' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-staleIfError' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Customizing HttpRequestMessage\n\nThe HttpRequestMessage used can be customized using a callback.\n\n\u003c!-- snippet: ModifyRequest --\u003e\n\u003ca id='snippet-ModifyRequest'\u003e\u003c/a\u003e\n```cs\nvar content = await httpCache.StringAsync(\n    uri,\n    modifyRequest: message =\u003e\n    {\n        message.Headers.Add(\"Key1\", \"Value1\");\n        message.Headers.Add(\"Key2\", \"Value2\");\n    });\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L348-L358' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-ModifyRequest' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Full HttpResponseMessage\n\nAn instance of the HttpResponseMessage can be created from a cached item:\n\n\u003c!-- snippet: FullHttpResponseMessage --\u003e\n\u003ca id='snippet-FullHttpResponseMessage'\u003e\u003c/a\u003e\n```cs\nusing var response = await httpCache.ResponseAsync(\"https://httpbin.org/status/200\");\n```\n\u003csup\u003e\u003ca href='/src/Tests/HttpCacheTests.cs#L181-L185' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-FullHttpResponseMessage' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n## Influences / Alternatives\n\n * [Tavis.HttpCache](https://github.com/tavis-software/Tavis.HttpCache)\n * [CacheCow](https://github.com/aliostad/CacheCow)\n * [Monkey Cache](https://github.com/jamesmontemagno/monkey-cache)\n\n\n## Icon\n\n[Cyborg](https://thenounproject.com/term/cyborg/689871/) designed by [Symbolon](https://thenounproject.com/symbolon/) from [The Noun Project](https://thenounproject.com).\n","funding_links":["https://github.com/sponsors/SimonCropp"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimoncropp%2Freplicant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimoncropp%2Freplicant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimoncropp%2Freplicant/lists"}