コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TridionChangeMonitor"/> class.
        /// </summary>
        /// <param name="tridionDependency">Associated <see cref="T:TcmCDService.Cache.TridionDependency" /></param>
        /// <exception cref="System.ArgumentNullException">tridionDependency</exception>
        public TridionChangeMonitor(TridionDependency tridionDependency)
        {
            if (tridionDependency == null)
            {
                throw new ArgumentNullException("tridionDependency");
            }

            bool flag = true;

            try
            {
                mTridionDependency           = tridionDependency;
                mTridionDependency.OnChange += new EventHandler(this.OnDependencyChanged);
                mUniqueId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);

                flag = false;
            }
            finally
            {
                base.InitializationComplete();

                if (flag)
                {
                    base.Dispose();
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TridionChangeMonitor"/> class.
        /// </summary>
        /// <param name="tridionDependency">Associated <see cref="T:TcmCDService.Cache.TridionDependency" /></param>
        /// <exception cref="System.ArgumentNullException">tridionDependency</exception>
        public TridionChangeMonitor(TridionDependency tridionDependency)
        {
            if (tridionDependency == null)
                throw new ArgumentNullException("tridionDependency");

            bool flag = true;

            try
            {
                mTridionDependency = tridionDependency;
                mTridionDependency.OnChange += new EventHandler(this.OnDependencyChanged);
                mUniqueId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);

                flag = false;
            }
            finally
            {
                base.InitializationComplete();

                if (flag)
                    base.Dispose();
            }
        }
コード例 #3
0
        /// <summary>
        /// Gets the specified key.
        /// </summary>
        /// <typeparam name="T">Cache item type</typeparam>
        /// <param name="key">Cache item key</param>
        /// <param name="valueInitializer">Cache item value initializer.</param>
        /// <returns>Cached item or newly created item</returns>
        public static T Get <T>(String key, Func <T> valueInitializer, CacheRegion cacheRegion, TcmUri cacheUri) where T : class
        {
            // No caching is applied
            if (mExpiration == -1)
            {
                return(valueInitializer());
            }

            MemoryCache cache = MemoryCache.Default;

            // Retrieve the value from the cache as the generic requested type
            var value = cache.Get(key);

            if (value != null)
            {
                T cachedItem = value as T;

                if (cachedItem != null)
                {
                    return(cachedItem);
                }

                return(default(T));
            }

            // Initialize the value if its not existing in the cache
            if (valueInitializer != null)
            {
                T initializedValue = valueInitializer();

                // Store the item in cache only if it returned an actual value
                if (!EqualityComparer <T> .Default.Equals(initializedValue, default(T)))
                {
                    CacheItemPolicy cacheItemPolicy = new CacheItemPolicy()
                    {
                        AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(Expiration),
                        RemovedCallback    = new CacheEntryRemovedCallback(ItemRemovedCallback)
                    };

                    if (cacheRegion != CacheRegion.Unknown)
                    {
                        String cacheKey = cacheUri.ToCacheKey();

                        TridionDependency dependency = mDependencies.FirstOrDefault(d => d.CacheRegion == cacheRegion && d.Key == cacheKey);

                        // Dependency did not exist yet, create it and add it to the cache dependencies list
                        if (dependency == null)
                        {
                            dependency = new TridionDependency(cacheRegion, cacheKey);
                            mDependencies.Add(dependency);
                        }

                        cacheItemPolicy.ChangeMonitors.Add(new TridionChangeMonitor(dependency));
                    }

                    cache.Set(key, initializedValue, cacheItemPolicy);
                    return(initializedValue);
                }
            }

            return(default(T));
        }