Example #1
0
        private void RegisterChangesTrackingService()
        {
            Func <ICacheRepository> repositoryFactory = () => new CacheRepositoryImpl(_connectionStringName, new EntityPrimaryKeyGeneratorInterceptor());
            var changeTrackingService = new ChangesTrackingService(repositoryFactory);

            _container.RegisterInstance <IChangesTrackingService>(changeTrackingService);
            var cacheManager = _container.Resolve <ICacheManager <object> >();

            var observedRegions = new[] {
                StoreServicesDecorator.RegionName,
                CatalogServicesDecorator.RegionName,
                MemberServicesDecorator.RegionName,
                MarketingServicesDecorator.RegionName,
                InventoryServicesDecorator.RegionName,
                PricingServicesDecorator.RegionName
            };

            var logger = _container.Resolve <ILog>();

            //Need observe cache events to correct update latest changes timestamp when platform running on multiple instances
            //Cache clean event will be raising  thanks to Redis cache synced invalidation
            EventHandler <CacheClearEventArgs> onClearHandler = (sender, args) =>
            {
                try
                {
                    changeTrackingService.Update(null, DateTime.UtcNow);
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
            };

            EventHandler <CacheClearRegionEventArgs> onClearRegionHandler = (sender, args) =>
            {
                if (args.Region != null && observedRegions.Any(x => x.EqualsInvariant(args.Region)))
                {
                    try
                    {
                        changeTrackingService.Update(null, DateTime.UtcNow);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                    }
                }
            };

            //Throttling is required to prevent frequent database updates operations. 10 seconds is set as minimum interval to prevent flooding of database when multiple
            //platform instances is running
            cacheManager.OnClearRegion += onClearRegionHandler.Throttle(TimeSpan.FromSeconds(10));
            cacheManager.OnClear       += onClearHandler.Throttle(TimeSpan.FromSeconds(10));
        }
Example #2
0
        private void RegisterChangesTrackingService()
        {
            Func <ICacheRepository> repositoryFactory = () => new CacheRepositoryImpl(_connectionStringName, new EntityPrimaryKeyGeneratorInterceptor());
            var changeTrackingService = new ChangesTrackingService(repositoryFactory);

            _container.RegisterInstance <IChangesTrackingService>(changeTrackingService);
            var cacheManager = _container.Resolve <ICacheManager <object> >();

            var observedRegions = new[] {
                StoreServicesDecorator.RegionName,
                CatalogServicesDecorator.RegionName,
                MemberServicesDecorator.RegionName,
                MarketingServicesDecorator.RegionName,
                InventoryServicesDecorator.RegionName,
                PricingServicesDecorator.RegionName
            };

            var logger = _container.Resolve <ILog>();

            //Need observe cache events to correct update latest changes timestamp when platform running on multiple instances
            //Cache clean event will be raising  thanks to Redis cache synced invalidation
            cacheManager.OnClear += (e, args) =>
            {
                try
                {
                    changeTrackingService.Update(null, DateTime.UtcNow);
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
            };
            cacheManager.OnClearRegion += (e, args) =>
            {
                if (args.Region != null && observedRegions.Any(x => x.EqualsInvariant(args.Region)))
                {
                    try
                    {
                        changeTrackingService.Update(null, DateTime.UtcNow);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                    }
                }
            };
        }