/// <summary>
        /// Tries to fetch the result from disk cache, the memory queue, or create it. If the memory queue has space,
        /// the writeCallback() will be executed and the resulting bytes put in a queue for writing to disk.
        /// If the memory queue is full, writing to disk will be attempted synchronously.
        /// In either case, writing to disk can also fail if the disk cache is full and eviction fails.
        /// If the memory queue is full, eviction will be done synchronously and can cause other threads to time out
        /// while waiting for QueueLock
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dataProviderCallback"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="retrieveContentType"></param>
        /// <returns></returns>
        /// <exception cref="OperationCanceledException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public async Task <AsyncCacheResult> GetOrCreateBytes(
            byte[] key,
            AsyncBytesResult dataProviderCallback,
            CancellationToken cancellationToken,
            bool retrieveContentType)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException(cancellationToken);
            }

            var swGetOrCreateBytes = Stopwatch.StartNew();
            var entry = new CacheEntry(key, PathBuilder);

            // Tell cleanup what we're using
            CleanupManager.NotifyUsed(entry);

            // Fast path on disk hit
            var swFileExists = Stopwatch.StartNew();

            var fileBasedResult = await TryGetFileBasedResult(entry, false, retrieveContentType, cancellationToken);

            if (fileBasedResult != null)
            {
                return(fileBasedResult);
            }
            // Just continue on creating the file. It must have been deleted between the calls

            swFileExists.Stop();



            var cacheResult = new AsyncCacheResult();

            //Looks like a miss. Let's enter a lock for the creation of the file. This is a different locking system
            // than for writing to the file
            //This prevents two identical requests from duplicating efforts. Different requests don't lock.

            //Lock execution using relativePath as the sync basis. Ignore casing differences. This prevents duplicate entries in the write queue and wasted CPU/RAM usage.
            var queueLockComplete = await QueueLocks.TryExecuteAsync(entry.StringKey,
                                                                     Options.WaitForIdenticalRequestsTimeoutMs, cancellationToken,
                                                                     async() =>
            {
                var swInsideQueueLock = Stopwatch.StartNew();

                // Now, if the item we seek is in the queue, we have a memcached hit.
                // If not, we should check the filesystem. It's possible the item has been written to disk already.
                // If both are a miss, we should see if there is enough room in the write queue.
                // If not, switch to in-thread writing.

                var existingQueuedWrite = CurrentWrites.Get(entry.StringKey);

                if (existingQueuedWrite != null)
                {
                    cacheResult.Data        = existingQueuedWrite.GetReadonlyStream();
                    cacheResult.ContentType = existingQueuedWrite.ContentType;
                    cacheResult.Detail      = AsyncCacheDetailResult.MemoryHit;
                    return;
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException(cancellationToken);
                }

                swFileExists.Start();
                // Fast path on disk hit, now that we're in a synchronized state
                var fileBasedResult2 = await TryGetFileBasedResult(entry, true, retrieveContentType, cancellationToken);
                if (fileBasedResult2 != null)
                {
                    cacheResult = fileBasedResult2;
                    return;
                }
                // Just continue on creating the file. It must have been deleted between the calls

                swFileExists.Stop();

                var swDataCreation = Stopwatch.StartNew();
                //Read, resize, process, and encode the image. Lots of exceptions thrown here.
                var result = await dataProviderCallback(cancellationToken);
                swDataCreation.Stop();

                //Create AsyncWrite object to enqueue
                var w = new AsyncWrite(entry.StringKey, result.Item2, result.Item1);

                cacheResult.Detail      = AsyncCacheDetailResult.Miss;
                cacheResult.ContentType = w.ContentType;
                cacheResult.Data        = w.GetReadonlyStream();

                // Create a lambda which we can call either in a spawned Task (if enqueued successfully), or
                // in this task, if our buffer is full.
                async Task <AsyncCacheDetailResult> EvictWriteAndLogUnsynchronized(bool queueFull, TimeSpan dataCreationTime, CancellationToken ct)
                {
                    var delegateStartedAt = DateTime.UtcNow;
                    var swReserveSpace    = Stopwatch.StartNew();
                    //We only permit eviction proceedings from within the queue or if the queue is disabled
                    var allowEviction      = !queueFull || CurrentWrites.MaxQueueBytes <= 0;
                    var reserveSpaceResult = await CleanupManager.TryReserveSpace(entry, w.ContentType,
                                                                                  w.GetUsedBytes(), allowEviction, EvictAndWriteLocks, ct);
                    swReserveSpace.Stop();

                    var syncString = queueFull ? "synchronous" : "async";
                    if (!reserveSpaceResult.Success)
                    {
                        Logger?.LogError(
                            queueFull
                                    ? "HybridCache synchronous eviction failed; {Message}. Time taken: {1}ms - {2}"
                                    : "HybridCache async eviction failed; {Message}. Time taken: {1}ms - {2}",
                            syncString, reserveSpaceResult.Message, swReserveSpace.ElapsedMilliseconds,
                            entry.RelativePath);

                        return(AsyncCacheDetailResult.CacheEvictionFailed);
                    }

                    var swIo = Stopwatch.StartNew();
                    // We only force an immediate File.Exists check when running from the Queue
                    // Otherwise it happens inside the lock
                    var fileWriteResult = await FileWriter.TryWriteFile(entry, delegate(Stream s, CancellationToken ct2)
                    {
                        if (ct2.IsCancellationRequested)
                        {
                            throw new OperationCanceledException(ct2);
                        }

                        var fromStream = w.GetReadonlyStream();
                        return(fromStream.CopyToAsync(s, 81920, ct2));
                    }, !queueFull, Options.WaitForIdenticalDiskWritesMs, ct);
                    swIo.Stop();

                    var swMarkCreated = Stopwatch.StartNew();
                    // Mark the file as created so it can be deleted
                    await CleanupManager.MarkFileCreated(entry,
                                                         w.ContentType,
                                                         w.GetUsedBytes(),
                                                         DateTime.UtcNow);
                    swMarkCreated.Stop();

                    switch (fileWriteResult)
                    {
                    case CacheFileWriter.FileWriteStatus.LockTimeout:
                        //We failed to lock the file.
                        Logger?.LogWarning("HybridCache {Sync} write failed; disk lock timeout exceeded after {IoTime}ms - {Path}",
                                           syncString, swIo.ElapsedMilliseconds, entry.RelativePath);
                        return(AsyncCacheDetailResult.WriteTimedOut);

                    case CacheFileWriter.FileWriteStatus.FileAlreadyExists:
                        Logger?.LogTrace("HybridCache {Sync} write found file already exists in {IoTime}ms, after a {DelayTime}ms delay and {CreationTime}- {Path}",
                                         syncString, swIo.ElapsedMilliseconds,
                                         delegateStartedAt.Subtract(w.JobCreatedAt).TotalMilliseconds,
                                         dataCreationTime, entry.RelativePath);
                        return(AsyncCacheDetailResult.FileAlreadyExists);

                    case CacheFileWriter.FileWriteStatus.FileCreated:
                        if (queueFull)
                        {
                            Logger?.LogTrace(@"HybridCache synchronous write complete. Create: {CreateTime}ms. Write {WriteTime}ms. Mark Created: {MarkCreatedTime}ms. Eviction: {EvictionTime}ms - {Path}",
                                             Math.Round(dataCreationTime.TotalMilliseconds).ToString(CultureInfo.InvariantCulture).PadLeft(4),
                                             swIo.ElapsedMilliseconds.ToString().PadLeft(4),
                                             swMarkCreated.ElapsedMilliseconds.ToString().PadLeft(4),
                                             swReserveSpace.ElapsedMilliseconds.ToString().PadLeft(4), entry.RelativePath);
                        }
                        else
                        {
                            Logger?.LogTrace(@"HybridCache async write complete. Create: {CreateTime}ms. Write {WriteTime}ms. Mark Created: {MarkCreatedTime}ms Eviction {EvictionTime}ms. Delay {DelayTime}ms. - {Path}",
                                             Math.Round(dataCreationTime.TotalMilliseconds).ToString(CultureInfo.InvariantCulture).PadLeft(4),
                                             swIo.ElapsedMilliseconds.ToString().PadLeft(4),
                                             swMarkCreated.ElapsedMilliseconds.ToString().PadLeft(4),
                                             swReserveSpace.ElapsedMilliseconds.ToString().PadLeft(4),
                                             Math.Round(delegateStartedAt.Subtract(w.JobCreatedAt).TotalMilliseconds).ToString(CultureInfo.InvariantCulture).PadLeft(4),
                                             entry.RelativePath);
                        }

                        return(AsyncCacheDetailResult.WriteSucceeded);

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                async Task <AsyncCacheDetailResult> EvictWriteAndLogSynchronized(bool queueFull,
                                                                                 TimeSpan dataCreationTime, CancellationToken ct)
                {
                    var cacheDetailResult = AsyncCacheDetailResult.Unknown;
                    var writeLockComplete = await EvictAndWriteLocks.TryExecuteAsync(entry.StringKey,
                                                                                     Options.WaitForIdenticalRequestsTimeoutMs, cancellationToken,
                                                                                     async() =>
                    {
                        cacheDetailResult =
                            await EvictWriteAndLogUnsynchronized(queueFull, dataCreationTime, ct);
                    });
                    if (!writeLockComplete)
                    {
                        cacheDetailResult = AsyncCacheDetailResult.EvictAndWriteLockTimedOut;
                    }

                    return(cacheDetailResult);
                }


                var swEnqueue   = Stopwatch.StartNew();
                var queueResult = CurrentWrites.Queue(w, async delegate
                {
                    try
                    {
                        var unused = await EvictWriteAndLogSynchronized(false, swDataCreation.Elapsed, CancellationToken.None);
                    }
                    catch (Exception ex)
                    {
                        Logger?.LogError(ex, "HybridCache failed to flush async write, {Exception} {Path}\n{StackTrace}", ex.ToString(),
                                         entry.RelativePath, ex.StackTrace);
                    }
                });
                swEnqueue.Stop();
                swInsideQueueLock.Stop();
                swGetOrCreateBytes.Stop();

                if (queueResult == AsyncWriteCollection.AsyncQueueResult.QueueFull)
                {
                    if (Options.WriteSynchronouslyWhenQueueFull)
                    {
                        var writerDelegateResult = await EvictWriteAndLogSynchronized(true, swDataCreation.Elapsed, cancellationToken);
                        cacheResult.Detail       = writerDelegateResult;
                    }
                }
            });
        /// <summary>
        /// May return either a physical file name or a MemoryStream with the data.
        /// Faster than GetCachedFile, as writes are (usually) asynchronous. If the write queue is full, the write is forced to be synchronous again.
        /// Identical to GetCachedFile() when asynchronous=false
        /// </summary>
        /// <param name="keyBasis"></param>
        /// <param name="extension"></param>
        /// <param name="writeCallback"></param>
        /// <param name="timeoutMs"></param>
        /// <returns></returns>
        public CacheResult GetCachedFile(string keyBasis, string extension, ResizeImageDelegate writeCallback, int timeoutMs, bool asynchronous)
        {
            Stopwatch sw = null;

            if (lp.Logger != null)
            {
                sw = new Stopwatch(); sw.Start();
            }

            // Path to the file in the blob container
            string      path   = new UrlHasher().Hash(keyBasis) + '.' + extension;
            CacheResult result = new CacheResult(CacheQueryResult.Hit, path);

            bool asyncFailed = false;

            //2013-apr-25: What happens if the file is still being written to blob storage - it's present but not complete? To handle that, we use mayBeLocked.
            bool mayBeLocked = Locks.MayBeLocked(path.ToUpperInvariant());

            // On the first check, verify the file exists by connecting to the blob directly
            if (!asynchronous)
            {
                //May throw an IOException if the file cannot be opened, and is locked by an external processes for longer than timeoutMs.
                //This method may take longer than timeoutMs under absolute worst conditions.
                if (!TryWriteFile(result, path, writeCallback, timeoutMs, !mayBeLocked))
                {
                    //On failure
                    result.Result = CacheQueryResult.Failed;
                }
            }
            else if (!Index.PathExistInIndex(path) || mayBeLocked)
            {
                //Looks like a miss. Let's enter a lock for the creation of the file. This is a different locking system than for writing to the file - far less contention, as it doesn't include the
                //This prevents two identical requests from duplicating efforts. Different requests don't lock.

                //Lock execution using relativePath as the sync basis. Ignore casing differences. This prevents duplicate entries in the write queue and wasted CPU/RAM usage.
                if (!QueueLocks.TryExecute(path.ToUpperInvariant(), timeoutMs,
                                           delegate() {
                    //Now, if the item we seek is in the queue, we have a memcached hit. If not, we should check the index. It's possible the item has been written to disk already.
                    //If both are a miss, we should see if there is enough room in the write queue. If not, switch to in-thread writing.

                    AsyncWrite t = CurrentWrites.Get(path);

                    if (t != null)
                    {
                        result.Data = t.GetReadonlyStream();
                    }

                    //On the second check, use cached data for speed. The cached data should be updated if another thread updated a file (but not if another process did).
                    //When t == null, and we're inside QueueLocks, all work on the file must be finished, so we have no need to consult mayBeLocked.
                    if (t == null && !Index.PathExistInIndex(path))
                    {
                        result.Result = CacheQueryResult.Miss;
                        //Still a miss, we even rechecked the filesystem. Write to memory.
                        MemoryStream ms = new MemoryStream(4096);      //4K initial capacity is minimal, but this array will get copied around alot, better to underestimate.
                        //Read, resize, process, and encode the image. Lots of exceptions thrown here.
                        writeCallback(ms);
                        ms.Position = 0;

                        AsyncWrite w = new AsyncWrite(CurrentWrites, ms, path);
                        if (CurrentWrites.Queue(w, delegate(AsyncWrite job) {
                            try
                            {
                                Stopwatch swio = new Stopwatch();

                                swio.Start();
                                //TODO: perhaps a different timeout?
                                if (!TryWriteFile(null, job.Path, delegate(Stream s) { ((MemoryStream)job.GetReadonlyStream()).WriteTo(s); }, timeoutMs, true))
                                {
                                    swio.Stop();
                                    //We failed to lock the file.
                                    if (lp.Logger != null)
                                    {
                                        lp.Logger.Warn("Failed to flush async write, timeout exceeded after {1}ms - {0}", result.Path, swio.ElapsedMilliseconds);
                                    }
                                }
                                else
                                {
                                    swio.Stop();
                                    if (lp.Logger != null)
                                    {
                                        lp.Logger.Trace("{0}ms: Async write started {1}ms after enqueue for {2}", swio.ElapsedMilliseconds.ToString().PadLeft(4), DateTime.UtcNow.Subtract(w.JobCreatedAt).Subtract(swio.Elapsed).TotalMilliseconds, result.Path);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (lp.Logger != null)
                                {
                                    lp.Logger.Error("Failed to flush async write, {0} {1}\n{2}", ex.ToString(), result.Path, ex.StackTrace);
                                }
                            }
                            finally
                            {
                                CurrentWrites.Remove(job);     //Remove from the queue, it's done or failed.
                            }
                        }))
                        {
                            //We queued it! Send back a read-only memory stream
                            result.Data = w.GetReadonlyStream();
                        }
                        else
                        {
                            asyncFailed = false;
                            //We failed to queue it - either the ThreadPool was exhausted or we exceeded the MB limit for the write queue.
                            //Write the MemoryStream to disk using the normal method.
                            //This is nested inside a queuelock because if we failed here, the next one will also. Better to force it to wait until the file is written to blob storage.
                            if (!TryWriteFile(result, path, delegate(Stream s) { ms.WriteTo(s); }, timeoutMs, false))
                            {
                                if (lp.Logger != null)
                                {
                                    lp.Logger.Warn("Failed to queue async write, also failed to lock for sync writing: {0}", result.Path);
                                }
                            }
                        }
                    }
                }))
                {
                    //On failure
                    result.Result = CacheQueryResult.Failed;
                }
            }
            if (lp.Logger != null)
            {
                sw.Stop();
                lp.Logger.Trace("{0}ms: {3}{1} for {2}, Key: {4}", sw.ElapsedMilliseconds.ToString(NumberFormatInfo.InvariantInfo).PadLeft(4), result.Result.ToString(), result.Path, asynchronous ? (asyncFailed ? "Fallback to sync  " : "Async ") : "", keyBasis);
            }

            //Fire event
            if (CacheResultReturned != null)
            {
                CacheResultReturned(this, result);
            }
            return(result);
        }
        /// <summary>
        /// May return either a physical file name or a MemoryStream with the data.
        /// Writes are (usually) asynchronous. If the write queue is full, the write is forced to be synchronous again.
        /// Identical to GetCachedFile() when asynchronous=false
        /// </summary>
        /// <param name="keyBasis"></param>
        /// <param name="extension"></param>
        /// <param name="writeCallback"></param>
        /// <param name="timeoutMs"></param>
        /// <param name="asynchronous"></param>
        /// <returns></returns>
        public async Task <CacheResult> GetCachedFile(string keyBasis, string extension, AsyncWriteResult writeCallback, int timeoutMs, bool asynchronous)
        {
            Stopwatch sw = null;

            if (logger != null)
            {
                sw = Stopwatch.StartNew();
            }

            //Relative to the cache directory. Not relative to the app or domain root
            var keyBasisBytes = new UTF8Encoding().GetBytes(keyBasis);
            var relativePath  = new HashBasedPathBuilder().BuildPath(keyBasisBytes, subfolders, "/") + '.' + extension;

            //Physical path
            var physicalPath = PhysicalCachePath.TrimEnd('\\', '/') + Path.DirectorySeparatorChar +
                               relativePath.Replace('/', Path.DirectorySeparatorChar);


            var result = new CacheResult(CacheQueryResult.Hit, physicalPath, relativePath);

            var asyncFailed = false;


            //2013-apr-25: What happens if the file is still being written to disk - it's present but not complete? To handle that, we use mayBeLocked.

            var mayBeLocked = Locks.MayBeLocked(relativePath.ToUpperInvariant());

            //On the first check, verify the file exists using System.IO directly (the last 'true' parameter).
            if (!asynchronous)
            {
                //On the first check, verify the file exists using System.IO directly (the last 'true' parameter)
                //May throw an IOException if the file cannot be opened, and is locked by an external processes for longer than timeoutMs.
                //This method may take longer than timeoutMs under absolute worst conditions.
                if (!await TryWriteFile(result, physicalPath, relativePath, writeCallback, timeoutMs, !mayBeLocked))
                {
                    //On failure
                    result.Result = CacheQueryResult.Failed;
                }
            }
            else if (!Index.ExistsCertain(relativePath, physicalPath) || mayBeLocked)
            {
                //Looks like a miss. Let's enter a lock for the creation of the file. This is a different locking system than for writing to the file - far less contention, as it doesn't include the
                //This prevents two identical requests from duplicating efforts. Different requests don't lock.

                //Lock execution using relativePath as the sync basis. Ignore casing differences. This prevents duplicate entries in the write queue and wasted CPU/RAM usage.
                if (!await QueueLocks.TryExecuteAsync(relativePath.ToUpperInvariant(), timeoutMs, CancellationToken.None,
                                                      async() => {
                    //Now, if the item we seek is in the queue, we have a memcached hit. If not, we should check the index. It's possible the item has been written to disk already.
                    //If both are a miss, we should see if there is enough room in the write queue. If not, switch to in-thread writing.

                    AsyncWrite t = CurrentWrites.Get(relativePath);

                    if (t != null)
                    {
                        result.Data = t.GetReadonlyStream();
                    }



                    //On the second check, use cached data for speed. The cached data should be updated if another thread updated a file (but not if another process did).
                    //When t == null, and we're inside QueueLocks, all work on the file must be finished, so we have no need to consult mayBeLocked.
                    if (t == null && !Index.Exists(relativePath, physicalPath))
                    {
                        result.Result = CacheQueryResult.Miss;
                        //Still a miss, we even rechecked the file system. Write to memory.
                        MemoryStream ms = new MemoryStream(4096);      //4K initial capacity is minimal, but this array will get copied around a lot, better to underestimate.
                        //Read, resize, process, and encode the image. Lots of exceptions thrown here.
                        await writeCallback(ms);
                        ms.Position = 0;

                        AsyncWrite w = new AsyncWrite(ms, physicalPath, relativePath);
                        var queueResult = CurrentWrites.Queue(w, async delegate(AsyncWrite job)
                        {
                            try
                            {
                                var swIo = Stopwatch.StartNew();
                                //We want this to run synchronously, since it's in a background thread already.
                                if (!(await TryWriteFile(null, job.PhysicalPath, job.Key,
                                                         delegate(Stream s)
                                {
                                    var fromStream = job.GetReadonlyStream();
                                    return(fromStream.CopyToAsync(s));
                                }, timeoutMs, true)))
                                {
                                    swIo.Stop();
                                    //We failed to lock the file.
                                    logger?.LogWarning(
                                        "Failed to flush async write, timeout exceeded after {0}ms - {1}",
                                        swIo.ElapsedMilliseconds, result.RelativePath);
                                }
                                else
                                {
                                    swIo.Stop();
                                    logger?.LogTrace("{0}ms: Async write started {1}ms after enqueue for {2}",
                                                     swIo.ElapsedMilliseconds.ToString().PadLeft(4),
                                                     DateTime.UtcNow.Subtract(w.JobCreatedAt).Subtract(swIo.Elapsed)
                                                     .TotalMilliseconds, result.RelativePath);
                                }
                            }
                            catch (Exception ex)
                            {
                                logger?.LogError("Failed to flush async write, {0} {1}\n{2}", ex.ToString(),
                                                 result.RelativePath, ex.StackTrace);
                            }
                            finally
                            {
                                CurrentWrites.Remove(job);     //Remove from the queue, it's done or failed.
                            }
                        });
                        if (queueResult == AsyncWriteCollection.AsyncQueueResult.Enqueued ||
                            queueResult == AsyncWriteCollection.AsyncQueueResult.AlreadyPresent)
                        {
                            //We queued it! Send back a read-only memory stream
                            result.Data = w.GetReadonlyStream();
                        }
                        else
                        {
                            asyncFailed = false;
                            //We failed to queue it - either the ThreadPool was exhausted or we exceeded the MB limit for the write queue.
                            //Write the MemoryStream to disk using the normal method.
                            //This is nested inside a queueLock because if we failed here, the next one will also. Better to force it to wait until the file is written to disk.
                            if (!await TryWriteFile(result, physicalPath, relativePath, async delegate(Stream s) { await ms.CopyToAsync(s); }, timeoutMs, false))
                            {
                                logger?.LogWarning("Failed to queue async write, also failed to lock for sync writing: {0}", result.RelativePath);
                            }
                        }
                    }
                }))
                {
                    //On failure
                    result.Result = CacheQueryResult.Failed;
                }
            }
            if (logger != null)
            {
                sw?.Stop();
                logger?.LogTrace("{0}ms: {1}{2} for {3}, Key: {4}",
                                 sw?.ElapsedMilliseconds.ToString(NumberFormatInfo.InvariantInfo).PadLeft(4),
                                 asynchronous ? (asyncFailed ? "AsyncHttpMode, fell back to sync write  " : "AsyncHttpMode+AsyncWrites ") : "AsyncHttpMode",
                                 result.Result.ToString(),
                                 result.RelativePath,
                                 keyBasis);
            }
            //Fire event
            CacheResultReturned?.Invoke(this, result);
            return(result);
        }