Memory Cache Manager is a multi-thread safe caching mechanims
There is a nuget package avaliable here https://www.nuget.org/packages/MemoryCacheManager/.
const string cacheKey = "MyCache";
const int value = 123;
// Add the cache
Cache.AddCache(cacheKey, value);
// Get the cache
var cacheExists = Cache.TryGetCache(cacheKey, out int cachedValue);
// cacheValue: 123
const string cacheKey = "UserId";
const user1 = 'user1';
const user1Id = 1;
const user2 = 'user2';
const user2Id = 2;
// Add the cache
Cache.AddCache(cacheKey, user1Id, user1);
Cache.AddCache(cacheKey, user2Id, user2);
// Get the cache
Cache.TryGetCache(cacheKey, out int userId1Cached, user1);
Cache.TryGetCache(cacheKey, out int userId2Cached, user2);
// userId1Cached: 1
// userId2Cached: 2
const string cacheKey = "MyCache";
const int value = 123;
var expirationTime = new TimeSpan(0, 10, 0); // Keep the cache per 10 minutes
// Add the cache
Cache.AddCacheExpirationTime(cacheKey, value, expirationTime);
// Get the cache
Cache.TryGetCache(cacheKey, out int cachedValue);
// cacheValue: 123
In this case, Memory cache will try to find a cache value, if there is no cache by user name, it will process the block.
public int GetUserId(string userName)
{
return Cache.GetCacheOrRunAndCacheWithExpirationTime("UserCache", new TimeSpan(0, 1, 0), () =>
{
// This block will run only if there is no cached value
var userId = new Random().Next();
return userId;
}, userName);
}
public async Task<int> GetUserId(string userName)
{
return Cache.GetCacheOrRunAndCacheWithExpirationTimeAsync("UserCache", new TimeSpan(0, 1, 0), async () =>
{
// This block will run only if there is no cached value
// Use any async function ....
var userId = new Random().Next();
return userId;
}, userName);
}
MIT