SzkoleniaRiskoff/src/Libraries/Nop.Core/Caching/CacheKey.cs

73 lines
2.3 KiB
C#
Raw Normal View History

2024-08-21 10:09:17 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using Nop.Core.Configuration;
2024-08-21 06:17:23 -04:00
using Nop.Core.Infrastructure;
2024-08-21 10:09:17 -04:00
namespace Nop.Core.Caching
2024-08-21 06:17:23 -04:00
{
/// <summary>
2024-08-21 10:09:17 -04:00
/// Represents key for caching objects
2024-08-21 06:17:23 -04:00
/// </summary>
2024-08-21 10:09:17 -04:00
public partial class CacheKey
2024-08-21 06:17:23 -04:00
{
2024-08-21 10:09:17 -04:00
#region Ctor
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
/// <summary>
/// Initialize a new instance with key and prefixes
/// </summary>
/// <param name="key">Key</param>
/// <param name="prefixes">Prefixes for remove by prefix functionality</param>
public CacheKey(string key, params string[] prefixes)
{
Key = key;
Prefixes.AddRange(prefixes.Where(prefix => !string.IsNullOrEmpty(prefix)));
}
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
#endregion
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
#region Methods
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
/// <summary>
/// Create a new instance from the current one and fill it with passed parameters
/// </summary>
/// <param name="createCacheKeyParameters">Function to create parameters</param>
/// <param name="keyObjects">Objects to create parameters</param>
/// <returns>Cache key</returns>
public virtual CacheKey Create(Func<object, object> createCacheKeyParameters, params object[] keyObjects)
{
var cacheKey = new CacheKey(Key, Prefixes.ToArray());
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
if (!keyObjects.Any())
return cacheKey;
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
cacheKey.Key = string.Format(cacheKey.Key, keyObjects.Select(createCacheKeyParameters).ToArray());
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
for (var i = 0; i < cacheKey.Prefixes.Count; i++)
cacheKey.Prefixes[i] = string.Format(cacheKey.Prefixes[i], keyObjects.Select(createCacheKeyParameters).ToArray());
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
return cacheKey;
}
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
#endregion
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
#region Properties
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
/// <summary>
/// Gets or sets a cache key
/// </summary>
public string Key { get; protected set; }
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
/// <summary>
/// Gets or sets prefixes for remove by prefix functionality
/// </summary>
public List<string> Prefixes { get; protected set; } = new List<string>();
2024-08-21 06:17:23 -04:00
2024-08-21 10:09:17 -04:00
/// <summary>
/// Gets or sets a cache time in minutes
/// </summary>
public int CacheTime { get; set; } = Singleton<AppSettings>.Instance.Get<CacheConfig>().DefaultCacheTime;
#endregion
}
2024-08-21 06:17:23 -04:00
}