Exemplo n.º 1
0
        override protected bool PreAddIndexableHook(Indexable indexable)
        {
            // None of this applies for Removes
            if (indexable.Type == IndexableType.Remove)
            {
                return(true);
            }

            CachedFileInfo info = (CachedFileInfo)file_info_cache [indexable.Uri];

            if (info == null)
            {
                info = new CachedFileInfo();
            }

            info.Uri = indexable.Uri;

            if (indexable.Uri.IsFile && indexable.IsNonTransient)
            {
                info.Path = indexable.Uri.LocalPath;
            }
            else if (indexable.ContentUri.IsFile && indexable.IsNonTransient)
            {
                info.Path = indexable.ContentUri.LocalPath;
            }
            else if (indexable.ParentUri != null && indexable.ParentUri.IsFile)
            {
                info.Path   = indexable.ParentUri.LocalPath;
                info.Shared = true;
            }

            // The path could be null in certain cases:
            //    * The indexable is a non-file URI and no
            //      parent URI is set.
            //    * The indexable is a child indexable and the
            //      parent URI is not a file URI.
            if (info.Path == null)
            {
                return(true);
            }

            info.Mtime = FileSystem.GetLastWriteTimeUtc(info.Path);

            if (!FileSystem.ExistsByDateTime(info.Mtime))
            {
                // If we can't get an mtime for the file, it must
                // have disappeared out from under us.  In that case,
                // don't bother adding anything.
                return(false);
            }

            file_info_cache [info.Uri] = info;
            // If we are all set to authorize this indexable, increment reference count for the path
            if (info.Shared)
            {
                IncrementReferenceCount(info.Path);
            }

            return(true);
        }
Exemplo n.º 2
0
        public virtual void UpdateCachedFile(string sourcePath, string internalPath, string genFilePath)
        {
            var fullSourcePath = Path.GetFullPath(sourcePath);
            var lastWriteTime  = File.GetLastWriteTime(sourcePath);

            var internalFilePath = Path.GetFullPath(Path.Combine(CacheDirectory, "files", internalPath));

            // Create directory if one doesn't exist
            var internalFileDirectory = Path.GetDirectoryName(internalFilePath);

            CreateDirectory(internalFileDirectory);

            // Copy gen file to internal
            File.Copy(genFilePath, internalFilePath, true);

            if (MappedCachedFiles.TryGetValue(internalPath, out var info))
            {
                // Update existing
                info.SourcePath  = fullSourcePath;
                info.LastUpdated = lastWriteTime;
            }
            else
            {
                // Add new entry
                var newInfo = new CachedFileInfo()
                {
                    SourcePath   = fullSourcePath,
                    InternalPath = internalPath,
                    LastUpdated  = lastWriteTime
                };

                MappedCachedFiles.Add(newInfo.InternalPath, newInfo);
            }
        }
        internal bool MeetsOverMaxCriteria(CachedFileInfo i)
        {
            DateTime now = DateTime.UtcNow;

            //Only require the 'used' date to comply if it 1) doesn't match created date and 2) is above 0
            return((now.Subtract(i.AccessedUtc) > ProhibitRemovalIfUsedWithin || ProhibitRemovalIfUsedWithin <= new TimeSpan(0) || i.AccessedUtc == i.UpdatedUtc) &&
                   (now.Subtract(i.UpdatedUtc) > ProhibitRemovalIfCreatedWithin || ProhibitRemovalIfCreatedWithin <= new TimeSpan(0)));
        }
Exemplo n.º 4
0
        private static bool RefreshFilesInFolder(CachedFolderInfo folderInfo, IDirectoryCacheProvider provider, bool raiseCacheChanged)
        {
            Dictionary <string, CachedFileInfo> files = folderInfo.Files;

            Dictionary <string, CachedFileInfo> oldFiles = null;

            if (!raiseCacheChanged)
            {
                oldFiles = new Dictionary <string, CachedFileInfo>();
                foreach (var oldFile in files)
                {
                    oldFiles.Add(oldFile.Key, oldFile.Value);
                }
            }

            files.Clear();
            var directoryEntries = provider.GetFilesInFolder(folderInfo.FullPath).ToList();

            foreach (var file in directoryEntries)
            {
                var fileInfo = new CachedFileInfo(file.Name, file.LastWriteTimeUtc);
                files.Add(fileInfo.NormalisedName, fileInfo);
                if (oldFiles != null)
                {
                    CachedFileInfo oldFile;
                    if (!oldFiles.TryGetValue(fileInfo.NormalisedName, out oldFile))
                    {
                        raiseCacheChanged = true;
                    }
                    else
                    {
                        if (oldFile.LastWriteTimeUtc != fileInfo.LastWriteTimeUtc)
                        {
                            raiseCacheChanged = true;
                        }
                        oldFiles.Remove(fileInfo.NormalisedName);
                    }
                }
            }

            if (!raiseCacheChanged && oldFiles != null && oldFiles.Count != 0)
            {
                raiseCacheChanged = true;
            }

            return(raiseCacheChanged);
        }
