예제 #1
0
        private void AddKeyInUse(string key)
        {
            var keysInUse = GetKeysInUse();

            keysInUse.Add(key);

            HttpRuntime.Cache.Insert(keysInUseKey.GetKey(), keysInUse, null, DateTime.MaxValue, new TimeSpan(12, 0, 0), CacheItemPriority.Default, null);
        }
예제 #2
0
        /// <summary>
        /// Remove an item from the session.
        /// </summary>
        /// <param name="model">Key model.</param>
        public virtual void Remove(KeyModel model)
        {
            DefaultNamespace(ref model);

            var key = model.GetKey();

            HttpContext.Current.Session.Remove(key);
        }
예제 #3
0
        /// <summary>
        /// Remove an item from the cache.
        /// </summary>
        /// <param name="model">Key model.</param>
        public void Remove(KeyModel model)
        {
            var key = model.GetKey();

            // Remove key from list of keys in use
            RemoveKeyInUse(key);

            HttpRuntime.Cache.Remove(key);
        }
예제 #4
0
        public void GetKeyTest()
        {
            const string key      = "Key";
            var          target   = new KeyModel(key);
            const string expected = "Test.Key";
            string       actual   = target.GetKey();

            Assert.AreEqual(expected, actual);
        }
예제 #5
0
        /// <summary>
        /// Remove an item from the cache.
        /// </summary>
        /// <param name="model">Key model.</param>
        public void Remove(KeyModel model)
        {
            var key = model.GetKey();

            //if (!excludeFromKeysInUse.Contains(model.Namespace))
            //{
            //    // Remove key from list of keys in use
            //    RemoveKeyInUse(key);
            //}

            HttpRuntime.Cache.Remove(key);
        }
예제 #6
0
        private void DefaultNamespace(CacheType cacheType, ref string key)
        {
            var @namespace = GetType().Namespace;

            if (!string.IsNullOrEmpty(key) && !key.Contains(@namespace))
            {
                var model = new KeyModel(cacheType, key);

                model.Namespace = GetType().Namespace;

                key = model.GetKey();
            }
        }
예제 #7
0
        /// <summary>
        /// Try to get a stored object.
        /// </summary>
        /// <typeparam name="T">Type of stored item.</typeparam>
        /// <param name="model">Key model.</param>
        /// <param name="value">Stored value.</param>
        /// <returns>Stored item as type.</returns>
        public bool TryGet <T>(KeyModel model, out T value)
        {
            try
            {
                value = (T)Cache.Get(model.GetKey());
            }
            catch (DataCacheException e)
            {
                value = default(T);

                Elmah.ErrorSignal.FromCurrentContext().Raise(e);
            }

            return(!Equals(value, default(T)));
        }
예제 #8
0
        ///// <summary>
        ///// Remove all variations of an item from the cache based on a key name.
        ///// </summary>
        ///// <remarks>
        ///// Uses <see cref="CacheType.Default" />.
        ///// </remarks>
        ///// <param name="key">Key used in key model.</param>
        //public void Remove(string key)
        //{
        //    Remove(CacheType.Default, key);
        //}

        ///// <summary>
        ///// Remove all variations of an item from the cache based on a key name.
        ///// </summary>
        ///// <remarks>
        ///// Note that <see cref="CacheServiceWrapper" /> has already included the namespace and cache type in the key value.
        ///// </remarks>
        ///// <param name="cacheType">Cache type to use.</param>
        ///// <param name="key">Key used in key model.</param>
        //public void Remove(CacheType cacheType, string key)
        //{
        //    var keyInUseNamespace = string.Empty;
        //    if (!string.IsNullOrEmpty(key) && key.IndexOf(']') >= 0)
        //    {
        //        keyInUseNamespace=  key.Split(new char[] {'[', ']'},StringSplitOptions.RemoveEmptyEntries)[0];
        //    }

        //   var keysInUse = GetKeysInUse(keyInUseNamespace);

        //    // Remove from cache for each variation of the key name in use
        //    foreach (var k in keysInUse.Where(m => m.StartsWith(key,StringComparison.Ordinal)))
        //    {
        //        try
        //        {
        //            Cache.Remove(k);
        //        }
        //        catch (DataCacheException e)
        //        {
        //            Elmah.ErrorSignal.FromCurrentContext().Raise(e);
        //        }
        //    }

        //    // Remove variations of key from list of keys in use
        //    RemoveKeyInUseVariations(key);
        //}

        /// <summary>
        /// Checks if the item is stored in the cache.
        /// </summary>
        /// <param name="model">Key model.</param>
        /// <returns><c>true</c> if the item is stored in the cache; otherwise, <c>false</c>.</returns>
        public bool Contains(KeyModel model)
        {
            try
            {
                var item = Cache.Get(model.GetKey());

                return(item != null);
            }
            catch (DataCacheException e)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(e);
            }

            return(false);
        }
