Как объединить частично предварительно кэшированный MemoryStream с FileStream?

2

Для приложения представления мультимедиа, критичного ко времени, важно, чтобы файлы мультимедиа были представлены прямо в тот момент, когда пользователь их выбирает. Эти файлы находятся в действительно огромной структуре каталогов, состоящей из тысяч медиа файлов.

Очевидно, что кэширование медиа файлов в MemoryStream - это путь; однако из-за огромного количества файлов невозможно полностью кэшировать каждый файл. Вместо этого моя идея состоит в том, чтобы предварительно кэшировать определенный буфер каждого файла, и, как только файл будет представлен, воспроизводить из этого кэша до тех пор, пока остальная часть файла не будет загружена с жесткого диска.

Чего я не вижу, так это как "объединить" MemoryStream и FileStream чтобы обеспечить беспроблемное воспроизведение. Я не очень силен в потоках данных (пока), и я вижу несколько проблем:

  • Как можно отслеживать текущую позицию чтения в MemoryStream и MemoryStream ее в FileStream без чтения MemoryStream?
  • Как один переключается с одного потока на другой, если оба потока не частично перекрывают друг друга или не создают "разрыв воспроизведения"?
  • Если используется очередь потоков (как предложено в разделе Как объединить два экземпляра System.Io.Stream в один?), Как я могу указать, что второй поток должен быть готов к немедленному доступу для чтения после завершения первого потока? Здесь, в частности, я не вижу, как MemoryStream мог бы помочь вообще, так как FileStream, как второй в очереди, начал бы доступ к жесткому диску только после его фактического использования.
  • Действительно ли реально иметь буквально сотни, если не тысячи открытых потоков одновременно?

Обратите внимание, что мне не нужен доступ для записи - чтение вполне достаточно для решения проблемы. Кроме того, этот вопрос аналогичен Composite Stream Wrapper, предоставляющему частичную MemoryStream и полностью оригинальный Stream, но решение при условии, что есть исправление ошибки для Windows Phone 8, которое не применимо в моем случае.

Мне бы очень хотелось расширить мое довольно ограниченное понимание этого, поэтому любая помощь очень ценится.

Теги:
caching
filestream
memorystream

1 ответ

2
Лучший ответ

Я бы предложил что-то вроде следующего решения:

  • CachableFileStream свой собственный CachableFileStream от FileStream
  • Реализуйте очень простой Cache который использует структуру данных, которую вы предпочитаете (например, Queue)
  • Разрешить Preload загрузку данных во внутренний кеш
  • Разрешить Reload ИНГ данных во внутренний кэш
  • Измените исходное поведение Read так, чтобы ваш кеш использовался

Чтобы дать вам представление о моей идее, я бы предложил такую реализацию, как следующая:

Использование может быть таким:

CachableFileStream cachedStream = new CachableFileStream(...)
{
    PreloadSize = 8192,
    ReloadSize = 4096,
};

// Force preloading data into the cache
cachedStream.Preload();

...
cachedStream.Read(buffer, 0, buffer.Length);
...

Предупреждение: приведенный ниже код не является ни проверенным, ни идеальным - это просто даст вам представление!

Класс CachableFileStream:

using System;
using System.IO;
using System.Threading.Tasks;

/// <summary>
/// Represents a filestream with cache.
/// </summary>
public class CachableFileStream : FileStream
{
    private Cache<byte> cache;
    private int preloadSize;
    private int reloadSize;

    /// <summary>
    /// Gets or sets the amount of bytes to be preloaded.
    /// </summary>
    public int PreloadSize
    {
        get
        {
            return this.preloadSize;
        }

        set
        {
            if (value <= 0)
                throw new ArgumentOutOfRangeException(nameof(value), "The specified preload size must not be smaller than or equal to zero.");

            this.preloadSize = value;
        }
    }

