Пример #1
0
        /// <summary>
        /// Flushes a cached page and it's parent page's list of child pages whenever a page is updated
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.ModelUpdatedEventArgs"/> instance containing the event data.</param>
        void Page_Updated(object sender, Rock.Data.ModelUpdatedEventArgs e)
        {
            // Get a reference to the updated page
            Rock.Model.Page page = e.Model as Rock.Model.Page;
            if (page != null)
            {
                // Check to see if the page being updated is cached
                System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default;
                if (cache.Contains(Rock.Web.Cache.PageCache.CacheKey(page.Id)))
                {
                    // Get the cached page
                    var cachedPage = Rock.Web.Cache.PageCache.Read(page.Id);

                    // if the parent page has changed, flush the old parent page's list of child pages
                    if (cachedPage.ParentPage != null && cachedPage.ParentPage.Id != page.ParentPageId)
                    {
                        cachedPage.ParentPage.FlushChildPages();
                    }

                    // Flush the updated page from cache
                    Rock.Web.Cache.PageCache.Flush(page.Id);
                }

                // Check to see if updated page has a parent
                if (page.ParentPageId.HasValue)
                {
                    // If the parent page is cached, flush it's list of child pages
                    if (cache.Contains(Rock.Web.Cache.PageCache.CacheKey(page.ParentPageId.Value)))
                    {
                        Rock.Web.Cache.PageCache.Read(page.ParentPageId.Value).FlushChildPages();
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 更新Cache
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        public static bool UpdateStore <TValue>(this System.Runtime.Caching.ObjectCache cache,
                                                string key,
                                                TValue item)
        {
            bool result = false;

            if (string.IsNullOrEmpty(key))
            {
                return(result);
            }

            if (cache.Contains(key))       //檢查cache 是否有對應的資料
            {
                var instance = cache[key]; //取得該筆資料

                lock (instance)
                {
                    var instanceValue = ((Util.CacheExtensions.Container <TValue>)instance).Value; //取的該筆資料的值

                    foreach (var property in instanceValue.GetType().GetProperties())              //取得該筆資料的型別與內容物件
                    {
                        if (item.GetType().GetProperty(property.Name) != null)
                        {
                            property.SetValue(instanceValue, item.GetType().GetProperty(property.Name).GetValue(item));//更新內容
                        }
                    }
                    result = true;
                }
            }

            return(result);
        }
Пример #3
0
 public static void RemoveItem(String CacheKeyName)
 {
     //
     if (cache.Contains(CacheKeyName))
     {
         cache.Remove(CacheKeyName);
     }
 }
Пример #4
0
        /// <summary>
        /// Retrieves the asset. Tries local storage first, then moves on to the remote storage systems.
        /// If neither could find the data, or if there is no remote storage set up, the failure callback is called.
        /// </summary>
        /// <param name="assetId">Asset identifier.</param>
        /// <param name="successCallback">Callback called when the asset was successfully found.</param>
        /// <param name="failureCallback">Callback called when there was a failure attempting to get the asset.</param>
        /// <param name="storeResultLocally">Specifies to locally store the asset if it was fetched from a remote.</param>
        public void GetAsset(Guid assetId, SuccessCallback successCallback, FailureCallback failureCallback, bool storeResultLocally = true)
        {
            successCallback = successCallback ?? throw new ArgumentNullException(nameof(successCallback));
            failureCallback = failureCallback ?? throw new ArgumentNullException(nameof(failureCallback));
            if (assetId == Guid.Empty)
            {
                throw new ArgumentException("Asset ID cannot be zero.", nameof(assetId));
            }

            if (_negativeCache != null)
            {
                _negativeCacheLock.EnterReadLock();
                try {
                    if (_negativeCache.Contains(assetId.ToString("N")))
                    {
                        failureCallback();
                        return;
                    }
                }
                finally {
                    _negativeCacheLock.ExitReadLock();
                }
            }

            // Solves GET in middle of PUT situation.
            if (_localStorage.Contains(assetId) && !_localStorage.AssetWasWrittenToDisk(assetId))
            {
                // Asset exists, just might not be on disk yet. Wait here until the asset makes it to disk.
                SpinWait.SpinUntil(() => _localStorage.AssetWasWrittenToDisk(assetId));
            }

            _assetReader.GetAssetAsync(assetId, asset => {
                if (asset != null)
                {
                    successCallback(asset);
                    return;
                }

                failureCallback();

                if (_negativeCache != null)
                {
                    _negativeCacheLock.EnterWriteLock();
                    try {
                        _negativeCache.Set(new System.Runtime.Caching.CacheItem(assetId.ToString("N"), 0), _negativeCachePolicy);
                    }
                    finally {
                        _negativeCacheLock.ExitWriteLock();
                    }
                }
            }, storeResultLocally ? ChattelReader.CacheRule.Normal : ChattelReader.CacheRule.SkipWrite);
        }
Пример #5
0
        /// <summary>
        /// 移除Cache
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        public static void RemoveStore(this System.Runtime.Caching.ObjectCache cache,
                                       string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return;
            }

            lock (cache)
            {
                if (cache.Contains(key))
                {
                    cache.Remove(key);
                }
            }
        }
 public T Obtener <T>(string nombre) where T : class
 {
     try
     {
         if (cache.Contains(nombre))
         {
             return((T)cache[nombre]);
         }
         else
         {
             return(null);
         }
     }
     catch
     {
         return(null);
     }
 }
Пример #7
0
        /// <summary>
        /// Flushes a cached page and it's parent page's list of child pages whenever a page is being deleted
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.ModelUpdatingEventArgs"/> instance containing the event data.</param>
        void Page_Deleting(object sender, Rock.Data.ModelUpdatingEventArgs e)
        {
            // Get a reference to the deleted page
            Rock.Model.Page page = e.Model as Rock.Model.Page;
            if (page != null)
            {
                // Check to see if the page being updated is cached
                System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default;
                if (cache.Contains(Rock.Web.Cache.PageCache.CacheKey(page.Id)))
                {
                    // Get the cached page
                    var cachedPage = Rock.Web.Cache.PageCache.Read(page.Id);

                    // if the parent page is not null, flush parent page's list of child pages
                    if (cachedPage.ParentPage != null)
                    {
                        cachedPage.ParentPage.FlushChildPages();
                    }

                    // Flush the updated page from cache
                    Rock.Web.Cache.PageCache.Flush(page.Id);
                }
            }
        }
Пример #8
0
        public void RequestTextureAssetOnCap(Guid capId, Guid assetId, AssetRequest.AssetRequestHandler handler, AssetRequest.AssetErrorHandler errHandler)
        {
            handler    = handler ?? throw new ArgumentNullException(nameof(handler));
            errHandler = errHandler ?? throw new ArgumentNullException(nameof(errHandler));

            if (_negativeCache != null)
            {
                _negativeCacheLock.EnterReadLock();
                var negCacheContainsId = false;
                try {
                    negCacheContainsId = _negativeCache.Contains(assetId.ToString("N"));
                }
                finally {
                    _negativeCacheLock.ExitReadLock();
                }

                if (negCacheContainsId)
                {
                    errHandler(new AssetError {
                        Error = AssetErrorType.AssetTypeWrong,
                    });
                    return;
                }
            }

            if (_caps.TryGetValue(capId, out Capability cap))
            {
                cap.RequestAsset(
                    assetId,
                    (asset) => {
                    if (!ConfigSingleton.ValidTypes.Any(type => type == asset.Type))
                    {
                        if (_negativeCache != null)
                        {
                            _negativeCacheLock.EnterWriteLock();
                            try {
                                _negativeCache.Set(new System.Runtime.Caching.CacheItem(assetId.ToString("N"), 0), _negativeCachePolicy);
                            }
                            finally {
                                _negativeCacheLock.ExitWriteLock();
                            }
                        }

                        errHandler(new AssetError {
                            Error = AssetErrorType.AssetTypeWrong,
                        });
                    }

                    switch (asset.Type)
                    {
                    case 0:
                    case 12:
                    case 18:
                    case 19:
                        handler(asset);
                        break;

                    default:
                        errHandler(new AssetError {
                            Error = AssetErrorType.AssetTypeWrong,
                        });
                        break;
                    }
                },
                    errHandler
                    );

                return;
            }

            errHandler(new AssetError {
                Error = AssetErrorType.CapabilityIdUnknown,
            });
        }