Skip to main content

Posts

Showing posts from May, 2020

Refresh Cache using Redis and BackgroundService .net 5

Install Nuget package   Microsoft. Extensions. Caching. StackExchangeRedis Creating a cache attribute [Cached(time: 10)]  public async Task<IActionResult> GetAll() => Ok(await SalesdbContext.Employees.ToListAsync()); We create an attribute called cached that accepts the expiry time for the cached data. Once the expiry time is over the response returned will be null and we will fetch the data from the database and update the cahce. [AttributeUsage(AttributeTargets.Method)]     public class CachedAttribute : Attribute, IAsyncActionFilter     {         public int Time { get; set; }         public CachedAttribute(int time)         {             Time = time;         }         public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)       ...

Reading Dynamic Object values

https://stackoverflow.com/questions/20318261/dynamic-object-with-dollar-on-string https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object https://stackoverflow.com/questions/47507667/deserialize-json-with-variable-property-names https://www.jerriepelser.com/blog/deserialize-different-json-object-same-class/ dynamic stuff = JsonConvert.DeserializeObject<dynamic>("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");             string name = stuff.Name;             string address = stuff.Address.City;             foreach (JProperty jproperty in stuff)             {                 Console.WriteLine("jproperty.Name = {0}", jproperty.Name);             }             dynamic d = s...