    /// <summary>
    /// Gets or sets the amount of bytes to be reloaded.
    /// </summary>
    public int ReloadSize
    {
        get
        {
            return this.reloadSize;
        }

        set
        {
            if (value <= 0)
                throw new ArgumentOutOfRangeException(nameof(value), "The specified reload size must not be smaller than or equal to zero.");

            this.reloadSize = value;
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="CachableFileStream"/> class with the specified path and creation mode.
    /// </summary>
    /// <param name="path">A relative or absolute path for the file that the current CachableFileStream object will encapsulate</param>
    /// <param name="mode">A constant that determines how to open or create the file.</param>
    /// <exception cref="System.ArgumentException">
    /// Path is an empty string (""), contains only white space, or contains one or more invalid characters.
    /// -or- path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in an NTFS environment.
    /// </exception>
    /// <exception cref="System.NotSupportedException">
    /// Path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in a non-NTFS environment.
    /// </exception>
    /// <exception cref="System.ArgumentNullException">
    /// Path is null.
    /// </exception>
    /// <exception cref="System.Security.SecurityException">
    /// The caller does not have the required permission.
    /// </exception>
    /// <exception cref="System.IO.FileNotFoundException">
    /// The file cannot be found, such as when mode is FileMode.Truncate or FileMode.Open, and the file specified by path does not exist.
    /// The file must already exist in these modes.
    /// </exception>
    /// <exception cref="System.IO.IOException">
    /// An I/O error, such as specifying FileMode.CreateNew when the file specified by path already exists, occurred.-or-The stream has been closed.
    /// </exception>
    /// <exception cref="System.IO.DirectoryNotFoundException">
    /// The specified path is invalid, such as being on an unmapped drive.
    /// </exception>
    /// <exception cref="System.IO.PathTooLongException">
    /// The specified path, file name, or both exceed the system-defined maximum length.
    /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
    /// </exception>
    /// <exception cref="System.ArgumentOutOfRangeException">
    /// Mode contains an invalid value
    /// </exception>
    public CachableFileStream(string path, FileMode mode) : base(path, mode)
    {
        this.cache = new Cache<byte>();
        this.cache.CacheIsRunningLow += CacheIsRunningLow;
    }

    /// <summary>
    /// Reads a block of bytes from the stream and writes the data in a given buffer.
    /// </summary>
    /// <param name="array">
    /// When this method returns, contains the specified byte array with the values between
    /// offset and (offset + count - 1) replaced by the bytes read from the current source.
    /// </param>
    /// <param name="offset">The byte offset in array at which the read bytes will be placed.</param>
    /// <param name="count">The maximum number of bytes to read.</param>
    /// <returns>
    /// The total number of bytes read into the buffer. This might be less than the number
    /// of bytes requested if that number of bytes are not currently available, or zero
    /// if the end of the stream is reached.
    /// </returns>
    /// <exception cref="System.ArgumentNullException">
    /// Array is null.
    /// </exception>
    /// <exception cref="System.ArgumentOutOfRangeException">
    /// Offset or count is negative.
    /// </exception>
    /// <exception cref="System.NotSupportedException">
    /// The stream does not support reading.
    /// </exception>
    /// <exception cref="System.IO.IOException">
    /// An I/O error occurred.
    /// </exception>
    /// <exception cref="System.ArgumentException">
    /// Offset and count describe an invalid range in array.
    /// </exception>
    /// <exception cref="System.ObjectDisposedException">
    /// Methods were called after the stream was closed.
    /// </exception>
    public override int Read(byte[] array, int offset, int count)
    {
        int readBytesFromCache;

        for (readBytesFromCache = 0; readBytesFromCache < count; readBytesFromCache++)
        {
            if (this.cache.Size == 0)
                break;

            array[offset + readBytesFromCache] = this.cache.Read();
        }

        if (readBytesFromCache < count)
            readBytesFromCache += base.Read(array, offset + readBytesFromCache, count - readBytesFromCache);

        return readBytesFromCache;
    }

    /// <summary>
    /// Preload data into the cache.
    /// </summary>
    public void Preload()
    {
        this.LoadBytesFromStreamIntoCache(this.PreloadSize);
    }

    /// <summary>
    /// Reload data into the cache.
    /// </summary>
    public void Reload()
    {
        this.LoadBytesFromStreamIntoCache(this.ReloadSize);
    }

    /// <summary>
    /// Loads bytes from the stream into the cache.
    /// </summary>
    /// <param name="count">The number of bytes to read.</param>
    private void LoadBytesFromStreamIntoCache(int count)
    {
        byte[] buffer = new byte[count];
        int readBytes = base.Read(buffer, 0, buffer.Length);

        this.cache.AddRange(buffer, 0, readBytes);
    }

    /// <summary>
    /// Represents the event handler for the CacheIsRunningLow event.
    /// </summary>
    /// <param name="sender">The sender of the event.</param>
    /// <param name="e">Event arguments.</param>
    private void CacheIsRunningLow(object sender, EventArgs e)
    {
        this.cache.WarnIfRunningLow = false;

        new Task(() =>
        {
            Reload();
            this.cache.WarnIfRunningLow = true;
        }).Start();
    }
}

Класс Cache:

using System;
using System.Collections.Concurrent;

/// <summary>
/// Represents a generic cache.
/// </summary>
/// <typeparam name="T">Defines the type of the items in the cache.</typeparam>
public class Cache<T>
{
    private ConcurrentQueue<T> queue;

    /// <summary>
    /// Is executed when the number of items within the cache run below the
    /// specified warning limit and WarnIfRunningLow is set.
    /// </summary>
    public event EventHandler CacheIsRunningLow;

    /// <summary>
    /// Gets or sets a value indicating whether the CacheIsRunningLow event shall be fired or not.
    /// </summary>
    public bool WarnIfRunningLow
    {
        get;
        set;
    }

    /// <summary>
    /// Gets or sets a value that represents the lower warning limit.
    /// </summary>
    public int LowerWarningLimit
    {
        get;
        set;
    }

    /// <summary>
    /// Gets the number of items currently stored in the cache.
    /// </summary>
    public int Size
    {
        get;
        private set;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Cache{T}"/> class.
    /// </summary>
    public Cache()
    {
        this.queue = new ConcurrentQueue<T>();
        this.Size = 0;
        this.LowerWarningLimit = 1024;
        this.WarnIfRunningLow = true;
    }

    /// <summary>
    /// Adds an item into the cache.
    /// </summary>
    /// <param name="item">The item to be added to the cache.</param>
    public void Add(T item)
    {
        this.queue.Enqueue(item);
        this.Size++;
    }

    /// <summary>
    /// Adds the items of the specified array to the end of the cache.
    /// </summary>
    /// <param name="items">The items to be added.</param>
    public void AddRange(T[] items)
    {
        this.AddRange(items, 0, items.Length);
    }

    /// <summary>
    /// Adds the specified count of items of the specified array starting
    /// from offset to the end of the cache.
    /// </summary>
    /// <param name="items">The array that contains the items.</param>
    /// <param name="offset">The offset that shall be used.</param>
    /// <param name="count">The number of items that shall be added.</param>
    public void AddRange(T[] items, int offset, int count)
    {
        for (int i = offset; i < count; i++)
            this.Add(items[i]);
    }

    /// <summary>
    /// Reads one item from the cache.
    /// </summary>
    /// <returns>The item that has been read from the cache.</returns>
    /// <exception cref="System.InvalidOperationException">
    /// The cache is empty.
    /// </exception>
    public T Read()
    {
        T item;

        if (!this.queue.TryDequeue(out item))
            throw new InvalidOperationException("The cache is empty.");

        this.Size--;

        if (this.WarnIfRunningLow &&
            this.Size < this.LowerWarningLimit)
        {
            this.CacheIsRunningLow?.Invoke(this, EventArgs.Empty);
        }

        return item;
    }

    /// <summary>
    /// Peeks the next item from cache.
    /// </summary>
    /// <returns>The item that has been read from the cache (without deletion).</returns>
    /// <exception cref="System.InvalidOperationException">
    /// The cache is empty.
    /// </exception>
    public T Peek()
    {
        T item;

        if (!this.queue.TryPeek(out item))
            throw new InvalidOperationException("The cache is empty.");

        return item;
    }
}

Я надеюсь, что это помогает, повеселиться ;-)

  • 1
    Спасибо, это очень солидное решение. Мне особенно нравится одновременная перезагрузка, если кэш исчерпан. Тем не менее, существует проблема, связанная с контекстом медиа-файла: после того, как данный файл был представлен, его повторное воспроизведение будет медленным (требуется загрузка с диска), аналогично для остановки и перемотки назад или поиска и воспроизведения. Может быть, это можно решить с помощью Cache , который не очищается при чтении из него - я должен подумать об этом.
  • 0
    @Informagic: Если вы сочтете мое решение полезным, я буду рад, если вы примете его (временно) в качестве ответа :-)
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню