Skip to content

Instantly share code, notes, and snippets.

@pichayean
Created May 4, 2021 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pichayean/d2f7e4de41e0a56be49f72988124c2fa to your computer and use it in GitHub Desktop.
Save pichayean/d2f7e4de41e0a56be49f72988124c2fa to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;
using Microsoft.Extensions.Caching.Distributed;
namespace distributed_caching_redis
{
public interface IUsersService
{
Task<IEnumerable<Users>> GetUsersAsync();
}
public class UsersService : IUsersService
{
private readonly IDistributedCache _distributedCache;
private readonly HttpClient _httpClient;
private const string CACHE_KEY = "users.cache.key";
public UsersService(IDistributedCache distributedCache, HttpClient httpClient)
{
_distributedCache = distributedCache;
_httpClient = httpClient;
}
public async Task<IEnumerable<Users>> GetUsersAsync()
{
var usersCache = _distributedCache.GetString(CACHE_KEY);
if (String.IsNullOrEmpty(usersCache))
{
var response = await _httpClient.GetAsync("https://jsonplaceholder.typicode.com/users");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
_distributedCache.SetString(CACHE_KEY, responseBody);
var res = JsonSerializer.Deserialize<IEnumerable<Users>>(responseBody);
return res;
}
else
{
var res = JsonSerializer.Deserialize<IEnumerable<Users>>(usersCache);
return res;
}
}
}
public class Users
{
public int id { get; set; }
public string name { get; set; }
public string username { get; set; }
public string email { get; set; }
public Address address { get; set; }
public string phone { get; set; }
public string website { get; set; }
public Company company { get; set; }
}
public class Company
{
public string name { get; set; }
public string catchPhrase { get; set; }
public string bs { get; set; }
}
public class Address
{
public string street { get; set; }
public string suite { get; set; }
public string city { get; set; }
public string zipcode { get; set; }
public Geo geo { get; set; }
}
public class Geo
{
public string lat { get; set; }
public string lng { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment