Skip to content

jakeuj/Microsoft.Extensions.Caching.Redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Microsoft.Extensions.Caching.Redis

Microsoft.Extensions.Caching.Redis

Usage

1. appsettings.json

{
  "redis": {
    "Host": "localhost",
    "Password": ""
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

2. Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();

        // Add Redis Cache services
        services.AddDistributedServiceStackRedisCache(options =>
        {
            Configuration.GetSection("redis").Bind(options);
            //Workaround for deadlock when resolving host name
            IPAddress ip;
            if (!IPAddress.TryParse(options.Host, out ip))
            {
                options.Host = Dns.GetHostAddressesAsync(options.Host)
                .Result.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork).ToString();
            }
        });
        // ...
    }
    // ...
}

3. Done

public class ValuesController : Controller
{
    private readonly IServiceStackRedisCache _cache;
    public ValuesController(IServiceStackRedisCache cache)
    {
        _cache = cache;
    }
	
    [HttpGet]
    public List<User> Get()
    {
        return _cache.GetAll<User>().ToList();
    }
	
    [HttpGet("{id}")]
    public void Get(int id)
    {
        // from db
        // var users = _userRepository.GetAll().ToList();

        // test data
        List<User> users = new List<User>();
        for (int a = 1; a < id; a++)
            users.Add(new User() { Id = a, Name = string.Format("Name{0}", a) });

        _cache.SetAll(users);
    }
}

About

Microsoft.Extensions.Caching.Redis

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages