Exemple #1
0
		/// <summary>
		/// Removes all items from the cache with
		/// a certain prefix in the key.
		/// </summary>
		/// <param name="group">The group.</param>
		public virtual void RemoveAll(
			CacheItemGroup group )
		{
			lock ( thisLock )
			{
				if ( backend != null )
				{
					backend.RemoveAll( group );
				}
			}
		}
		// ------------------------------------------------------------------

		/// <summary>
		/// Shortcut for checking whether invalid.
		/// </summary>
		/// <param name="group">The group.</param>
		/// <returns>
		/// 	<c>true</c> if [is null or empty] [the specified group]; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsNullOrEmpty(
			CacheItemGroup group )
		{
			return group == null || group.IsEmpty;
		}
		/// <summary>
		/// Removes all items from the cache with
		/// a certain prefix in the key.
		/// </summary>
		/// <param name="group">The group name of the keys to remove. If the
		/// prefix is NULL or empty, the complete cache is emptied.</param>
		public void RemoveAll(
			CacheItemGroup group )
		{
			lock ( thisLock )
			{
				if ( CacheItemGroup.IsNullOrEmpty( group ) )
				{
					int count = inMemoryCache.Count;

					if ( count > 0 )
					{
						LogCentral.Current.LogDebug(
							string.Format(
							@"[Cache] About to removed {0} cache items at '{1}', " +
							@"ignoring their keys. The items follow:",
							count,
							DateTime.Now ) );

						int index = 1;
						foreach ( string key in inMemoryCache.Keys )
						{
							LogCentral.Current.LogDebug(
								string.Format(
								@"[Cache] Item {0} to remove: key = '{1}', " +
								@"value = '{2}'.",
								index + 1,
								key,
								inMemoryCache[key].Dump() ) );

							index++;
						}
					}

					// Cleaning all.
					inMemoryCache.Clear();

					if ( count > 0 )
					{
						LogCentral.Current.LogDebug(
							string.Format(
							@"[Cache] Removed {0} cache items, ignoring their keys.",
							count ) );
					}
				}
				else
				{
					List<string> keysToRemove = new List<string>();

					string groupName = group.Name;
					foreach ( string key in inMemoryCache.Keys )
					{
						SimpleCacheItemInformation itemInfo =
							inMemoryCache[key];

						if ( !CacheItemGroup.IsNullOrEmpty( group ) &&
							itemInfo != null )
						{
							CacheItemGroup g = itemInfo.Group;

							if ( !CacheItemGroup.IsNullOrEmpty( g ) &&
								g.Name.StartsWith( groupName,
								StringComparison.InvariantCultureIgnoreCase ) )
							{
								keysToRemove.Add( key );
							}
						}
					}

					foreach ( string key in keysToRemove )
					{
						LogCentral.Current.LogDebug(
							string.Format(
							@"[Cache] About to remove cache item at '{0}' with: " +
							@"key = '{1}', value = '{2}'.",
							DateTime.Now,
							key,
							inMemoryCache[key].Dump() ) );

						inMemoryCache.Remove( key );
					}
				}
			}
		}