예제 #1
0
        public async Task CopyAsync(CachedData data, string export)
        {
            data.Lock();

            try
            {
                using (var input = await data.CreateStreamAsync())
                    using (FileStream dest = new FileStream(export, FileMode.Open, FileAccess.Write))
                    {
                        await input.CopyToAsync(dest).ConfigureAwait(false);
                    }
            }
            finally
            {
                data.Unlock();
            }
        }
예제 #2
0
        /// <summary>
        /// Enforce the manager to add/update cache from <see cref="CacheFactory"/>
        /// </summary>
        /// <param name="key"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        public async Task <CachedData> CacheAsync(string key, CacheFactory factory, bool lockData)
        {
            CachedData data = new CachedData(key);
            await data.Semaphore.WaitAsync();

            CachedData caching = null;

            lock (lockObj)
            {
                if (Caching.ContainsKey(key))
                {
                    caching = Caching[key];
                }
                else
                {
                    data.Lock();
                    Caching.Add(key, data);
                }
            }

            if (caching != null)
            {
                await caching.Semaphore.WaitAsync();

                caching.Semaphore.Release();
                return(caching);
            }

            using (var fs = AllocateTemporaryFile(factory, out var path))
            {
                await factory.Factory(fs);

                data.Path = path;
                data.Size = fs.Length;

                TotalSize += fs.Length;
                TotalCount++;
            }

            CachedData last = null;

            lock (lockObj)
            {
                Caching.Remove(key);

                if (Caches.ContainsKey(key))
                {
                    last = Caches[key];
                }

                data.Disposed += DataOnDisposed;
                Caches[key]    = data;
            }

            last?.Dispose();

            CheckCaches();

            if (!lockData)
            {
                data.Unlock();
            }
            data.Semaphore.Release();

            Cached?.Invoke(this, new CachedEventArgs(data));

            return(data);
        }