Example #1
0
        /// <summary>
        /// Creates a scoped layered cache
        /// </summary>
        /// <param name="name"></param>
        /// <param name="level1">First cache to check (e.g. in-memory scoped cache)</param>
        /// <param name="level2">Fallback cache (e.g. distributed scoped cache)</param>
        public LayeredScopedCache(String name, IScopedCache level1, IScopedCache level2)
        {
            this.Name = name;

            if (level1 == null)
            {
                throw new ApplicationException("innerCache1 must not be null");
            }

            if (level2 == null)
            {
                throw new ApplicationException("innerCache2 must not be null");
            }

            if (level2 == level1)
            {
                throw new ApplicationException(
                          string.Format("level2 must not be the same as level1, received {0}={1} and {2}={3}",
                                        "level1", level1.Name, "level2", level2.Name));
            }

            this.level1 = level1;
            this.level2 = level2;

            this.policy = new LayeredScopedCachePolicy {
                Level1CacheName = level1.Name, Level2CacheName = level2.Name
            };
        }
Example #2
0
        /// <summary>
        /// Creates a scoped layered cache
        /// </summary>
        /// <param name="name"></param>
        /// <param name="level1CacheName">Name of first cache to check (e.g. in-memory scoped cache), should be registered in CacheManager</param>
        /// <param name="level2CacheName">Name of fallback cache (e.g. distributed scoped cache), should be registered in CacheManager</param>
        public LayeredScopedCache(String name, String level1CacheName, String level2CacheName)
        {
            this.Name = name;

            var level1 = CacheManager.GetCache(level1CacheName);

            if (!(level1 is IScopedCache))
            {
                var message = (level1 == null)
                    ? $"Cache is not registered: level1CacheName={level1CacheName}"
                    : $"Cache type does not implement {nameof(IScopedCache)}: level1CacheName={level1CacheName}";
                throw new ApplicationException(message);
            }

            var level2 = CacheManager.GetCache(level2CacheName);

            if (!(level2 is IScopedCache))
            {
                var message = (level2 == null)
                    ? $"Cache is not registered: level2CacheName={level2CacheName}"
                    : $"Cache type does not implement {nameof(IScopedCache)}: level2CacheName={level2CacheName}";
                throw new ApplicationException(message);
            }

            if (level2 == level1)
            {
                throw new ApplicationException(
                          string.Format(
                              "level2 must not be the same as level1, received {0}={1}, {2}={3}, which map to {4} and {5}",
                              "level1CacheName", level1CacheName,
                              "level2CacheName", level2CacheName,
                              level1.Name,
                              level2.Name));
            }

            this.level1 = (IScopedCache)level1;
            this.level2 = (IScopedCache)level2;

            this.policy = new LayeredScopedCachePolicy {
                Level1CacheName = level1.Name, Level2CacheName = level2.Name
            };
        }
Example #3
0
        public LayeredScopedCache(String name, LayeredScopedCachePolicy policy)
            : this(name, policy?.Level1CacheName, policy?.Level2CacheName)
        {
            this.policy = policy ?? throw new ArgumentNullException(nameof(policy));

            if (policy.InvalidateLevel1OnLevel2Upsert)
            {
                if (this.level1 is NoCache)
                {
                    policy.InvalidateLevel1OnLevel2Upsert = false;
                }
                else
                {
                    level1Notifier = CacheManager.GetAssociatedNotifier(this.level1);
                    if (level1Notifier == null)
                    {
                        throw new ApplicationException(
                                  "InvalidateLevel1OnLevel2Upsert requires level1 cache to have SyncProvider defined in policy: level1CacheName=" +
                                  policy.Level1CacheName);
                    }
                }
            }
        }