Giter VIP home page Giter VIP logo

enyimmemcachedcore's Introduction

Enyim Memcached Client

This is a .NET Core client library for Memcached migrated from EnyimMemcached. Available on Nuget https://www.nuget.org/packages/EnyimMemcachedCore

Build Status

Configure

The appsettings.json Without Authentication

{
  "enyimMemcached": {
    "Servers": [
      {
        "Address": "memcached",
        "Port": 11211
      }
    ]
  }
}

The appsettings.json With Authentication

{
  "enyimMemcached": {
    "Servers": [
      {
        "Address": "memcached",
        "Port": 11211
      }
    ],
    "Authentication": {
      "Type": "Enyim.Caching.Memcached.PlainTextAuthenticator",
      "Parameters": {
        "zone": "",
        "userName": "username",
        "password": "password"
      }
    }
  }
}

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEnyimMemcached(Configuration.GetSection("enyimMemcached"));
	// or
	//services.AddEnyimMemcached(options => someSetupAction(options));
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    { 
        app.UseEnyimMemcached();
    }
}

Example usage

Use IMemcachedClient interface

public class TabNavService
{
    private ITabNavRepository _tabNavRepository;
    private IMemcachedClient _memcachedClient;

    public TabNavService(
        ITabNavRepository tabNavRepository,
        IMemcachedClient memcachedClient)
    {
        _tabNavRepository = tabNavRepository;
        _memcachedClient = memcachedClient;
    }

    public async Task<IEnumerable<TabNav>> GetAll()
    {
		var cacheKey = "aboutus_tabnavs_all";
        var result = await _memcachedClient.GetAsync<IEnumerable<TabNav>>(cacheKey);
        if (!result.Success)
        {
            var tabNavs = await _tabNavRepository.GetAll();
            await _memcachedClient.AddAsync(cacheKey, tabNavs, 300);
            return tabNavs;
        }
        else
        {
            return result.Value;
        }
    }
}

Use IDistributedCache interface

public class CreativeService
{
    private ICreativeRepository _creativeRepository;
    private IDistributedCache _cache;

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

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

        var creativesJson = await _cache.GetStringAsync(cacheKey);
        if (creativesJson == null)
        {
            creatives = await _creativeRepository.GetCreatives(unitName)
                    .ProjectTo<CreativeDTO>().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<List<CreativeDTO>>(creativesJson);
        }

        return creatives;
    }
}

enyimmemcachedcore's People

Contributors

bcuff avatar catcherwong avatar ciaranj avatar cnblogs-dudu avatar enyim avatar greatkeke avatar greggbjensen avatar iamkrillin avatar jzablocki avatar keke-cnblogs avatar mausch avatar mikeleedev avatar nathanbaulch avatar noonamer avatar vytautask avatar

Watchers

 avatar  avatar

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.