System.Web.Caching.Cache.Get C# (CSharp) Method

Get() public method

public Get ( string key ) : object
key string
return object
		public object Get (string key)
		{
			try {
				cacheLock.EnterUpgradeableReadLock ();
				CacheItem it = cache [key];
				if (it == null)
					return null;
				
				if (it.Dependency != null && it.Dependency.HasChanged) {
					try {
						cacheLock.EnterWriteLock ();
						if (!NeedsUpdate (it, CacheItemUpdateReason.DependencyChanged, false))
							Remove (it.Key, CacheItemRemovedReason.DependencyChanged, false, true);
					} finally {
						// See comment at the top of the file, above cacheLock declaration
						cacheLock.ExitWriteLock ();
					}
					
					return null;
				}

				if (!DisableExpiration) {
					if (it.SlidingExpiration != NoSlidingExpiration) {
						it.AbsoluteExpiration = DateTime.Now + it.SlidingExpiration;
						// Cast to long is ok since we know that sliding expiration
						// is less than 365 days (31536000000ms)
						long remaining = (long)it.SlidingExpiration.TotalMilliseconds;
						it.ExpiresAt = it.AbsoluteExpiration.Ticks;
						
						if (expirationTimer != null && (expirationTimerPeriod == 0 || expirationTimerPeriod > remaining)) {
							expirationTimerPeriod = remaining;
							expirationTimer.Change (expirationTimerPeriod, expirationTimerPeriod);
						}
					
					} else if (DateTime.Now >= it.AbsoluteExpiration) {
						try {
							cacheLock.EnterWriteLock ();
							if (!NeedsUpdate (it, CacheItemUpdateReason.Expired, false))
								Remove (key, CacheItemRemovedReason.Expired, false, true);
						} finally {
							// See comment at the top of the file, above cacheLock declaration
							cacheLock.ExitWriteLock ();
						}

						return null;
					}
				}
				
				return it.Value;
			} finally {
				// See comment at the top of the file, above cacheLock declaration
				cacheLock.ExitUpgradeableReadLock ();
			}
		}
		

Usage Example

Example #1
0
        // Executed at runtime, before the method.
        public override void OnEntry(MethodExecutionArgs eventArgs)
        {
            // Compose the cache key.
            string key = _formatStrings.Format(
                eventArgs.Instance, eventArgs.Method, eventArgs.Arguments.ToArray());

            // Test whether the cache contains the current method call.
            var value = Cache.Get(key);

            if (ReferenceEquals(value, null))
            {
                lock (Cache)
                {
                    value = Cache.Get(key);
                    if (ReferenceEquals(value, null))
                    {
                        // If not, we will continue the execution as normally.
                        // We store the key in a state variable to have it in the OnExit method.
                        eventArgs.MethodExecutionTag = key;
                    }
                    else
                    {
                        // If it is in cache, we set the cached value as the return value
                        // and we force the method to return immediately.
                        eventArgs.ReturnValue  = value;
                        eventArgs.FlowBehavior = FlowBehavior.Return;
                    }
                }
            }
            else
            {
                eventArgs.ReturnValue  = value;
                eventArgs.FlowBehavior = FlowBehavior.Return;
            }
        }
All Usage Examples Of System.Web.Caching.Cache::Get