Exemple #1
0
        public override bool TryAssert(OrganizationServiceContext context, Entity entity, CrmEntityRight right, CrmEntityCacheDependencyTrace dependencies)
        {
            var info = CacheInfoFactory.GetCacheInfo(context, entity, right);

            if (!info.IsCacheable)
            {
                return(UnderlyingProvider.TryAssert(context, entity, right, dependencies));
            }

            // No locking is required here because the Items collection is already per-thread/per-request.
            var cachedValue = HttpContext.Current.Items[info.Key];

            if (cachedValue is bool)
            {
                return((bool)cachedValue);
            }

            var value = UnderlyingProvider.TryAssert(context, entity, right, dependencies);

            // We ignore whether the value is cacheable or not and always cache it at this level, as the result
            // will always be the same per-request.
            HttpContext.Current.Items[info.Key] = value;

            return(value);
        }
Exemple #2
0
        public override bool TryAssert(OrganizationServiceContext context, Entity entity, CrmEntityRight right, CrmEntityCacheDependencyTrace dependencies)
        {
            var info = CacheInfoFactory.GetCacheInfo(context, entity, right);

            if (!info.IsCacheable)
            {
                return(UnderlyingProvider.TryAssert(context, entity, right, dependencies));
            }

            Stopwatch stopwatch = null;

            return(ObjectCacheManager.Get(info.Key,
                                          cache =>
            {
                stopwatch = Stopwatch.StartNew();

                var value = UnderlyingProvider.TryAssert(context, entity, right, dependencies);

                stopwatch.Stop();

                return value;
            },
                                          (cache, value) =>
            {
                if (dependencies.IsCacheable)
                {
                    cache.Insert(info.Key, value, dependencies);

                    if (stopwatch != null)
                    {
                        cache.AddCacheItemTelemetry(info.Key, new CacheItemTelemetry {
                            Duration = stopwatch.Elapsed
                        });
                    }
                }
            }));
        }