public void Get(string key, Action <PackageCacherResponse> response) { lock (_lock) { if (cacheContent.ContainsKey(key)) { var bundle = cacheContent[key]; bundle.UpdateLastAccessTime(); if (bundle.IsDownloaded) { U.LogDebug(U.Message(IU.FILE_FOUND_BY_KEY, U.Arg("Key", key))); response(new PackageCacherResponse(key, bundle.LocalAddress, null, bundle.RevisionNumber)); } else { U.LogDebug(U.Message(IU.FILE_IS_DOWNLOADING, U.Arg("Key", key))); bundle.PackageDownloaded += (sender, args) => { response(new PackageCacherResponse(key, bundle.LocalAddress, null, bundle.RevisionNumber)); }; } return; } } U.LogDebug(U.Message(IU.KEY_NOT_FOUND, U.Arg("Key", key))); response(new PackageCacherResponse(key, null, null, -1)); }
public void Remove(string key) { lock (_lock) { if (cacheContent.ContainsKey(key)) { cacheContent.Remove(key); U.LogDebug(U.Message(IU.ENTRY_REMOVED, U.Arg("Key", key))); } } }
public void RegisterDownloaded(string key, int revisionNumber) { lock (_lock) { cacheContent[key].RevisionNumber = revisionNumber; cacheContent[key].UpdateLastAccessTime(); cacheContent[key].SetDownloaded(); U.LogDebug(U.Message(IU.ADDRESS_CONFIRMED, U.Arg("Key", key), U.Arg("RevNumber", revisionNumber))); } }
public string ReserveAddress(string key) { lock (_lock) { string localAddress = null; if (cacheContent.ContainsKey(key)) { throw new InvalidDataException(U.Message(IU.NOT_ALLOWED_KEYS_DUPLICATION, U.Arg("Key", key))); } localAddress = GetLocalAddress(key); var bundle = new StaffBundle() { LocalAddress = localAddress, }; cacheContent.Add(key, bundle); U.LogDebug(U.Message(IU.LOCAL_ADDRESS_RESERVED, U.Arg("LocalAddress", localAddress))); return(localAddress); } }
public void Clean() { IEnumerable <string> tempFiles = null; Common.Utility.ExceptionablePlaceWrapper(() => { lock (_lock) { var now = DateTime.Now; var life = new TimeSpan(0, 0, 0, 0, this.lifeInterval); cacheContent = cacheContent.Where( x => !x.Value.IsDownloaded || now.Subtract((DateTime)x.Value.GetLastUsingTime()).Ticks < life.Ticks) .ToDictionary(x => x.Key, x => x.Value); //also remove files survived by some reason from previous iteration //By some reason it means locking of file by reading or writing tempFiles = Directory.EnumerateFiles(this.tempFileDirectory) .Where(x => x.EndsWith(".tmp")).Except(cacheContent.Values.Select(x => x.LocalAddress)); } }, "", "", false); ; Parallel.ForEach(tempFiles, (file) => { Common.Utility.ExceptionablePlaceWrapper(() => { //if file is locked, it won't be deleted. File.Delete(file); }, U.Message(InstallationUtility.FILE_NOT_DELETED, U.Arg("FileName", file)), U.Message(InstallationUtility.FILE_DELETED, U.Arg("FileName", file)), false); }); }