示例#1
0
        /// <summary>
        /// Store cache item
        /// </summary>
        private void storeQueue_StoreReady(object sender, StoreReadyEventArgs e)
        {
            CacheItem cache = null;

            lock (_syncObject)
            {
                if (_cacheStatus.ContainsKey(e.ItemKey))
                {
                    CacheItemStatus item = _cacheStatus[e.ItemKey];
                    if (item.HasStored == false)
                    {
                        item.HasStored = true; //mark it as stored
                        cache          = item.Item;
                    }
                }
            }

            if (cache != null)
            {
                try
                {
                    logger.Debug("Store the cache item '{0}'", cache.Key);
                    _storeManager.Store(cache);
                }
                catch (Exception ex)
                {
                    logger.Error("Error ocurred while storing cache: {0}", ex.Message);
                }
            }
        }
示例#2
0
        private void setItemWithPersistance(CacheItem item, int expirationSecond, bool absExpire)
        {
            //clean inactive items
            if (_poolUsedSize + item.Content.Length >= _poolMemorySize)
            {
                cleanUpItems(true);
            }

            if (_poolUsedSize + item.Content.Length >= _poolMemorySize)
            {
                //store directly
                _storeManager.Store(item);
                return;
            }

            //try fetch from cache pool first
            CacheItemStatus existItem;

            if (_cacheStatus.TryGetValue(item.Key, out existItem))
            {
                #region update cache item
                logger.Debug("Update the cache '{0}' in cache pool", item.Key);

                _poolUsedSize += (item.Content.Length - existItem.Item.Content.Length);

                //update the cache in cache pool
                existItem.Item = item;

                //update status //ignore the client specify expiration time
                //existItem.ExpirationSecond = expirationSecond;
                //existItem.AbsoluteExpire = absExpire;
                existItem.LastAccessTime = _nowTime;
                existItem.Hits           = 0;

                //reset this flag
                existItem.HasStored = false;

                //add item to storing queue
                _storeQueue.AddItem(item.Key);
                #endregion
            }
            else
            {
                #region add new cache item
                logger.Debug("Add new cache '{0}' to cache pool", item.Key);

                //create new status object, ignore the client specify expiration time
                CacheItemStatus staus = new CacheItemStatus(
                    item, false, _nowTime, _persistanceItemKeepAliveTime, false); //expirationSecond, absExpire);

                //add to cache pool
                _cacheStatus.Add(item.Key, staus);

                _poolUsedSize += item.Content.Length;

                //add item to storing queue
                _storeQueue.AddItem(item.Key);
                #endregion
            }
        }
示例#3
0
        private void setItem(CacheItem item, int expirationSecond, bool absExpire)
        {
            //clean inactive items
            if (_poolUsedSize + item.Content.Length >= _poolMemorySize)
            {
                cleanUpItems(true);
            }

            if (_poolUsedSize + item.Content.Length >= _poolMemorySize)
            {
                logger.Warn("Node '{0}' out of memory", _nodeId);
                throw new ApplicationException("Out of memory");
            }

            //try fetch from cache pool first
            CacheItemStatus existItem;

            if (_cacheStatus.TryGetValue(item.Key, out existItem))
            {
                #region update cache item
                logger.Debug("Update the cache '{0}' in cache pool", item.Key);

                _poolUsedSize += (item.Content.Length - existItem.Item.Content.Length);

                //update the cache in cache pool
                existItem.Item = item;

                //update status
                existItem.ExpirationSecond = expirationSecond;
                existItem.AbsoluteExpire   = absExpire;
                existItem.LastAccessTime   = _nowTime;
                existItem.Hits             = 0;
                #endregion
            }
            else
            {
                #region add new cache item
                logger.Debug("Add new cache '{0}' to cache pool", item.Key);

                CacheItemStatus staus = new CacheItemStatus(
                    item, true, _nowTime, expirationSecond, absExpire);

                //add to cache pool
                _cacheStatus.Add(item.Key, staus);

                _poolUsedSize += item.Content.Length;
                #endregion
            }
        }
示例#4
0
        private CacheItem getItemWithPersistance(string key)
        {
            //try fetch from cache pool first
            CacheItemStatus existItem;

            if (_cacheStatus.TryGetValue(key, out existItem))
            {
                #region fetch from cache pool
                logger.Debug("Fetch cache item '{0}' from cache pool", key);

                //update status
                if (!existItem.AbsoluteExpire)
                {
                    existItem.LastAccessTime = _nowTime;
                }

                if (existItem.Hits < int.MaxValue)
                {
                    existItem.Hits++;
                }

                return(new CacheItem(
                           key, existItem.Item.Content,
                           existItem.Item.ItemTime, existItem.Item.Properties));

                #endregion
            }
            else
            {
                #region fetch from storing
                logger.Debug("Fetch cache item '{0}' from storing manager", key);

                //fetch from storing
                try
                {
                    CacheItem item = _storeManager.Fetch(key);

                    if (_poolUsedSize + item.Content.Length >= _poolMemorySize)
                    {
                        cleanUpItems(true);
                    }

                    if (_poolUsedSize + item.Content.Length >= _poolMemorySize)
                    {
                        //return cache item directly
                        return(item);
                    }

                    //insert item into cache pool
                    CacheItemStatus status = new CacheItemStatus(
                        item, true, _nowTime, _persistanceItemKeepAliveTime, false);
                    _cacheStatus.Add(key, status);

                    //add used size
                    _poolUsedSize += item.Content.Length;

                    return(item);
                }
                catch (StoringException)
                {
                    throw new ItemNotFoundException("Cache not found");
                }
                #endregion
            }
        }