You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Infrastructure/Extensions/DistributedCacheExtensions.cs

29 lines
870 B

using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Text;
namespace Infrastructure.Extensions
{
public static class DistributedCacheExtensions
{
public static void Set<T>(this IDistributedCache cache, object key, T value)
{
cache.SetString(key.ToString(), value.ToJson(), new DistributedCacheEntryOptions { AbsoluteExpiration = DateTimeOffset.UtcNow.AddHours(1) });
}
public static T Get<T>(this IDistributedCache cache, object key)
{
var value = cache.GetString(key.ToString());
if (value != null)
{
return value.FromJson<T>();
}
return default(T);
}
public static void Remove(this IDistributedCache cache, object key)
{
cache.Remove(key.ToString());
}
}
}