Webiant Logo Webiant Logo
  1. No results found.

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

CustomerCacheEventConsumer.cs

using Nop.Core.Domain.Customers;
using Nop.Services.Caching;
using Nop.Services.Events;
using Nop.Services.Orders;

namespace Nop.Services.Customers.Caching;

/// 
/// Represents a customer cache event consumer
/// 
public partial class CustomerCacheEventConsumer : CacheEventConsumer, IConsumer
{
    #region Methods

    /// 
    /// Handle password changed event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(CustomerPasswordChangedEvent eventMessage)
    {
        await RemoveAsync(NopCustomerServicesDefaults.CustomerPasswordLifetimeCacheKey, eventMessage.Password.CustomerId);
    }

    /// 
    /// Clear cache by entity event type
    /// 
    /// Entity
    /// Entity event type
    /// A task that represents the asynchronous operation
    protected override async Task ClearCacheAsync(Customer entity, EntityEventType entityEventType)
    {
        if (entityEventType == EntityEventType.Delete)
        {
            await RemoveAsync(NopCustomerServicesDefaults.CustomerAddressesCacheKey, entity);
            await RemoveByPrefixAsync(NopCustomerServicesDefaults.CustomerAddressesByCustomerPrefix, entity);
        }

        await base.ClearCacheAsync(entity, entityEventType);
    }

    /// 
    /// Clear cache data
    /// 
    /// Entity
    /// A task that represents the asynchronous operation
    protected override async Task ClearCacheAsync(Customer entity)
    {
        await RemoveAsync(NopCustomerServicesDefaults.CustomerRolesCacheKey, entity);
        await RemoveByPrefixAsync(NopOrderDefaults.ShoppingCartItemsByCustomerPrefix, entity);
        await RemoveAsync(NopCustomerServicesDefaults.CustomerByGuidCacheKey, entity.CustomerGuid);

        if (string.IsNullOrEmpty(entity.SystemName))
            return;

        await RemoveAsync(NopCustomerServicesDefaults.CustomerBySystemNameCacheKey, entity.SystemName);
    }

    #endregion
}