Beispiel #1
0
        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));
        }
Beispiel #2
0
        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)));
                }
            }
        }
Beispiel #3
0
        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)));
            }
        }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
        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);
            });
        }