Couchbase.Extensions.Caching 1.0.0

Session Middleware for Couchbase and .NET Core

A custom ASP.NET Core Middleware plugin for distributed session state using Couchbase server as the backing store. Supports both Memcached (in-memory) and Couchbase (persistent) buckets.

Install-Package Couchbase.Extensions.Caching -Version 1.0.0
dotnet add package Couchbase.Extensions.Caching --version 1.0.0
paket add Couchbase.Extensions.Caching --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Couchbase Distributed Cache for ASP.NET Core

A custom ASP.NET Core Middleware plugin for a distributed cache using Couchbase server as the backing store. Supports both Memcached (in-memory) and Couchbase (persistent) buckets.

Getting Started

Assuming you have an installation of Couchbase Server and Visual Studio (examples with VSCODE forthcoming), do the following:

Couchbase .NET Core Distributed Cache:

  • Create a .NET Core Web Application using Visual Studio or VsCodeor CIL
  • Install the package from NuGet or build from source and add reference

Setup

In Setup.cs add the following to the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

     services.AddCouchbase(opt =>
        {
            opt.Servers = new List<Uri>
            {
                new Uri("http://10.111.150.101:8091")
            };
        });

        services.AddDistributedCouchbaseCache("default", opt => { });
}

You can change the localhost hostname to wherever you are hosting your Couchbase cluster.

In your controller add a parameter for IDistributedCache to the constructor:

 public class HomeController : Controller
 {
	private IDistributedCache _cache;

    public HomeController(IDistributedCache cache)
    {
    	_cache = cache;
    }
 }

Tear down

There are a couple different ways to free up the resources (TCP sockets, etc) opened by the Couchbase ICluster and IBucket used by the Distributed Session. Here is one simple way to tap into the ApplicationStopped cancellation token:

public void ConfigureServices(IServiceCollection services)
{
	...

 	applicationLifetime.ApplicationStopped.Register(() =>
    {
        app.ApplicationServices.GetRequiredService<ICouchbaseLifetimeService>().Close();
    });
}

Using Caching in your Controllers

Add the following code to HomeController:

public IActionResult Index()
{
	_cache.Set("CacheTime", System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()));
	return View();
}

public IActionResult About()
{
	ViewData["Message"] = "Your application description page. "
				+ System.Text.Encoding.UTF8.GetString(_cache.Get("CacheTime"));
	return View();
}

Couchbase Distributed Cache for ASP.NET Core

A custom ASP.NET Core Middleware plugin for a distributed cache using Couchbase server as the backing store. Supports both Memcached (in-memory) and Couchbase (persistent) buckets.

Getting Started

Assuming you have an installation of Couchbase Server and Visual Studio (examples with VSCODE forthcoming), do the following:

Couchbase .NET Core Distributed Cache:

  • Create a .NET Core Web Application using Visual Studio or VsCodeor CIL
  • Install the package from NuGet or build from source and add reference

Setup

In Setup.cs add the following to the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

     services.AddCouchbase(opt =>
        {
            opt.Servers = new List<Uri>
            {
                new Uri("http://10.111.150.101:8091")
            };
        });

        services.AddDistributedCouchbaseCache("default", opt => { });
}

You can change the localhost hostname to wherever you are hosting your Couchbase cluster.

In your controller add a parameter for IDistributedCache to the constructor:

 public class HomeController : Controller
 {
	private IDistributedCache _cache;

    public HomeController(IDistributedCache cache)
    {
    	_cache = cache;
    }
 }

Tear down

There are a couple different ways to free up the resources (TCP sockets, etc) opened by the Couchbase ICluster and IBucket used by the Distributed Session. Here is one simple way to tap into the ApplicationStopped cancellation token:

public void ConfigureServices(IServiceCollection services)
{
	...

 	applicationLifetime.ApplicationStopped.Register(() =>
    {
        app.ApplicationServices.GetRequiredService<ICouchbaseLifetimeService>().Close();
    });
}

Using Caching in your Controllers

Add the following code to HomeController:

public IActionResult Index()
{
	_cache.Set("CacheTime", System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()));
	return View();
}

public IActionResult About()
{
	ViewData["Message"] = "Your application description page. "
				+ System.Text.Encoding.UTF8.GetString(_cache.Get("CacheTime"));
	return View();
}

Release Notes

Supports .NET Core 2.0

Version History

Version Downloads Last updated
1.0.0 (current version) 63 11/8/2017