Code Monkey home page Code Monkey logo

enyim.caching's Introduction

VIEApps.Enyim.Caching

The memcached client library on .NET 5 & .NET Standard 2.0:

  • 100% compatible with EnyimMemcached 2.x
  • Fully async (EnyimMemcached still blocks threads while reading from sockets/response)
  • Multiple nodes supported with Ketama for better distribution
  • Object serialization by various transcoders: Json.NET Bson, Protocol Buffers, MessagePack
  • Ready with .NET Core 2.x+/.NET Framework 4.6.1+ with more useful methods (Set, Add, Replace, Refresh, Exists)

NuGet

NuGet

Information

Usage of ASP.NET Core 2.x+ apps

  • Add services.AddMemcached(...) and app.UseMemcached() in Startup.cs
  • Add IMemcachedClient or IDistributedCache into constructor (using dependency injection)

Configure (by the appsettings.json file) without authentication

{
	"Memcached": {
		"Servers": [
		{
			"Address": "192.168.0.2",
			"Port": 11211
		},
		{
			"Address": "192.168.0.3",
			"Port": 11211
		}],
		"SocketPool": {
			"MinPoolSize": 10,
			"MaxPoolSize": 100,
			"DeadTimeout": "00:01:00",
			"ConnectionTimeout": "00:00:05",
			"ReceiveTimeout": "00:00:01"
		}
	}
}

Configure (by the appsettings.json file) with authentication

{
	"Memcached": {
		"Servers": [
		{
			"Address": "192.168.0.2",
			"Port": 11211
		},
		{
			"Address": "192.168.0.3",
			"Port": 11211
		}],
		"SocketPool": {
			"MinPoolSize": 10,
			"MaxPoolSize": 100,
			"DeadTimeout": "00:01:00",
			"ConnectionTimeout": "00:00:05",
			"ReceiveTimeout": "00:00:01"
		},
		"Authentication": {
			"Type": "Enyim.Caching.Memcached.PlainTextAuthenticator, Enyim.Caching",
			"Parameters": {
				"zone": "",
				"userName": "username",
				"password": "password"
			}
		}
	}
}

Startup.cs

public class Startup
{
	public void ConfigureServices(IServiceCollection services)
	{
		// ....
		services.AddMemcached(options => Configuration.GetSection("Memcached").Bind(options));
	}
	
	public void Configure(IApplicationBuilder app, IHostingEnvironment env)
	{ 
		// ....
		app.UseMemcached();
	}
}

Use IMemcachedClient interface

public class TabNavService
{
	ITabNavRepository _tabNavRepository;
	IMemcachedClient _cache;

	public TabNavService(ITabNavRepository tabNavRepository, IMemcachedClient cache)
	{
		_tabNavRepository = tabNavRepository;
		_cache = cache;
	}

	public async Task<IEnumerable<TabNav>> GetAllAsync()
	{
		var cacheKey = "aboutus_tabnavs_all";
		var result = await _cache.GetAsync<IEnumerable<TabNav>>(cacheKey);
		if (result == null)
		{
			var tabNavs = await _tabNavRepository.GetAll();
			await _cache.SetAsync(cacheKey, tabNavs, TimeSpan.FromMinutes(30));
			return tabNavs;
		}
		else
			return result;
	}
}

Use IDistributedCache interface

public class CreativeService
{
	ICreativeRepository _creativeRepository;
	IDistributedCache _cache;

	public CreativeService(ICreativeRepository creativeRepository, IDistributedCache cache)
	{
		_creativeRepository = creativeRepository;
		_cache = cache;
	}

	public async Task<IList<CreativeDTO>> GetCreativesAsync(string unitName)
	{
		var cacheKey = $"creatives_{unitName}";
		IList<CreativeDTO> creatives = null;

		var creativesBytes = await _cache.GetAsync(cacheKey);
		var creativesJson = creativesBytes != null ? System.Text.Encoding.UTF8.GetString(creativesBytes) : null;
		if (creativesJson == null)
		{
			creatives = await _creativeRepository.GetCreatives(unitName).ProjectTo<CreativeDTO>().ToListAsync();
			var json = creatives != null && creatives.Count() > 0 ? JsonConvert.SerializeObject(creatives) : string.Empty;
			creativesBytes = System.Text.Encoding.UTF8.GetBytes(json);
			await _cache.SetAsync(cacheKey, creativesBytes, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(30)));
		}
		else
			creatives = JsonConvert.DeserializeObject<List<CreativeDTO>>(creativesJson);

		return creatives;
	}
}

Usage of .NET Core 2.x+/.NET Framework 4.6.1+ standalone apps

Configure (by the app.config/web.config) without authentication

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<configSections>
		<section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientConfigurationSectionHandler, Enyim.Caching" />
	</configSections>
	<memcached>
		<servers>
			<add address="192.168.0.2" port="11211" />
			<add address="192.168.0.3" port="11211" />
		</servers>
		<socketPool minPoolSize="10" maxPoolSize="100" deadTimeout="00:01:00" connectionTimeout="00:00:05" receiveTimeout="00:00:01" />
	</memcached>
</configuration>

Configure (by the app.config/web.config) with authentication

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<configSections>
		<section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientConfigurationSectionHandler, Enyim.Caching" />
	</configSections>
	<memcached>
		<servers>
			<add address="192.168.0.2" port="11211" />
			<add address="192.168.0.3" port="11211" />
		</servers>
		<socketPool minPoolSize="10" maxPoolSize="100" deadTimeout="00:01:00" connectionTimeout="00:00:05" receiveTimeout="00:00:01" />
		<authentication type="Enyim.Caching.Memcached.PlainTextAuthenticator, Enyim.Caching" zone="" userName="username" password="password" />
	</memcached>
</configuration>

Example

public class CreativeService
{
	MemcachedClient _cache;

	public CreativeService()
	{
		_cache = new MemcachedClient(ConfigurationManager.GetSection("memcached") as MemcachedClientConfigurationSectionHandler);
	}

	public async Task<IList<CreativeDTO>> GetCreativesAsync(string unitName)
	{
		return await _cache.GetAsync<IList<CreativeDTO>>($"creatives_{unitName}");
	}
}

Other transcoders (Protocol Buffers, Json.NET Bson, MessagePack)

See VIEApps.Enyim.Caching.Transcoders

Need a library for working with other distributed cache (Redis) in the same time?

See VIEApps.Components.Caching

enyim.caching's People

Contributors

quynh-ng avatar sethreber avatar shortendarragh-mt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

enyim.caching's Issues

Reconnect after los of connection doesn't work

Hi there,

I've written a little demo application and try to run my program, shut-down the memcached server, start it again and hoped to see my application reconnecting to the cache. Only it doesn't. What am I missing here?

Here's the application: https://github.com/staal-it/MemcachedDemo. I'm running Memcached on Windows, version 1.4.5.

This is how I setup my client (not from config):

public MemcacedCache()
{

            _logger = new Logger();

            var config = new MemcachedClientConfiguration();

            config.Servers.Add(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11211));

            config.Protocol = MemcachedProtocol.Binary;

            _client = new MemcachedClient(null, config);
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.