Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

CacheKey.cs

using Nop.Core.Configuration;
using Nop.Core.Infrastructure;

namespace Nop.Core.Caching;

/// 
/// Represents key for caching objects
/// 
public partial class CacheKey
{
    #region Ctor

    /// 
    /// Initialize a new instance with key and prefixes
    /// 
    /// Key
    /// Prefixes for remove by prefix functionality
    public CacheKey(string key, params string[] prefixes)
    {
        Key = key;
        Prefixes.AddRange(prefixes.Where(prefix => !string.IsNullOrEmpty(prefix)));
    }

    #endregion

    #region Methods

    /// 
    /// Create a new instance from the current one and fill it with passed parameters
    /// 
    /// Function to create parameters
    /// Objects to create parameters
    /// Cache key
    public virtual CacheKey Create(Func createCacheKeyParameters, params object[] keyObjects)
    {
        var cacheKey = new CacheKey(Key, Prefixes.ToArray());

        if (!keyObjects.Any())
            return cacheKey;

        cacheKey.Key = string.Format(cacheKey.Key, keyObjects.Select(createCacheKeyParameters).ToArray());

        for (var i = 0; i < cacheKey.Prefixes.Count; i++)
            cacheKey.Prefixes[i] = string.Format(cacheKey.Prefixes[i], keyObjects.Select(createCacheKeyParameters).ToArray());

        return cacheKey;
    }

    #endregion

    #region Properties

    /// 
    /// Gets or sets a cache key
    /// 
    public string Key { get; protected set; }

    /// 
    /// Gets or sets prefixes for remove by prefix functionality
    /// 
    public List Prefixes { get; protected set; } = new();

    /// 
    /// Gets or sets a cache time in minutes
    /// 
    public int CacheTime { get; set; } = Singleton.Instance.Get().DefaultCacheTime;

    #endregion
}