mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 19:37:55 +02:00
This is faster and uses way less memory ``` BenchmarkDotNet=v0.12.1, OS=fedora 32 Intel Core i7-6700HQ CPU 2.60GHz (Skylake), 1 CPU, 8 logical and 4 physical cores .NET Core SDK=5.0.100 [Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), X64 RyuJIT DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), X64 RyuJIT | Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated | |------- |---------:|--------:|--------:|--------:|------:|------:|----------:| | Bytes | 158.4 us | 2.56 us | 2.14 us | 16.8457 | - | - | 52.08 KB | | String | 172.8 us | 0.78 us | 0.70 us | 41.5039 | - | - | 127.82 KB | | Custom | 155.5 us | 2.95 us | 2.76 us | 10.0098 | - | - | 31.27 KB | ```
163 lines
4.3 KiB
C#
163 lines
4.3 KiB
C#
#pragma warning disable CS1591
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using MediaBrowser.Common.Json;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Emby.Server.Implementations.LiveTv.EmbyTV
|
|
{
|
|
public class ItemDataProvider<T>
|
|
where T : class
|
|
{
|
|
private readonly string _dataPath;
|
|
private readonly object _fileDataLock = new object();
|
|
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.GetOptions();
|
|
private T[] _items;
|
|
|
|
public ItemDataProvider(
|
|
ILogger logger,
|
|
string dataPath,
|
|
Func<T, T, bool> equalityComparer)
|
|
{
|
|
Logger = logger;
|
|
_dataPath = dataPath;
|
|
EqualityComparer = equalityComparer;
|
|
}
|
|
|
|
protected ILogger Logger { get; }
|
|
|
|
protected Func<T, T, bool> EqualityComparer { get; }
|
|
|
|
private void EnsureLoaded()
|
|
{
|
|
if (_items != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (File.Exists(_dataPath))
|
|
{
|
|
Logger.LogInformation("Loading live tv data from {Path}", _dataPath);
|
|
|
|
try
|
|
{
|
|
var bytes = File.ReadAllBytes(_dataPath);
|
|
_items = JsonSerializer.Deserialize<T[]>(bytes, _jsonOptions);
|
|
return;
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
Logger.LogError(ex, "Error deserializing {Path}", _dataPath);
|
|
}
|
|
}
|
|
|
|
_items = Array.Empty<T>();
|
|
}
|
|
|
|
private void SaveList()
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(_dataPath));
|
|
var jsonString = JsonSerializer.Serialize(_items, _jsonOptions);
|
|
File.WriteAllText(_dataPath, jsonString);
|
|
}
|
|
|
|
public IReadOnlyList<T> GetAll()
|
|
{
|
|
lock (_fileDataLock)
|
|
{
|
|
EnsureLoaded();
|
|
return (T[])_items.Clone();
|
|
}
|
|
}
|
|
|
|
public virtual void Update(T item)
|
|
{
|
|
if (item == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(item));
|
|
}
|
|
|
|
lock (_fileDataLock)
|
|
{
|
|
EnsureLoaded();
|
|
|
|
var index = Array.FindIndex(_items, i => EqualityComparer(i, item));
|
|
if (index == -1)
|
|
{
|
|
throw new ArgumentException("item not found");
|
|
}
|
|
|
|
_items[index] = item;
|
|
|
|
SaveList();
|
|
}
|
|
}
|
|
|
|
public virtual void Add(T item)
|
|
{
|
|
if (item == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(item));
|
|
}
|
|
|
|
lock (_fileDataLock)
|
|
{
|
|
EnsureLoaded();
|
|
|
|
if (_items.Any(i => EqualityComparer(i, item)))
|
|
{
|
|
throw new ArgumentException("item already exists", nameof(item));
|
|
}
|
|
|
|
int oldLen = _items.Length;
|
|
var newList = new T[oldLen + 1];
|
|
_items.CopyTo(newList, 0);
|
|
newList[oldLen] = item;
|
|
_items = newList;
|
|
|
|
SaveList();
|
|
}
|
|
}
|
|
|
|
public virtual void AddOrUpdate(T item)
|
|
{
|
|
lock (_fileDataLock)
|
|
{
|
|
EnsureLoaded();
|
|
|
|
int index = Array.FindIndex(_items, i => EqualityComparer(i, item));
|
|
if (index == -1)
|
|
{
|
|
int oldLen = _items.Length;
|
|
var newList = new T[oldLen + 1];
|
|
_items.CopyTo(newList, 0);
|
|
newList[oldLen] = item;
|
|
_items = newList;
|
|
}
|
|
else
|
|
{
|
|
_items[index] = item;
|
|
}
|
|
|
|
SaveList();
|
|
}
|
|
}
|
|
|
|
public virtual void Delete(T item)
|
|
{
|
|
lock (_fileDataLock)
|
|
{
|
|
EnsureLoaded();
|
|
_items = _items.Where(i => !EqualityComparer(i, item)).ToArray();
|
|
|
|
SaveList();
|
|
}
|
|
}
|
|
}
|
|
}
|