forked from brantburnett/Couchbase.Extensions
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation ---------- Provide support for distributed cache to users of the 3.x SDK. Modifications ------------- Upgrade to SDK 3.x. Rewrite Couchbase to be based on an ICouchbaseCollection instead of an IBucket. Also update to work with only async methods from ICouchbaseCollection, and to specifically force the use of the LegacyTranscoder for byte arrays. Update the DI system to allow injecting a specific collection, but by default inject the default collection from the bucket specified. Also drop the overload that accepts a bucket password. Upgrade to C# 8 and enable nullable reference types. Update unit tests, the example project, and documentation. Results ------- SDK 3.x compatibility, with future-proofing for scopes/collections. The `.GetAwaiter().GetResult()` approach to async from sync is not perfect, as it could cause deadlocks depending on the SynchronizationContext in use. However, it's a decent and quick approach for now that may be improved in the future. Closes #84
- Loading branch information
1 parent
931c7ea
commit 2409ae3
Showing
21 changed files
with
411 additions
and
512 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,69 @@ | ||
# 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. | ||
|
||
A custom ASP.NET Core Middleware plugin for a distributed cache using Couchbase server as the backing store. Supports both Ephemeral (in-memory) and Couchbase (persistent) buckets. | ||
|
||
## Getting Started ## | ||
Assuming you have an [installation of Couchbase Server](https://developer.couchbase.com/documentation/server/4.5/getting-started/installing.html) and Visual Studio (examples with VSCODE forthcoming), do the following: | ||
|
||
### Couchbase .NET Core Distributed Cache: ### | ||
Assuming you have an [installation of Couchbase Server](https://docs.couchbase.com/server/current/introduction/intro.html) 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](https://www.nuget.org/packages/Couchbase.Extensions.Caching/) 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 => | ||
```csharp | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
// Add framework services. | ||
services.AddMvc(); | ||
|
||
services.AddCouchbase(opt => | ||
{ | ||
opt.Servers = new List<Uri> | ||
{ | ||
opt.Servers = new List<Uri> | ||
{ | ||
new Uri("http://10.111.150.101:8091") | ||
}; | ||
}); | ||
new Uri("http://10.111.150.101:8091") | ||
}; | ||
}); | ||
|
||
services.AddDistributedCouchbaseCache("default", opt => { }); | ||
} | ||
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; | ||
} | ||
} | ||
```csharp | ||
public class HomeController : Controller | ||
{ | ||
private IDistributedCache _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(); | ||
}); | ||
} | ||
public HomeController(IDistributedCache cache) | ||
{ | ||
_cache = cache; | ||
} | ||
} | ||
``` | ||
|
||
### 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(); | ||
} | ||
```csharp | ||
public async Task<IActionResult> Index() | ||
{ | ||
await _cache.SetAsync("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(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.