Webiant Logo Webiant Logo
  1. No results found.

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

CacheEventConsumer.cs

using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Events;
using Nop.Core.Infrastructure;
using Nop.Services.Events;

namespace Nop.Services.Caching;

/// 
/// Represents the base entity cache event consumer
/// 
/// Entity type
public abstract partial class CacheEventConsumer :
    IConsumer>,
    IConsumer>,
    IConsumer>
    where TEntity : BaseEntity
{
    #region Fields

    protected readonly IShortTermCacheManager _shortTermCacheManager;
    protected readonly IStaticCacheManager _staticCacheManager;

    #endregion

    #region Ctor

    protected CacheEventConsumer()
    {
        _shortTermCacheManager = EngineContext.Current.Resolve();
        _staticCacheManager = EngineContext.Current.Resolve();
    }

    #endregion

    #region Utilities

    /// 
    /// Clear cache by entity event type
    /// 
    /// Entity
    /// Entity event type
    /// A task that represents the asynchronous operation
    protected virtual async Task ClearCacheAsync(TEntity entity, EntityEventType entityEventType)
    {
        await RemoveByPrefixAsync(NopEntityCacheDefaults.ByIdsPrefix);
        await RemoveByPrefixAsync(NopEntityCacheDefaults.AllPrefix);

        if (entityEventType != EntityEventType.Insert)
            await RemoveAsync(NopEntityCacheDefaults.ByIdCacheKey, entity);

        await ClearCacheAsync(entity);
    }

    /// 
    /// Clear cache data
    /// 
    /// Entity
    /// A task that represents the asynchronous operation
    protected virtual Task ClearCacheAsync(TEntity entity)
    {
        return Task.CompletedTask;
    }

    /// 
    /// Removes items by cache key prefix
    /// 
    /// Cache key prefix
    /// Parameters to create cache key prefix
    /// A task that represents the asynchronous operation
    protected virtual async Task RemoveByPrefixAsync(string prefix, params object[] prefixParameters)
    {
        _shortTermCacheManager.RemoveByPrefix(prefix, prefixParameters);
        await _staticCacheManager.RemoveByPrefixAsync(prefix, prefixParameters);
    }

    /// 
    /// Remove the value with the specified key from the cache
    /// 
    /// Cache key
    /// Parameters to create cache key
    /// A task that represents the asynchronous operation
    public async Task RemoveAsync(CacheKey cacheKey, params object[] cacheKeyParameters)
    {
        _shortTermCacheManager.Remove(cacheKey.Key, cacheKeyParameters);
        await _staticCacheManager.RemoveAsync(cacheKey, cacheKeyParameters);
    }

    #endregion

    #region Methods

    /// 
    /// Handle entity inserted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public virtual async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        await ClearCacheAsync(eventMessage.Entity, EntityEventType.Insert);
    }

    /// 
    /// Handle entity updated event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public virtual async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        await ClearCacheAsync(eventMessage.Entity, EntityEventType.Update);
    }

    /// 
    /// Handle entity deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public virtual async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        await ClearCacheAsync(eventMessage.Entity, EntityEventType.Delete);
    }

    #endregion

    #region Nested

    protected enum EntityEventType
    {
        Insert,
        Update,
        Delete
    }

    #endregion
}