예제 #9
0
        ///// <summary>
        ///// Current activity ID the user is working with.
        ///// </summary>
        //public long ActivityID
        //{
        //    get { return GetContextID<long>(ContextType.Activity); }
        //    set { SetContextID(ContextType.Activity, value); }
        //}

        ///// <summary>
        ///// Current appointment ID the user is working with.
        ///// </summary>
        //public long AppointmentID
        //{
        //    get { return GetContextID<long>(ContextType.Appointment); }
        //    set { SetContextID(ContextType.Appointment, value); }
        //}

        ///// <summary>
        ///// Current contract ID the user is working with.
        ///// </summary>
        //public string ContractID
        //{
        //    get { return GetContextID<string>(ContextType.Contract); }
        //    set { SetContextID(ContextType.Contract, value); }
        //}

        ///// <summary>
        ///// Current Centrelink reference number the user is working with.
        ///// </summary>
        //public string CRN
        //{
        //    get { return GetContextID<string>(ContextType.CRN); }
        //    set { SetContextID(ContextType.CRN, value); }
        //}

        ///// <summary>
        ///// Current employer ID the user is working with.
        ///// </summary>
        //public long EmployerID
        //{
        //    get { return GetContextID<long>(ContextType.Employer); }
        //    set { SetContextID(ContextType.Employer, value); }
        //}

        ///// <summary>
        ///// Current job seeker ID the user is working with.
        ///// </summary>
        //public long JobSeekerID
        //{
        //    get { return GetContextID<long>(ContextType.JobSeeker); }
        //    set { SetContextID(ContextType.JobSeeker, value); }
        //}

        ///// <summary>
        ///// Current override ID the user is working with.
        ///// </summary>
        //public long OverrideID
        //{
        //    get { return GetContextID<long>(ContextType.Override); }
        //    set { SetContextID(ContextType.Override, value); }
        //}

        ///// <summary>
        ///// Current payment ID the user is working with.
        ///// </summary>
        //public long PaymentID
        //{
        //    get { return GetContextID<long>(ContextType.Payment); }
        //    set { SetContextID(ContextType.Payment, value); }
        //}

        ///// <summary>
        ///// Current provider ID the user is working with.
        ///// </summary>
        //public long ProviderID
        //{
        //    get { return GetContextID<long>(ContextType.Provider); }
        //    set { SetContextID(ContextType.Provider, value); }
        //}

        ///// <summary>
        ///// Current vacancy ID the user is working with.
        ///// </summary>
        //public long VacancyID
        //{
        //    get { return GetContextID<long>(ContextType.Vacancy); }
        //    set { SetContextID(ContextType.Vacancy, value); }
        //}

        ///// <summary>
        ///// Get the value of a context ID.
        ///// </summary>
        ///// <remarks>
        ///// For internal use by Infrastructure only. Use the ID properties instead.
        ///// </remarks>
        ///// <typeparam name="T">The object type of the context ID.</typeparam>
        ///// <param name="contextType">The context type.</param>
        ///// <returns>The context ID value.</returns>
        //public T GetContextID<T>(ContextType contextType)
        //{
        //    var model = new KeyModel("ContextIDs");

        //    DefaultNamespace(ref model);

        //    Dictionary<ContextType, object> contextTypes;

        //    if (TryGet(model, out contextTypes) && contextTypes.ContainsKey(contextType))
        //    {
        //        return (T)contextTypes[contextType];
        //    }

        //    return default(T);
        //}

        ///// <summary>
        ///// Set the value of a context ID.
        ///// </summary>
        ///// <remarks>
        ///// For internal use by Infrastructure only. Use the ID properties instead.
        ///// </remarks>
        ///// <typeparam name="T">The object type of the context ID.</typeparam>
        ///// <param name="contextType">The context type.</param>
        ///// <param name="value">The value to set the context ID to.</param>
        //public void SetContextID<T>(ContextType contextType, T value)
        //{
        //    var model = new KeyModel("ContextIDs");

        //    DefaultNamespace(ref model);

        //    Dictionary<ContextType, object> contextTypes;

        //    if (!TryGet(model, out contextTypes))
        //    {
        //        contextTypes = new Dictionary<ContextType, object>();
        //    }

        //    if (contextTypes.ContainsKey(contextType))
        //    {
        //        contextTypes[contextType] = value;
        //    }
        //    else
        //    {
        //        contextTypes.Add(contextType, value);
        //    }

        //    Set(model, contextTypes);
        //}

        /// <summary>
        /// Set an item in the session under a unique key.
        /// </summary>
        /// <typeparam name="T">Type of item.</typeparam>
        /// <param name="model">Key model.</param>
        /// <param name="o">Item to be stored in session.</param>
        /// <exception cref="TypeMismatchException">Thrown if the session already has an object set with the key and its actual type does not match the expected type.</exception>
        public virtual void Set <T>(KeyModel model, T o)
        {
            DefaultNamespace(ref model);
            T data;

            if (TryGet(model, out data))
            {
                if (typeof(T) != data.GetType())
                {
                    throw new TypeMismatchException(typeof(T), data.GetType());
                }
            }

            var key = model.GetKey();

            HttpContext.Current.Session[key] = o;
        }
