/// <summary> /// Forcefully remove one or more entries from the cache. Note that this /// is currently a O(n^2) operation (i.e. it might be slow). /// </summary> /// <param name="objects">The objects to remove from the cache.</param> public static void Remove(params object[] objects) { Check.VerifyNotNull(objects, Error.NullParameter, "objects"); // determine which cache to use CacheStore cacheStore = GetCacheStore(CacheContentType.Entity); // access cache cacheStore.Lock.AcquireWriterLock(rwLockTimeOut); try { if (objects != null && objects.Length > 0) { foreach (object obj in objects) { if (obj is ICacheKeyProvider) { ICacheKeyProvider ckp = obj as ICacheKeyProvider; cacheStore.Remove(ckp.CacheKey); } else { cacheStore.RemoveObject(obj); } } } } finally { cacheStore.Lock.ReleaseWriterLock(); } }
/// <summary> /// Remove an entry from the cache. /// </summary> /// <param name="key">The key used to find the entry to remove.</param> public static void Remove(string key) { Check.VerifyNotNull(key, Error.NullParameter, "key"); // determine which cache to use CacheStore cacheStore = GetCacheStore(CacheContentType.Entity); // access cache cacheStore.Lock.AcquireWriterLock(rwLockTimeOut); try { cacheStore.Remove(key); } finally { cacheStore.Lock.ReleaseWriterLock(); } }
/// <summary> /// Clear information on query results for select/count queries on the specified type. /// This method is used by Gentle to invalidate obsolete query results when objects /// of the given type are inserted or removed. /// </summary> public static void ClearQueryResultsByType(Type type) { // this is a bit hackish but does the job for now string match = String.Format("{0}|{1}|select ", CacheContentType.Query, type); // process all cache stores and remove query results if invalidated foreach (CacheStore[] cacheStores in stores.Values) { CacheStore cacheStore = cacheStores[(int)CacheContentType.Query]; if (cacheStore != null) { IList removeList = new ArrayList(); cacheStore.Lock.AcquireWriterLock(rwLockTimeOut); try { // build list of items to remove foreach (CacheEntry entry in cacheStore) { if (entry.Key.Length >= match.Length && entry.Key.Substring(0, match.Length).Equals(match)) { removeList.Add(entry.Key); } } // remove found items foreach (string key in removeList) { cacheStore.Remove(key); } } finally { cacheStore.Lock.ReleaseWriterLock(); } } } }