Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cnblogs/EnyimMemcachedCore
.NET Memcached client. Available on https://www.nuget.org/packages/EnyimMemcachedCore
https://github.com/cnblogs/EnyimMemcachedCore
csharp donet memcached
Last synced: 1 day ago
JSON representation
.NET Memcached client. Available on https://www.nuget.org/packages/EnyimMemcachedCore
- Host: GitHub
- URL: https://github.com/cnblogs/EnyimMemcachedCore
- Owner: cnblogs
- License: apache-2.0
- Fork: true (enyim/EnyimMemcached)
- Created: 2014-04-15T08:52:50.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-10-27T05:00:17.000Z (18 days ago)
- Last Synced: 2024-11-07T20:50:16.279Z (7 days ago)
- Topics: csharp, donet, memcached
- Language: C#
- Homepage:
- Size: 3.29 MB
- Stars: 162
- Watchers: 8
- Forks: 45
- Open Issues: 32
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Enyim Memcached Client
This is a memcached client library for .NET migrated from [EnyimMemcached](https://github.com/enyim/EnyimMemcached).
## Configure
### The appsettings.json Without Authentication
```json
{
"enyimMemcached": {
"Servers": [
{
"Address": "memcached",
"Port": 11211
}
],
"Transcoder": "MessagePackTranscoder"
}
}
```
#### The appsettings.json With Authentication
```json
{
"enyimMemcached": {
"Servers": [
{
"Address": "memcached",
"Port": 11211
}
],
"Authentication": {
"Type": "Enyim.Caching.Memcached.PlainTextAuthenticator",
"Parameters": {
"zone": "",
"userName": "username",
"password": "password"
}
}
}
}
```
### Startup.cs
```cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddEnyimMemcached();
// services.AddEnyimMemcached("enyimMemcached");
// services.AddEnyimMemcached(Configuration);
// services.AddEnyimMemcached(Configuration, "enyimMemcached");
// services.AddEnyimMemcached(Configuration.GetSection("enyimMemcached"));
// services.AddEnyimMemcached(options => options.AddServer("memcached", 11211));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseEnyimMemcached();
}
}
```## Example usage
### Use IMemcachedClient interface
```cs
public class HomeController : Controller
{
private readonly IMemcachedClient _memcachedClient;
private readonly IBlogPostService _blogPostService;public HomeController(IMemcachedClient memcachedClient, IBlogPostService blogPostService)
{
_memcachedClient = memcachedClient;
_blogPostService = blogPostService;
}public async Task Index()
{
var cacheKey = "blogposts-recent";
var cacheSeconds = 600;var posts = await _memcachedClient.GetValueOrCreateAsync(
cacheKey,
cacheSeconds,
async () => await _blogPostService.GetRecent(10));return Ok(posts);
}
}
```
### Use IDistributedCache interface
```cs
public class CreativeService
{
private ICreativeRepository _creativeRepository;
private IDistributedCache _cache;public CreativeService(
ICreativeRepository creativeRepository,
IDistributedCache cache)
{
_creativeRepository = creativeRepository;
_cache = cache;
}public async Task> GetCreatives(string unitName)
{
var cacheKey = $"creatives_{unitName}";
IList creatives = null;var creativesJson = await _cache.GetStringAsync(cacheKey);
if (creativesJson == null)
{
creatives = await _creativeRepository.GetCreatives(unitName)
.ProjectTo().ToListAsync();var json = string.Empty;
if (creatives != null && creatives.Count() > 0)
{
json = JsonConvert.SerializeObject(creatives);
}
await _cache.SetStringAsync(
cacheKey,
json,
new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(5)));
}
else
{
creatives = JsonConvert.DeserializeObject>(creativesJson);
}return creatives;
}
}
```