diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-11-27 14:36:56 -0500 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-11-27 14:36:56 -0500 |
| commit | 26ef23d628c6f84baca5491203e1fe2a9a82d6b9 (patch) | |
| tree | d8fa32cccb34ef1c6faf824a60bb3fe6d74e05c0 /Emby.Server.Implementations | |
| parent | b485c4ca51c26dab56dbc45275bdff4ff165293c (diff) | |
update caching headers
Diffstat (limited to 'Emby.Server.Implementations')
6 files changed, 87 insertions, 15 deletions
diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 5c60a6f86..308b8356f 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -88,11 +88,14 @@ namespace Emby.Server.Implementations.Data var queries = new List<string> { - "PRAGMA temp_store = memory", - //"PRAGMA journal_mode=WAL" //"PRAGMA cache size=-10000" }; + if (EnableTempStoreMemory) + { + queries.Add("PRAGMA temp_store = memory"); + } + //var cacheSize = CacheSize; //if (cacheSize.HasValue) //{ @@ -116,7 +119,7 @@ namespace Emby.Server.Implementations.Data db.ExecuteAll(string.Join(";", queries.ToArray())); } } - else + else if (queries.Count > 0) { db.ExecuteAll(string.Join(";", queries.ToArray())); } @@ -124,6 +127,14 @@ namespace Emby.Server.Implementations.Data return db; } + protected virtual bool EnableTempStoreMemory + { + get + { + return false; + } + } + protected virtual int? CacheSize { get diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 17afbcfa9..1bd64b21d 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -63,8 +63,8 @@ namespace Emby.Server.Implementations.Data string[] queries = { - "create table if not exists userdisplaypreferences (id GUID, userId GUID, client text, data BLOB)", - "create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)" + "create table if not exists userdisplaypreferences (id GUID, userId GUID, client text, data BLOB)", + "create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)" }; connection.RunQueries(queries); @@ -107,10 +107,10 @@ namespace Emby.Server.Implementations.Data private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection) { - using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userid, @client, @data)")) - { - var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider); + var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider); + using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userId, @client, @data)")) + { statement.TryBind("@id", displayPreferences.Id.ToGuidParamValue()); statement.TryBind("@userId", userId.ToGuidParamValue()); statement.TryBind("@client", client); diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index c6e5a6dcf..29aacc059 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -30,6 +30,7 @@ using MediaBrowser.Server.Implementations.Playlists; using MediaBrowser.Model.Reflection; using SQLitePCL.pretty; using MediaBrowser.Model.System; +using MediaBrowser.Model.Threading; namespace Emby.Server.Implementations.Data { @@ -68,11 +69,13 @@ namespace Emby.Server.Implementations.Data private readonly IMemoryStreamFactory _memoryStreamProvider; private readonly IFileSystem _fileSystem; private readonly IEnvironmentInfo _environmentInfo; + private readonly ITimerFactory _timerFactory; + private ITimer _shrinkMemoryTimer; /// <summary> /// Initializes a new instance of the <see cref="SqliteItemRepository"/> class. /// </summary> - public SqliteItemRepository(IServerConfigurationManager config, IJsonSerializer jsonSerializer, ILogger logger, IMemoryStreamFactory memoryStreamProvider, IAssemblyInfo assemblyInfo, IFileSystem fileSystem, IEnvironmentInfo environmentInfo) + public SqliteItemRepository(IServerConfigurationManager config, IJsonSerializer jsonSerializer, ILogger logger, IMemoryStreamFactory memoryStreamProvider, IAssemblyInfo assemblyInfo, IFileSystem fileSystem, IEnvironmentInfo environmentInfo, ITimerFactory timerFactory) : base(logger) { if (config == null) @@ -89,6 +92,7 @@ namespace Emby.Server.Implementations.Data _memoryStreamProvider = memoryStreamProvider; _fileSystem = fileSystem; _environmentInfo = environmentInfo; + _timerFactory = timerFactory; _typeMapper = new TypeMapper(assemblyInfo); _criticReviewsPath = Path.Combine(_config.ApplicationPaths.DataPath, "critic-reviews"); @@ -119,6 +123,14 @@ namespace Emby.Server.Implementations.Data } } + protected override bool EnableTempStoreMemory + { + get + { + return true; + } + } + private SQLiteDatabaseConnection _backgroundConnection; protected override void CloseConnection() { @@ -129,6 +141,12 @@ namespace Emby.Server.Implementations.Data _backgroundConnection.Dispose(); _backgroundConnection = null; } + + if (_shrinkMemoryTimer != null) + { + _shrinkMemoryTimer.Dispose(); + _shrinkMemoryTimer = null; + } } /// <summary> @@ -364,13 +382,35 @@ namespace Emby.Server.Implementations.Data connection.RunQueries(postQueries); - //SqliteExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb"); //await Vacuum(_connection).ConfigureAwait(false); } userDataRepo.Initialize(WriteLock); _backgroundConnection = CreateConnection(true); + + _shrinkMemoryTimer = _timerFactory.Create(OnShrinkMemoryTimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(30)); + } + + private void OnShrinkMemoryTimerCallback(object state) + { + try + { + using (WriteLock.Write()) + { + using (var connection = CreateConnection()) + { + connection.RunQueries(new string[] + { + "pragma shrink_memory" + }); + } + } + } + catch (Exception ex) + { + Logger.ErrorException("Error running shrink memory", ex); + } } private readonly string[] _retriveItemColumns = @@ -666,7 +706,7 @@ namespace Emby.Server.Implementations.Data { var requiresReset = false; - var statements = db.PrepareAll(string.Join(";", new string[] + var statements = db.PrepareAll(string.Join(";", new string[] { GetSaveItemCommandText(), "delete from AncestorIds where ItemId=@ItemId", diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index 4c1b8fcd9..b01f215e0 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -84,6 +84,14 @@ namespace Emby.Server.Implementations.Data } } + protected override bool EnableTempStoreMemory + { + get + { + return true; + } + } + private void ImportUserDataIfNeeded(IDatabaseConnection connection) { if (!_fileSystem.FileExists(_importFile)) diff --git a/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs b/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs index 313db6a75..995dc7b7b 100644 --- a/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs +++ b/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs @@ -735,7 +735,7 @@ namespace Emby.Server.Implementations.HttpServer /// <returns><c>true</c> if [is not modified] [the specified cache key]; otherwise, <c>false</c>.</returns> private bool IsNotModified(IRequest requestContext, Guid? cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration) { - var isNotModified = true; + //var isNotModified = true; var ifModifiedSinceHeader = requestContext.Headers.Get("If-Modified-Since"); @@ -745,18 +745,23 @@ namespace Emby.Server.Implementations.HttpServer if (DateTime.TryParse(ifModifiedSinceHeader, out ifModifiedSince)) { - isNotModified = IsNotModified(ifModifiedSince.ToUniversalTime(), cacheDuration, lastDateModified); + if (IsNotModified(ifModifiedSince.ToUniversalTime(), cacheDuration, lastDateModified)) + { + return true; + } } } var ifNoneMatchHeader = requestContext.Headers.Get("If-None-Match"); // Validate If-None-Match - if (isNotModified && (cacheKey.HasValue || !string.IsNullOrEmpty(ifNoneMatchHeader))) + if ((cacheKey.HasValue || !string.IsNullOrEmpty(ifNoneMatchHeader))) { Guid ifNoneMatch; - if (Guid.TryParse(ifNoneMatchHeader ?? string.Empty, out ifNoneMatch)) + ifNoneMatchHeader = (ifNoneMatchHeader ?? string.Empty).Trim('\"'); + + if (Guid.TryParse(ifNoneMatchHeader, out ifNoneMatch)) { if (cacheKey.HasValue && cacheKey.Value == ifNoneMatch) { diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index 8cce7a8bf..b2d9fbcc9 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -83,6 +83,14 @@ namespace Emby.Server.Implementations.Sync } } + protected override bool EnableTempStoreMemory + { + get + { + return true; + } + } + private const string BaseJobSelectText = "select Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount from SyncJobs"; private const string BaseJobItemSelectText = "select Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex, ItemDateModifiedTicks from SyncJobItems"; |
