void InitForMemoryCache(bool isPublic, string[] filenamesArg, string[] cachekeysArg, CacheDependency dependency, DateTime utcStart) {
            bool dispose = true;
            try {
                MemCache memCache = HttpRuntime.CacheInternal as MemCache;
                _bits = new SafeBitVector32(0);
                _utcLastModified = DateTime.MinValue;
                IList<String> files = filenamesArg;
                IList<String> keys = cachekeysArg;
                if (dependency != null) {
                    ReadOnlyCollection<string> filePaths = (dependency._fileChangeMonitor != null) ? dependency._fileChangeMonitor.FilePaths : null;
                    ReadOnlyCollection<string> cacheKeys = (dependency._entryChangeMonitor != null) ? dependency._entryChangeMonitor.CacheKeys : null;
                    if (filePaths != null || filenamesArg != null) {
                        if (filePaths == null) {
                            files = filenamesArg;
                        }
                        else if (filenamesArg == null) {
                            files = filePaths;
                        }
                        else {
                            files = new List<String>(filenamesArg.Length + filePaths.Count);
                            foreach (string f in filenamesArg) {
                                files.Add(f);
                            }
                            foreach (string f in filePaths) {
                                files.Add(f);
                            }
                        }
                    }
                    if (cacheKeys != null || cachekeysArg != null) {
                        if (cacheKeys == null) {
                            keys = cachekeysArg;
                        }
                        else if (cachekeysArg == null) {
                            keys = cacheKeys;
                        }
                        else {
                            keys = new List<String>(cachekeysArg.Length + cacheKeys.Count);
                            foreach (string f in cachekeysArg) {
                                keys.Add(f);
                            }
                            foreach (string f in cacheKeys) {
                                keys.Add(f);
                            }
                        }
                    }
                }
                
                _fileChangeMonitor = (files != null) ? new HostFileChangeMonitor(files) : null;
                _entryChangeMonitor = (keys != null) ? memCache.CreateCacheEntryChangeMonitor(keys, isPublic) : null;
                
                string uniqueId = null;
                
                if (_fileChangeMonitor != null) {
                    _utcLastModified = _fileChangeMonitor.LastModified.UtcDateTime;
                    uniqueId = _fileChangeMonitor.UniqueId;
                    _fileChangeMonitor.NotifyOnChanged(new OnChangedCallback(OnChangedCallback));
                }
                if (_entryChangeMonitor != null) {
                    DateTime utcLastModified = _entryChangeMonitor.LastModified.UtcDateTime;
                    if (utcLastModified > _utcLastModified) {
                        _utcLastModified = utcLastModified;
                    }
                    uniqueId += _entryChangeMonitor.UniqueId;
                    _entryChangeMonitor.NotifyOnChanged(new OnChangedCallback(OnChangedCallback));
                }
                
                _uniqueID = uniqueId;
#if DBG
                _isUniqueIDInitialized = true;
#endif
                // check if file has changed since the start time
                if (utcStart < DateTime.MaxValue) {
                    if (_utcLastModified > utcStart
                        && !(_utcLastModified - DateTime.UtcNow > FUTURE_FILETIME_BUFFER)) {   // See VSWhidbey 400917
                        _bits[CHANGED] = true;
                    }
                }

                _bits[BASE_INIT] = true;
                if (dependency != null && dependency._bits[CHANGED]) {
                    _bits[CHANGED] = true;
                }
                if (_bits[WANTS_DISPOSE] || _bits[CHANGED]) {
                    Debug.Trace("CacheDependencyInit", "WANTS_DISPOSE or CHANGED.  InitForMemoryCache calling DisposeInternal");
                    DisposeInternal();
                }
                dispose = false;
            }
            finally {
                if (dispose) {
                    _bits[BASE_INIT] = true;
                    Debug.Trace("CacheDependencyInit", "\n\nERROR in CacheDependency.InitForMemoryCache, calling DisposeInternal");
                    DisposeInternal();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 添加到缓存
        /// </summary>
        /// <param name="value">被缓存的数据</param>
        /// <param name="cacheKey">缓存key</param>
        /// <param name="filePath">依赖缓存文件的绝对路径</param>
        public static void SetCache_M(this object value, string cacheKey, string dependency_filePath)
        {
            if (!File.Exists(dependency_filePath))
            {
                throw new FileNotFoundException("缓存依赖文件不存在");
            }

            ObjectCache cache = MemoryCache.Default;
            CacheItemPolicy policy = new CacheItemPolicy();

            //缓存优先级别
            policy.Priority = System.Runtime.Caching.CacheItemPriority.Default;

            cache.Set(cacheKey, value, policy);

            //设置监视对象
            HostFileChangeMonitor monitor = new HostFileChangeMonitor(new List<string>() { dependency_filePath });
            //设置监视对象的回调操作
            //依赖文件发生变化 即删除缓存
            monitor.NotifyOnChanged(new OnChangedCallback(o =>
            {
                RemoveCache_M(cacheKey);
            }));
            //添加到监视器
            policy.ChangeMonitors.Add(monitor);

            
        }