Ref caching documented here:
Is there a smart way of invalidating the cache?
You can supply a cache dependency to the method in which you can do pretty much anything you'd like. If you're using GetContentResult the automatic caching does that using a dependency that "fires" whenever EPiServer's master cache key is updated.
Here's the thing: I'm caching all product queries for 10 minutes, but I want to be able to "clear the cache". So my plan was to use a cache dependency and then create a plugin where an admin editor can "reset cache" by changing the dependency.
Keeping in mind that the cache dependency will be evaluated everytime the query runs, what would be the best way of implenting the cache dependency? I'm concerned that the cost of evaluating the cache dependency will be the same as the benefit from caching... if you see what I'm saying.
Given that I'm not misunderstanding you I think you could do:
1. Create a constant string somewhere that is used as a cache key below. Let's say it's in a class named Banana with property name Key.
2. When querying do:
2.1 Cache.Add(Banana.Key, "I'm a string whos value doesn't matter, to bad for me."); (Vital to use Cache.Add and not Cache.Insert)
2.2. Execute the query with a dependency that monitors Banana.Key (using this constructor).
3. When editor presses "reset cache" do EPiServer.CacheManager.Remove(Banana.Key)
Note that Cache.Add doesn't modify the cache if the key already exist meaning that it will only be added after the application has just started or an editor has pressed "reset cached". By using EPiServer's cache manager when removing you'll clear the value on all servers meaning the query's cache will be cleared on all servers.
The link in 2.2 to the CacheDependency constructor - can you explain the use of the datetime parameter?
No :) This looks better :)
Great, thanks! :)