Webiant Logo Webiant Logo
  1. No results found.

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

CacheKeyService.cs

using System.Globalization;
using System.Text;
using Nop.Core.Configuration;

namespace Nop.Core.Caching;

/// 
/// Represents the default cache key service implementation
/// 
public abstract partial class CacheKeyService : ICacheKeyService
{
    #region Fields

    protected readonly AppSettings _appSettings;

    #endregion

    #region Ctor

    protected CacheKeyService(AppSettings appSettings)
    {
        _appSettings = appSettings;
    }

    #endregion

    #region Utilities

    /// 
    /// Prepare the cache key prefix
    /// 
    /// Cache key prefix
    /// Parameters to create cache key prefix
    protected virtual string PrepareKeyPrefix(string prefix, params object[] prefixParameters)
    {
        return prefixParameters?.Any() ?? false
            ? string.Format(prefix, prefixParameters.Select(CreateCacheKeyParameters).ToArray())
            : prefix;
    }

    /// 
    /// Create the hash value of the passed identifiers
    /// 
    /// Collection of identifiers
    /// String hash value
    protected virtual string CreateIdsHash(IEnumerable ids)
    {
        var identifiers = ids.ToList();

        if (!identifiers.Any())
            return string.Empty;

        var identifiersString = string.Join(", ", identifiers.OrderBy(id => id));
        return HashHelper.CreateHash(Encoding.UTF8.GetBytes(identifiersString), HashAlgorithm);
    }

    /// 
    /// Converts an object to cache parameter
    /// 
    /// Object to convert
    /// Cache parameter
    protected virtual object CreateCacheKeyParameters(object parameter)
    {
        return parameter switch
        {
            null => "null",
            IEnumerable ids => CreateIdsHash(ids),
            IEnumerable entities => CreateIdsHash(entities.Select(entity => entity.Id)),
            BaseEntity entity => entity.Id,
            decimal param => param.ToString(CultureInfo.InvariantCulture),
            _ => parameter
        };
    }

    #endregion

    #region Methods

    /// 
    /// Create a copy of cache key and fills it by passed parameters
    /// 
    /// Initial cache key
    /// Parameters to create cache key
    /// Cache key
    public virtual CacheKey PrepareKey(CacheKey cacheKey, params object[] cacheKeyParameters)
    {
        return cacheKey.Create(CreateCacheKeyParameters, cacheKeyParameters);
    }

    /// 
    /// Create a copy of cache key using the default cache time and fills it by passed parameters
    /// 
    /// Initial cache key
    /// Parameters to create cache key
    /// Cache key
    public virtual CacheKey PrepareKeyForDefaultCache(CacheKey cacheKey, params object[] cacheKeyParameters)
    {
        var key = cacheKey.Create(CreateCacheKeyParameters, cacheKeyParameters);

        key.CacheTime = _appSettings.Get().DefaultCacheTime;

        return key;
    }
        
    #endregion

    #region Properties

    /// 
    /// Gets an algorithm used to create the hash value of identifiers need to cache
    /// 
    protected string HashAlgorithm => "SHA1";

    #endregion
}