Skip to content

radario/couchbase-aspnet

 
 

Repository files navigation

Couchbase ASP.NET For .NET Core

An Open Source project that provides ASP.Net Core infrastructure support for Couchbase Server.

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

  • Install the package from NuGet or build from source and add reference

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

      public void ConfigureServices(IServiceCollection services)
      {
          // Add framework services.
          services.AddMvc();
    
          services.AddDistributedCouchbaseCache(opt =>
          {
              opt.BucketName = "default";
              opt.Configuration = new ClientConfiguration
              {
                  Servers = new List<Uri>
                  {
                      new Uri("http://localhost:8091")
                  }
              };
              ClusterHelper.Initialize(opt.Configuration);
              opt.Bucket = ClusterHelper.GetBucket(opt.BucketName);
          });
      }
    

    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;
          }
      }
    
  • 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();
      }
    

About

Couchbase Asp.Net infrastructure support

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%