{"id":19241968,"url":"https://github.com/jaxelr/nancy.rapidcache","last_synced_at":"2025-04-21T09:32:32.558Z","repository":{"id":85526955,"uuid":"127538148","full_name":"Jaxelr/Nancy.RapidCache","owner":"Jaxelr","description":"A library to asynchronously cache responses on NancyFx Modules.","archived":false,"fork":false,"pushed_at":"2024-08-05T13:01:34.000Z","size":382,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-16T03:08:06.225Z","etag":null,"topics":["cache","couchbase","nancyfx","redis"],"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/Jaxelr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","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":"2018-03-31T14:15:13.000Z","updated_at":"2024-08-05T13:01:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"8baa76e3-c7c2-41db-95e0-2346c850a60a","html_url":"https://github.com/Jaxelr/Nancy.RapidCache","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaxelr%2FNancy.RapidCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaxelr%2FNancy.RapidCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaxelr%2FNancy.RapidCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaxelr%2FNancy.RapidCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jaxelr","download_url":"https://codeload.github.com/Jaxelr/Nancy.RapidCache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250032167,"owners_count":21363785,"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","couchbase","nancyfx","redis"],"created_at":"2024-11-09T17:13:03.351Z","updated_at":"2025-04-21T09:32:32.246Z","avatar_url":"https://github.com/Jaxelr.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nancy.RapidCache [![Mit License][mit-img]][mit]\n\nCache content asynchronously inside NancyFx. Allows you to set a timespan of lifecycle for your cache. Also allows to redefine the desired Cache backend and desires key request. By default it provides a default memory cache and a default uri key.\n\n__Note:__ Since Nancyfx is no longer being maintained i will be deprioritizing this library, please check [here for details](https://github.com/NancyFx/Nancy/issues/3010)\n\n## Builds\n\nThis library targets Nancyfx 2 and above.\n\n| Appveyor  | Branch | Coverage |\n| :---:     | :---: | :--: |\n| [![Build status][build-master-img]][build-master] | master | [![CodeCov][codecov-master-img]][codecov-master] |\n| [![Build status][build-develop-img]][build-develop] | develop | [![CodeCov][codecov-develop-img]][codecov-develop] |\n\n## Packages\n\nPackage | NuGet (Stable) | MyGet (Prerelease)\n| :--- | :---: | :---: |\n| Nancy.RapidCache | [![NuGet][nuget-rapid-img]][nuget-rapid] | [![MyGet][myget-rapid-img]][myget-rapid] |\n| Nancy.RapidCache.Redis | [![NuGet][nuget-redis-img]][nuget-redis] | [![MyGet][myget-redis-img]][myget-redis] |\n| Nancy.RapidCache.Couchbase | [![NuGet][nuget-couchbase-img]][nuget-couchbase] | [![MyGet][myget-couchbase-img]][myget-couchbase] |\n| Nancy.RapidCache.IMemory | [![NuGet][nuget-memory-img]][nuget-memory] | [![MyGet][myget-memory-img]][myget-memory] |\n\n## Installation\n\nInstall via nuget https://nuget.org/packages/Nancy.RapidCache\n\n```powershell\nPM\u003e Install-Package Nancy.RapidCache\n```\n\n## Sample usage\n\nThe following example is using the default \"In-Memory\" CacheStore which is nothing more than a concurrent dictionary.\n\n1. Add to your Nancy bootstrapper\n\n```c#\nusing Nancy.RapidCache.Extensions;\nusing Nancy.Routing;\n\nnamespace WebApplication\n{\n    public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper\n    {\n        protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)\n        {\n            base.ApplicationStartup(container, pipelines);\n            /* Enable rapidcache, vary by url, query, form and accept header */\n            this.EnableRapidCache(container.Resolve\u003cIRouteResolver\u003e(), ApplicationPipelines, new[] { \"query\", \"form\", \"accept\" });\n        }\n    }\n}\n```\n\n1. Enable caching by adding the \"AsCacheable\" extension method to any of your routes\n\n```c#\nusing System;\nusing Nancy;\nusing Nancy.RapidCache.Extensions;\n\nnamespace WebApplication\n{\n    public class ExampleModule : NancyModule\n    {\n        public ExampleModule()\n        {\n            Get(\"/\", _ =\u003e\n            {\n                /* cache view for 30 secs */\n                return View[\"hello.html\"].AsCacheable(DateTime.Now.AddSeconds(30));\n            });\n\n            Get(\"/CachedResponse\", _ =\u003e\n            {\n                /* cache response for 30 secs */\n                return Response.AsText(\"hello\").AsCacheable(DateTime.Now.AddSeconds(30));\n            });\n        }\n    }\n}\n```\n\nOr alternatively, set the Cache on all requests by using an after request on the start of the pipeline.\n\n```c#\nusing Nancy.RapidCache.Extensions;\nusing Nancy.Routing;\n\nnamespace WebApplication\n{\n    public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper\n    {\n        protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)\n        {\n            base.ApplicationStartup(container, pipelines);\n            /* Enable RapidCache, vary by url params query, form and accept headers */\n            this.EnableRapidCache(container.Resolve\u003cIRouteResolver\u003e(), ApplicationPipelines, new[] { \"query\", \"form\", \"accept\" });\n            pipelines.AfterRequest.AddItemToStartOfPipeline(ConfigureCache);\n        }\n\n        public void ConfigureCache(NancyContext context)\n        {\n            /* Cache all responses by 30 seconds with status code OK */\n            context.Response = context.Response.AsCacheable(DateTime.Now.AddSeconds(30));\n        }\n    }\n}\n```\n\n *Keep in mind that for *Post* methods, the requests body is __NOT__ part of the key in this scenario. In context, you can filter by the values of:\n\n* Query\n* Form values\n* Accept Headers\n\n along with the url that identifies your resource (this is using the DefaultKeyGenerator). \n\n## Using Different Cache Stores\n\n### DiskCacheStore\n\nWhen using Nancy in self hosting mode, you can use the DiskCacheStore to enable caching throught RapidCache to a tmp file.\n\n```c#\nusing Nancy.RapidCache.CacheStore;\nusing Nancy.RapidCache.Extensions;\nusing Nancy.Routing;\n\nnamespace WebApplication\n{\n    public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper\n    {\n        protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)\n        {\n            base.ApplicationStartup(container, pipelines);\n            /* Enable RapidCache using the DiskCacheStore, vary by url params id,query,takem, skip and accept header */\n            this.EnableRapidCache(container.Resolve\u003cIRouteResolver\u003e(), ApplicationPipelines, new[] { \"query\", \"form\", \"accept\" }, new DiskCacheStore(\"c:/tmp/cache\"));\n        }\n    }\n}\n```\n\n### IMemoryCacheStore\n\nOne of the lightweight alternatives to the MemoryCacheStore which is included as part of the RapidCache library is the external IMemoryCacheStore, which requires a separate nuget lib installation:\n\n``` powershell\nPM\u003e Install-Package Nancy.RapidCache.IMemory\n```\n\nthe difference being that the store uses the `Microsoft.Extensions.Caching.Memory` as a dependency. The declaration is the same, simply replacing the previous one:\n\n```c#\nnamespace WebApplication\n{\n    public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper\n    {\n        protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)\n        {\n            base.ApplicationStartup(container, pipelines);\n            /* Enable cache using the IMemoryCacheStore, which uses the MsCaching Memory library, using the same key variations */\n            this.EnableRapidCache(container.Resolve\u003cIRouteResolver\u003e(), ApplicationPipelines, new[] { \"query\", \"form\", \"accept\" }, new IMemoryCacheStore());\n        }\n    }\n}\n```\n\n### RedisCacheStore\n\nRapidCache provides a small lib for integration with Redis, given that you provide a valid connection. It is provided as a separate package, so install it first via nuget:\n\n``` powershell\nPM\u003e Install-Package Nancy.RapidCache.Redis\n```\n\nthe usage is similar to the other stores:\n\n```c#\nusing Nancy.RapidCache.CacheStore;\nusing Nancy.RapidCache.Extensions;\nusing Nancy.Routing;\n\nnamespace WebApplication\n{\n    public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper\n    {\n        protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)\n        {\n            base.ApplicationStartup(container, pipelines);\n            /* Enable RapidCache using the RedisCacheStore, vary by url params id,query,takem, skip and accept header */\n            this.EnableRapidCache(container.Resolve\u003cIRouteResolver\u003e(), ApplicationPipelines, new[] { \"query\", \"form\", \"accept\" }, new RedisCacheStore(\"localhost\"));\n        }\n    }\n}\n```\n\n### CouchbaseCacheStore\n\nRapidCache also provides a small lib for a integration with Couchbase, given that you provide a valid Cluster object from the CouchbaseNetClient package and a bucket name that can be located. Install it via nuget:\n\n``` powershell\nPM\u003e Install-Package Nancy.RapidCache.Couchbase\n```\n\nOnce installed, we need to configure the cluster first, and then pass the cluster to the constructor:\n\n```c#\nusing Nancy.RapidCache.CacheStore;\nusing Nancy.RapidCache.Extensions;\nusing Nancy.Routing;\n\nnamespace WebApplication\n{\n    public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper\n    {\n        protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)\n        {\n            base.ApplicationStartup(container, pipelines);\n\n            var cluster = new Cluster(new ClientConfiguration\n            {\n                Servers = new List\u003cUri\u003e { new Uri(\"http://127.0.0.1\") }\n            });\n\n            var authenticator = new PasswordAuthenticator(\"user\", \"password\");\n            cluster.Authenticate(authenticator);\n\n            /* Enable RapidCache using the CouchbaseCacheStore, vary by url params id,query,takem, skip and accept header */\n            /* Provide a bucket on the configuration */\n            this.EnableRapidCache(container.Resolve\u003cIRouteResolver\u003e(), ApplicationPipelines, new[] { \"query\", \"form\", \"accept\" }, new CouchbaseCacheStore(cluster, \"myBucket\")));\n        }\n    }\n}\n```\n\n## Definining your own cache key generation using ICacheKeyGenerator\n\nDefine your own key per resolver that will help you cache to the level of granulatity you need. \n\n```c#\nusing System;\nusing System.Text;\nusing Nancy;\nusing Nancy.RapidCache.Extensions;\nusing Nancy.Routing;\n\nnamespace WebApplication\n{\n    public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper\n    {\n        protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)\n        {\n            base.ApplicationStartup(container, pipelines);\n            this.EnableRapidCache(container.Resolve\u003cIRouteResolver\u003e(), ApplicationPipelines, new UrlHashKeyGenerator());\n        }\n\n        public class UrlHashKeyGenerator : Nancy.RapidCache.CacheKey.ICacheKeyGenerator\n        {\n            public string Get(Request request)\n            {\n                using (var md5 = MD5.Create())\n                {\n                    byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(request.Url.ToString()));\n                    return Convert.ToBase64String(hash);\n                }\n            }\n        }\n    }\n}\n```\n\nFor more details please check the [documentation][wiki].\n\n[mit-img]: http://img.shields.io/badge/License-MIT-blue.svg\n[mit]: https://github.com/Jaxelr/Nancy.RapidCache/blob/master/LICENSE\n[build-master-img]: https://ci.appveyor.com/api/projects/status/3cfeq9e3lh4edbcg/branch/master?svg=true\n[build-master]: https://ci.appveyor.com/project/Jaxelr/nancy-rapidcache/branch/master\n[build-develop-img]: https://ci.appveyor.com/api/projects/status/3cfeq9e3lh4edbcg/branch/develop?svg=true\n[build-develop]: https://ci.appveyor.com/project/Jaxelr/nancy-rapidcache/branch/develop\n[codecov-master-img]: https://codecov.io/gh/Jaxelr/Nancy.RapidCache/branch/master/graph/badge.svg\n[codecov-master]: https://codecov.io/gh/Jaxelr/Nancy.RapidCache/branch/master\n[codecov-develop-img]: https://codecov.io/gh/Jaxelr/Nancy.RapidCache/branch/develop/graph/badge.svg\n[codecov-develop]: https://codecov.io/gh/Jaxelr/Nancy.RapidCache/branch/develop\n[nuget-rapid-img]: https://img.shields.io/nuget/v/Nancy.RapidCache.svg\n[nuget-rapid]: https://www.nuget.org/packages/Nancy.RapidCache\n[myget-rapid-img]: https://img.shields.io/myget/nancy-rapid-cache/v/Nancy.RapidCache.svg\n[myget-rapid]: https://www.myget.org/feed/nancy-rapid-cache/package/nuget/Nancy.RapidCache\n[nuget-redis-img]: https://img.shields.io/nuget/v/Nancy.RapidCache.Redis.svg\n[nuget-redis]: https://www.nuget.org/packages/Nancy.RapidCache.Redis\n[myget-redis-img]: https://img.shields.io/myget/nancy-rapid-cache/v/Nancy.RapidCache.Redis.svg\n[myget-redis]: https://www.myget.org/feed/nancy-rapid-cache/package/nuget/Nancy.RapidCache.Redis\n[nuget-couchbase-img]: https://img.shields.io/nuget/v/Nancy.RapidCache.Couchbase.svg\n[nuget-couchbase]: https://www.nuget.org/packages/Nancy.RapidCache.Couchbase\n[myget-couchbase-img]: https://img.shields.io/myget/nancy-rapid-cache/v/Nancy.RapidCache.Couchbase.svg\n[myget-couchbase]: https://www.myget.org/feed/nancy-rapid-cache/package/nuget/Nancy.RapidCache.Couchbase\n[nuget-memory-img]: https://img.shields.io/nuget/v/Nancy.RapidCache.IMemory.svg\n[nuget-memory]: https://www.nuget.org/packages/Nancy.RapidCache.IMemory\n[myget-memory-img]: https://img.shields.io/myget/nancy-rapid-cache/v/Nancy.RapidCache.IMemory.svg\n[myget-memory]: https://www.myget.org/feed/nancy-rapid-cache/package/nuget/Nancy.RapidCache.IMemory\n[wiki]: https://github.com/Jaxelr/Nancy.RapidCache/wiki\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaxelr%2Fnancy.rapidcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaxelr%2Fnancy.rapidcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaxelr%2Fnancy.rapidcache/lists"}