예제 #10
0
        /// <summary>
        /// Set an item in the cache under a unique key for a set amount of time.
        /// </summary>
        /// <typeparam name="T">Type of item.</typeparam>
        /// <param name="model">Key model.</param>
        /// <param name="o">Item to be stored in cache.</param>
        /// <param name="timeToLive">Time for item to live in cache.</param>
        public void Set <T>(KeyModel model, T o, TimeSpan timeToLive)
        {
            var key = model.GetKey();

            try
            {
                Cache.Put(key, o, timeToLive);

                //if (!excludeFromKeysInUse.Contains(model.Namespace))
                //{
                //    // Add key to list of keys in use
                //    AddKeyInUse(key);
                //}
            }
            catch (DataCacheException e)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(e);
            }
        }
예제 #11
0
        /// <summary>
        /// Remove an item from the cache.
        /// </summary>
        /// <param name="model">Key model.</param>
        public void Remove(KeyModel model)
        {
            var key = model.GetKey();

            //if (!excludeFromKeysInUse.Contains(model.Namespace))
            //{
            //    // Remove key from list of keys in use
            //    RemoveKeyInUse(key);
            //}

            try
            {
                Cache.Remove(key);
            }
            catch (DataCacheException e)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(e);
            }
        }
예제 #12
0
        /// <summary>
        /// Set an item in the cache under a unique key.
        /// </summary>
        /// <typeparam name="T">Type of item.</typeparam>
        /// <param name="o">Item to be stored in cache.</param>
        /// <param name="model">Key model.</param>
        /// <param name="minutes">Number of minutes to store.</param>
        /// <param name="priority">Priority of the item. Note that <see cref="CacheItemPriority.NotRemovable" /> is only to be used by Infrastructure.</param>
        /// <exception cref="TypeMismatchException">Thrown if the cache already has an object set with the key and its actual type does not match the expected type.</exception>
        internal void Set <T>(KeyModel model, T o, int minutes, CacheItemPriority priority)
        {
            T data;

            if (TryGet(model, out data))
            {
                if (typeof(T) != data.GetType())
                {
                    throw new TypeMismatchException(typeof(T), data.GetType());
                }
            }

            var key = model.GetKey();

            // Add key to list of keys in use
            AddKeyInUse(key);

            HttpRuntime.Cache.Insert(key, o, null, DateTime.Now.AddMinutes(minutes), Cache.NoSlidingExpiration, priority, null);
        }
