Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Refactor of the ICacheManager interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Jun 16, 2020
1 parent 2afd3f6 commit c174243
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 388 deletions.
81 changes: 0 additions & 81 deletions Grand.Core/Caching/CacheExtensions.cs

This file was deleted.

20 changes: 4 additions & 16 deletions Grand.Core/Caching/ICacheManager.cs
Expand Up @@ -13,30 +13,18 @@ public interface ICacheManager: IDisposable
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <param name="acquire">Function to load</param>
/// <returns>The value associated with the specified key.</returns>
Task<T> GetAsync<T>(string key);
Task<T> GetAsync<T>(string key, Func<Task<T>> acquire);

/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <param name="acquire">Function to load</param>
/// <returns>The value associated with the specified key.</returns>
T Get<T>(string key);

/// <summary>
/// Gets or sets the value associated with the specified key asynchronosly
/// </summary>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
Task<(T Result, bool FromCache)> TryGetValueAsync<T>(string key);

/// <summary>
/// Gets or sets the value associated with the specified key synchronosly
/// </summary>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
(T Result, bool FromCache) TryGetValue<T>(string key);
T Get<T>(string key, Func<T> acquire);

/// <summary>
/// Adds the specified key and object to the cache.
Expand Down
42 changes: 16 additions & 26 deletions Grand.Core/Caching/MemoryCacheManager.cs
Expand Up @@ -144,46 +144,36 @@ private void PostEviction(object key, object value, EvictionReason reason, objec
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Key of cached item</param>
/// <param name="acquire">Function to load</param>
/// <returns>The cached value associated with the specified key</returns>
public virtual Task<T> GetAsync<T>(string key)
public virtual async Task<T> GetAsync<T>(string key, Func<Task<T>> acquire)
{
return Task.FromResult(_cache.Get<T>(key));
return await _cache.GetOrCreateAsync(key, entry =>
{
AddKey(key);
entry.SetOptions(GetMemoryCacheEntryOptions(CommonHelper.CacheTimeMinutes));
return acquire();
});
}

/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Key of cached item</param>
/// <param name="acquire">Function to load</param>
/// <returns>The cached value associated with the specified key</returns>
public T Get<T>(string key)
{
return _cache.Get<T>(key);
}

/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <param name="key">Key of cached item</param>
/// <returns>The cached value associated with the specified key</returns>
public virtual (T, bool) TryGetValue<T>(string key)
public T Get<T>(string key, Func<T> acquire)
{
if (_cache.TryGetValue(key, out T value))
return _cache.GetOrCreate(key, entry =>
{
return (value, true);
}
return (default(T), false);
AddKey(key);
entry.SetOptions(GetMemoryCacheEntryOptions(CommonHelper.CacheTimeMinutes));
return acquire();
});
}

/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <param name="key">Key of cached item</param>
/// <returns>The cached value associated with the specified key</returns>
public Task<(T Result, bool FromCache)> TryGetValueAsync<T>(string key)
{
return Task.FromResult(TryGetValue<T>(key));
}


/// <summary>
/// Adds the specified key and object to the cache
Expand Down
211 changes: 0 additions & 211 deletions Grand.Core/Caching/PerRequestCacheManager.cs

This file was deleted.

0 comments on commit c174243

Please sign in to comment.