Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void CacheContinously()
- {
- Monitor.Enter(this);
- while(Monitor.Wait(this) && _continouslyCaching) {
- QueueEntry currentEntry;
- while(GetNext(out currentEntry)) {
- bool canSkip = _queueEntries.Count > 1;
- Monitor.Exit(this);
- ICacheable cacheable = currentEntry.Cacheable;
- bool toCache = currentEntry.ToCache;
- bool gotLock;
- if(canSkip) {
- gotLock = false;
- Monitor.TryEnter(cacheable, ref gotLock);
- } else {
- Monitor.Enter(cacheable);
- gotLock = true;
- }
- if(gotLock) {
- if(toCache) {
- if(!cacheable.IsCached) {
- #if SPAM_CACHING_MESSAGES
- Console.WriteLine("Caching {0}...", cacheable);
- #endif
- cacheable.Cache();
- cacheCount++;
- #if SPAM_CACHING_MESSAGES
- Console.WriteLine("Cached {0}!", cacheable);
- #endif
- }
- } else if(cacheable.IsCached) {
- #if SPAM_CACHING_MESSAGES
- Console.WriteLine("Uncaching {0}...", cacheable);
- #endif
- cacheable.Uncache();
- cacheCount--;
- #if SPAM_CACHING_MESSAGES
- Console.WriteLine("Uncached {0}!", cacheable);
- #endif
- }
- #if SPAM_CACHING_MESSAGES
- Console.WriteLine("Total cached items: {0}!", cacheCount);
- #endif
- Monitor.Exit(cacheable);
- }
- Monitor.Enter(this);
- if(!gotLock) {
- if(!IsQueued(cacheable, !toCache)) {
- Enqueue(cacheable, toCache);
- }
- }
- }
- }
- Monitor.Exit(this);
- }
Advertisement
Add Comment
Please, Sign In to add comment