예제 #13
0
        /// <summary>
        /// Set an item in the cache under a unique key.
        /// </summary>
        /// <typeparam name="T">Type of item.</typeparam>
        /// <param name="o">Item to be stored in cache.</param>
        /// <param name="model">Key model.</param>
        /// <param name="minutes">Number of minutes to store.</param>
        /// <param name="priority">Priority of the item. Note that <see cref="CacheItemPriority.NotRemovable" /> is only to be used by Infrastructure.</param>
        /// <exception cref="TypeMismatchException">Thrown if the cache already has an object set with the key and its actual type does not match the expected type.</exception>
        internal void Set <T>(KeyModel model, T o, int minutes, CacheItemPriority priority)
        {
            T data;

            if (TryGet(model, out data))
            {
                if (typeof(T) != data.GetType())
                {
                    throw new TypeMismatchException(typeof(T), data.GetType());
                }
            }

            var key = model.GetKey();

            //if (!excludeFromKeysInUse.Contains(model.Namespace))
            //{
            //    // Add key to list of keys in use
            //    AddKeyInUse(key);
            //}
            HttpRuntime.Cache.Insert(key, o, null, UserService.DateTime.AddMinutes(minutes), Cache.NoSlidingExpiration, priority, null);
        }
예제 #14
0
        /// <summary>
        /// Try to get a stored object.
        /// </summary>
        /// <typeparam name="T">Type of stored item.</typeparam>
        /// <param name="model">Key model.</param>
        /// <param name="value">Stored value.</param>
        /// <returns>Stored item as type.</returns>
        public bool TryGet <T>(KeyModel model, out T value)
        {
            try
            {
                if (!Contains(model))
                {
                    value = default(T);

                    return(false);
                }

                value = (T)HttpRuntime.Cache[model.GetKey()];
            }
            catch
            {
                value = default(T);

                return(false);
            }

            return(true);
        }
예제 #15
0
        /// <summary>
        /// Try to get a stored object.
        /// </summary>
        /// <typeparam name="T">Type of stored item.</typeparam>
        /// <param name="model">Key model.</param>
        /// <param name="value">Stored value.</param>
        /// <returns>Stored item as type.</returns>
        public virtual bool TryGet <T>(KeyModel model, out T value)
        {
            DefaultNamespace(ref model);

            try
            {
                if (!Contains(model))
                {
                    value = default(T);

                    return(false);
                }

                value = (T)HttpContext.Current.Session[model.GetKey()];
            }
            catch
            {
                value = default(T);

                return(false);
            }

            return(true);
        }
예제 #16
0
 /// <summary>
 /// Checks if the item is stored in the cache.
 /// </summary>
 /// <param name="model">Key model.</param>
 /// <returns>true if the item is stored in the cache; otherwise, false.</returns>
 public bool Contains(KeyModel model)
 {
     return(HttpRuntime.Cache[model.GetKey()] != null);
 }
예제 #17
0
        /// <summary>
        /// Checks if the item is stored in the session.
        /// </summary>
        /// <param name="model">Key model.</param>
        /// <returns><c>true</c> if the item is stored in the session; otherwise, <c>false</c>.</returns>
        public virtual bool Contains(KeyModel model)
        {
            DefaultNamespace(ref model);

            return(HttpContext.Current.Session[model.GetKey()] != null);
        }