Exemplo n.º 5
0
        public void FlushAccessedDate(CleanupWorkItem item)
        {
            CachedFileInfo c = cache.Index.GetCachedFileInfo(item.RelativePath);

            if (c == null)
            {
                return;            //File was already deleted, nothing to do.
            }
            try{
                cache.Locks.TryExecuteSynchronous(item.RelativePath.ToUpperInvariant(), 1, CancellationToken.None, delegate
                {
                    File.SetLastAccessTimeUtc(item.PhysicalPath, c.AccessedUtc);
                });
                //In both of these exception cases, we don't care.
            }catch (FileNotFoundException) {
            }catch (UnauthorizedAccessException) {
            }
        }
Exemplo n.º 6
0
        public IFileInfo GetFileInfo(string subpath)
        {
            // TODO: Normalize the subpath here, e.g. strip/always-add leading slashes, ensure slash consistency, etc.
            var       key = nameof(GetFileInfo) + "_" + subpath;
            IFileInfo cachedResult;

            if (_cache.TryGetValue(key, out cachedResult))
            {
                // Item already exists in cache, just return it
                return(cachedResult);
            }

            var fileInfo = _fileProvider.GetFileInfo(subpath);

            if (!fileInfo.Exists)
            {
                // Requested subpath doesn't exist, just return it
                return(fileInfo);
            }

            if (fileInfo.Length > _fileSizeLimit)
            {
                // File is too large to cache, just return it
                _logger.LogTrace("File contents for {subpath} will not be cached as it's over the file size limit of {fileSizeLimit}", subpath, _fileSizeLimit);
                return(fileInfo);
            }

            // Create the cache entry and return
            var cachedFileInfo   = new CachedFileInfo(_logger, fileInfo, subpath);
            var fileChangedToken = Watch(subpath);

            fileChangedToken.RegisterChangeCallback(_ => _logger.LogDebug("Change detected for {subpath} located at {filepath}", subpath, fileInfo.PhysicalPath), null);
            var cacheEntry = _cache.CreateEntry(key)
                             .RegisterPostEvictionCallback((k, value, reason, s) =>
                                                           _logger.LogTrace("Cache entry {key} was evicted due to {reason}", k, reason))
                             .AddExpirationToken(fileChangedToken)
                             .SetValue(cachedFileInfo);

            // You have to call Dispose() to actually add the item to the underlying cache. Yeah, I know.
            cacheEntry.Dispose();
            return(cachedFileInfo);
        }
 internal bool ShouldRemove(string relativePath, CachedFileInfo info, bool isOverMax)
 {
     return(isOverMax ? MeetsOverMaxCriteria(info) : MeetsCleanupCriteria(info));
 }
Exemplo n.º 8
0
		override protected bool PreAddIndexableHook (Indexable indexable)
		{
			// None of this applies for Removes
			if (indexable.Type == IndexableType.Remove)
				return true;

			CachedFileInfo info = (CachedFileInfo) file_info_cache [indexable.Uri];

			if (info == null)
				info = new CachedFileInfo ();

			info.Uri = indexable.Uri;

			if (indexable.Uri.IsFile && indexable.IsNonTransient)
				info.Path = indexable.Uri.LocalPath;
			else if (indexable.ContentUri.IsFile && indexable.IsNonTransient)
				info.Path = indexable.ContentUri.LocalPath;
			else if (indexable.ParentUri != null && indexable.ParentUri.IsFile) {
				info.Path = indexable.ParentUri.LocalPath;
				info.Shared = true;
			}

			// The path could be null in certain cases:
			//    * The indexable is a non-file URI and no
			//      parent URI is set.
			//    * The indexable is a child indexable and the
			//      parent URI is not a file URI.
			if (info.Path == null)
				return true;

			info.Mtime = FileSystem.GetLastWriteTimeUtc (info.Path);

			if (! FileSystem.ExistsByDateTime (info.Mtime)) {
				// If we can't get an mtime for the file, it must
				// have disappeared out from under us.  In that case,
				// don't bother adding anything.
				return false;
			}

			file_info_cache [info.Uri] = info;
			// If we are all set to authorize this indexable, increment reference count for the path
			if (info.Shared)
				IncrementReferenceCount (info.Path);

			return true;
		}