Cache - Removing All Items

G

Guadala Harry

I want to create a STATIC method that removes all items from the Cache.

Questions:
1. Is a safe thing to do (any threading issues?)

2. Is the following code a good way to get the job done - given that there
is apparently no method like Cache.Clear() or Cache.Items.Clear()? Or am I
missing an easier way to do it?

string currentKey= "";
System.Collections.IDictionaryEnumerator cacheContents =
System.Web.HttpContext.Current.Cache.GetEnumerator();
while (cacheContents.MoveNext()) {
currentKey = cacheContents.Key.ToString();
System.Web.HttpContext.Current.Cache.Remove(currentKey);
}

Thanks!

-GH
 
B

bruce barker

a better approach is to use a dependancy key in the cache, and change.

-- bruce (sqlwork.com)

| I want to create a STATIC method that removes all items from the Cache.
|
| Questions:
| 1. Is a safe thing to do (any threading issues?)
|
| 2. Is the following code a good way to get the job done - given that there
| is apparently no method like Cache.Clear() or Cache.Items.Clear()? Or am I
| missing an easier way to do it?
|
| string currentKey= "";
| System.Collections.IDictionaryEnumerator cacheContents =
| System.Web.HttpContext.Current.Cache.GetEnumerator();
| while (cacheContents.MoveNext()) {
| currentKey = cacheContents.Key.ToString();
| System.Web.HttpContext.Current.Cache.Remove(currentKey);
| }
|
| Thanks!
|
| -GH
|
|
 
J

Joe Fallon

I would not do that.
There are other things that use the cache too.
You aren't the only thing adding stuff to the cache.
You could seriously break stuff.

Use cache dependencies or expirations instead soyou can control the things
that you created and added to the cache.
 
G

Guadala Harry

<<You aren't the only thing adding stuff to the cache>>

That's a big one... didn't know that. Thank you.
Too bad I can't have a dependency on database tables. I already put things
in the Cache with a sliding expiration - but anything still in the Cache
reflecting the current state of the underlying database must get removed
from the Cache whenever the database gets updated. Short of waiting until
ASP.NET/2, is there any elegant way to create a dependency between Cached
data and SQL Server db tables? The only time I explicitly blow things out of
the Cache is when the db gets updated.

-GH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top