Exemple #1
0
        // common implementation
        static internal CacheInternal Create() {
            CacheCommon cacheCommon = new CacheCommon();
            CacheInternal cacheInternal;
#if USE_MEMORY_CACHE
            if (UseMemoryCache) {
                cacheInternal = new MemCache(cacheCommon);
                cacheCommon.AddSRefTarget(cacheInternal);
            }
            else {
#endif
                int numSubCaches = 0;
                uint numCPUs = (uint) SystemInfo.GetNumProcessCPUs();
                // the number of subcaches is the minimal power of 2 greater
                // than or equal to the number of cpus
                numSubCaches = 1;
                numCPUs -= 1;
                while (numCPUs > 0) {
                    numSubCaches <<= 1;
                    numCPUs >>= 1;
                }
                if (numSubCaches == 1) {
                    cacheInternal = new CacheSingle(cacheCommon, null, 0);
                }
                else {
                    cacheInternal = new CacheMultiple(cacheCommon, numSubCaches);
                }
#if USE_MEMORY_CACHE
            }
#endif
            cacheCommon.SetCacheInternal(cacheInternal);
            cacheCommon.ResetFromConfigSettings();

            return cacheInternal;
        }
        void InitForMemoryCache(bool isPublic, string[] filenamesArg, string[] cachekeysArg, CacheDependency dependency, DateTime utcStart)
        {
            bool dispose = true;

            try {
                MemCache memCache = HttpRuntime.InternalCache 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();
                }
            }
        }