nancy-rapid-cache - Nancy.RapidCache.Couchbase 0.4.2
This library adds Couchbase support as a store for RapidCache
PM> Install-Package Nancy.RapidCache.Couchbase -Version 0.4.2 -Source https://www.myget.org/F/nancy-rapid-cache/api/v3/index.json
> nuget.exe install Nancy.RapidCache.Couchbase -Version 0.4.2 -Source https://www.myget.org/F/nancy-rapid-cache/api/v3/index.json
> dotnet add package Nancy.RapidCache.Couchbase --version 0.4.2 --source https://www.myget.org/F/nancy-rapid-cache/api/v3/index.json
source https://www.myget.org/F/nancy-rapid-cache/api/v3/index.json
nuget Nancy.RapidCache.Couchbase ~> 0.4.2
Copy to clipboard
> choco install Nancy.RapidCache.Couchbase --version 0.4.2 --source https://www.myget.org/F/nancy-rapid-cache/api/v2
Import-Module PowerShellGet
Register-PSRepository -Name "nancy-rapid-cache" -SourceLocation "https://www.myget.org/F/nancy-rapid-cache/api/v2"
Install-Module -Name "Nancy.RapidCache.Couchbase" -RequiredVersion "0.4.2" -Repository "nancy-rapid-cache"
Copy to clipboard
Nancy.RapidCache 
Cache 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.
Note: Since Nancyfx is no longer being maintained i will be deprioritizing this library, please check here for details
Builds
This library targets Nancyfx 2 and above.
| Appveyor | Branch | Coverage |
|---|---|---|
| master | ||
| develop |
Packages
| Package | NuGet (Stable) | MyGet (Prerelease) |
|---|---|---|
| Nancy.RapidCache | ||
| Nancy.RapidCache.Redis | ||
| Nancy.RapidCache.Couchbase | ||
| Nancy.RapidCache.IMemory |
Installation
Install via nuget https://nuget.org/packages/Nancy.RapidCache
PM> Install-Package Nancy.RapidCache
Sample usage
The following example is using the default "In-Memory" CacheStore which is nothing more than a concurrent dictionary.
- Add to your Nancy bootstrapper
using Nancy.RapidCache.Extensions;
using Nancy.Routing;
namespace WebApplication
{
public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
/* Enable rapidcache, vary by url, query, form and accept header */
this.EnableRapidCache(container.Resolve<IRouteResolver>(), ApplicationPipelines, new[] { "query", "form", "accept" });
}
}
}
- Enable caching by adding the "AsCacheable" extension method to any of your routes
using System;
using Nancy;
using Nancy.RapidCache.Extensions;
namespace WebApplication
{
public class ExampleModule : NancyModule
{
public ExampleModule()
{
Get("/", _ =>
{
/* cache view for 30 secs */
return View["hello.html"].AsCacheable(DateTime.Now.AddSeconds(30));
});
Get("/CachedResponse", _ =>
{
/* cache response for 30 secs */
return Response.AsText("hello").AsCacheable(DateTime.Now.AddSeconds(30));
});
}
}
}
Or alternatively, set the Cache on all requests by using an after request on the start of the pipeline.
using Nancy.RapidCache.Extensions;
using Nancy.Routing;
namespace WebApplication
{
public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
/* Enable RapidCache, vary by url params query, form and accept headers */
this.EnableRapidCache(container.Resolve<IRouteResolver>(), ApplicationPipelines, new[] { "query", "form", "accept" });
pipelines.AfterRequest.AddItemToStartOfPipeline(ConfigureCache);
}
public void ConfigureCache(NancyContext context)
{
/* Cache all responses by 30 seconds with status code OK */
context.Response = context.Response.AsCacheable(DateTime.Now.AddSeconds(30));
}
}
}
*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:
- Query
- Form values
- Accept Headers
along with the url that identifies your resource (this is using the DefaultKeyGenerator).
Using Different Cache Stores
DiskCacheStore
When using Nancy in self hosting mode, you can use the DiskCacheStore to enable caching throught RapidCache to a tmp file.
using Nancy.RapidCache.CacheStore;
using Nancy.RapidCache.Extensions;
using Nancy.Routing;
namespace WebApplication
{
public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
/* Enable RapidCache using the DiskCacheStore, vary by url params id,query,takem, skip and accept header */
this.EnableRapidCache(container.Resolve<IRouteResolver>(), ApplicationPipelines, new[] { "query", "form", "accept" }, new DiskCacheStore("c:/tmp/cache"));
}
}
}
IMemoryCacheStore
One 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:
PM> Install-Package Nancy.RapidCache.IMemory
the difference being that the store uses the Microsoft.Extensions.Caching.Memory as a dependency. The declaration is the same, simply replacing the previous one:
namespace WebApplication
{
public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
/* Enable cache using the IMemoryCacheStore, which uses the MsCaching Memory library, using the same key variations */
this.EnableRapidCache(container.Resolve<IRouteResolver>(), ApplicationPipelines, new[] { "query", "form", "accept" }, new IMemoryCacheStore());
}
}
}
RedisCacheStore
RapidCache 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:
PM> Install-Package Nancy.RapidCache.Redis
the usage is similar to the other stores:
using Nancy.RapidCache.CacheStore;
using Nancy.RapidCache.Extensions;
using Nancy.Routing;
namespace WebApplication
{
public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
/* Enable RapidCache using the RedisCacheStore, vary by url params id,query,takem, skip and accept header */
this.EnableRapidCache(container.Resolve<IRouteResolver>(), ApplicationPipelines, new[] { "query", "form", "accept" }, new RedisCacheStore("localhost"));
}
}
}
CouchbaseCacheStore
RapidCache 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:
PM> Install-Package Nancy.RapidCache.Couchbase
Once installed, we need to configure the cluster first, and then pass the cluster to the constructor:
using Nancy.RapidCache.CacheStore;
using Nancy.RapidCache.Extensions;
using Nancy.Routing;
namespace WebApplication
{
public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
var cluster = new Cluster(new ClientConfiguration
{
Servers = new List<Uri> { new Uri("http://127.0.0.1") }
});
var authenticator = new PasswordAuthenticator("user", "password");
cluster.Authenticate(authenticator);
/* Enable RapidCache using the CouchbaseCacheStore, vary by url params id,query,takem, skip and accept header */
/* Provide a bucket on the configuration */
this.EnableRapidCache(container.Resolve<IRouteResolver>(), ApplicationPipelines, new[] { "query", "form", "accept" }, new CouchbaseCacheStore(cluster, "myBucket")));
}
}
}
Definining your own cache key generation using ICacheKeyGenerator
Define your own key per resolver that will help you cache to the level of granulatity you need.
using System;
using System.Text;
using Nancy;
using Nancy.RapidCache.Extensions;
using Nancy.Routing;
namespace WebApplication
{
public class ApplicationBootrapper : Nancy.DefaultNancyBootstrapper
{
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
this.EnableRapidCache(container.Resolve<IRouteResolver>(), ApplicationPipelines, new UrlHashKeyGenerator());
}
public class UrlHashKeyGenerator : Nancy.RapidCache.CacheKey.ICacheKeyGenerator
{
public string Get(Request request)
{
using (var md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(request.Url.ToString()));
return Convert.ToBase64String(hash);
}
}
}
}
}
For more details please check the documentation.
Upgrade to target Nancy 2.0
-
.NETFramework 4.5.2
- CouchbaseNetClient (>= 2.7.16)
- Nancy.RapidCache (>= 0.4.2)
-
.NETStandard 2.0
- CouchbaseNetClient (>= 2.7.16)
- Nancy.RapidCache (>= 0.4.2)
- .NETFramework 4.5.2: 4.5.2.0
- .NETStandard 2.0: 2.0.0.0
OwnersJaxelr |
AuthorsJaxel Rojas |
Project URLhttps://github.com/Jaxelr/Nancy.RapidCache |
LicenseMIT |
Tagscache nancyfx couchbase |
Info374 total downloads |
| 85 downloads for version 0.4.2 |
| Download (13.15 KB) |
| Found on the current feed only |
Package history
| Version | Size | Last updated | Downloads | Mirrored? | |||
|---|---|---|---|---|---|---|---|
|
|
0.4.2 | 13.15 KB | Thu, 20 Feb 2020 15:46:00 GMT | 85 |
|
||
|
|
0.4.1 | 10.55 KB | Fri, 31 May 2019 11:06:13 GMT | 68 |
|
||
|
|
0.4.0 | 10.54 KB | Fri, 31 May 2019 04:18:34 GMT | 74 |
|
||
|
|
0.3.2 | 10.53 KB | Thu, 02 May 2019 11:26:11 GMT | 67 |
|
||
|
|
0.3.1 | 10.57 KB | Wed, 30 Jan 2019 11:54:13 GMT | 80 |
|