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

Add() public method

public Add ( string key, object value, System.Web.Caching.CacheDependency dependencies, System.DateTime absoluteExpiration, System.TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback ) : object
key string
value object
dependencies System.Web.Caching.CacheDependency
absoluteExpiration System.DateTime
slidingExpiration System.TimeSpan
priority CacheItemPriority
onRemoveCallback CacheItemRemovedCallback
return object
		public object Add (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
		{
			if (key == null)
				throw new ArgumentNullException ("key");
			
			try {
				cacheLock.EnterWriteLock ();
				CacheItem it = cache [key];

				if (it != null)
					return it.Value;
				Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, null, false);
			} finally {
				// See comment at the top of the file, above cacheLock declaration
				cacheLock.ExitWriteLock ();
			}
				
			return null;
		}
		

Usage Example

        public static AggregationCategorizationService GetService(Cache cache, String userId)
        {

            try
            {
                if (cache["AggCatService_" + userId] == null)
                {
                    string certificateFile = System.Configuration.ConfigurationManager.AppSettings["PrivateKeyPath"];
                    string password = System.Configuration.ConfigurationManager.AppSettings["PrivateKeyPassword"];
                    X509Certificate2 certificate = new X509Certificate2(certificateFile, password);

                    string consumerKey = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"];
                    string consumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"];
                    string issuerId = System.Configuration.ConfigurationManager.AppSettings["SAMLIdentityProviderID"];

                    SamlRequestValidator samlValidator = new SamlRequestValidator(certificate, consumerKey, consumerSecret, issuerId, userId);

                    ServiceContext ctx = new ServiceContext(samlValidator);
                    cache.Add("AggCatService_" + userId, new AggregationCategorizationService(ctx), null, DateTime.Now.AddMinutes(50),
                              Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                }
                return (AggregationCategorizationService)cache["AggCatService_" + userId];
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to create AggCatService: " + ex.Message);
            }
        }
All Usage Examples Of System.Web.Caching.Cache::Add