diff options
Diffstat (limited to 'back-end/src')
-rw-r--r-- | back-end/src/Cache/MemPackCacheSerializer.cs | 34 | ||||
-rw-r--r-- | back-end/src/Cache/ResultCacheEntry.cs | 44 | ||||
-rw-r--r-- | back-end/src/Cache/SearchResultCache.cs | 111 | ||||
-rw-r--r-- | back-end/src/Endpoints/BookmarkEndpoint.cs | 31 | ||||
-rw-r--r-- | back-end/src/SimpleBookmark.csproj | 11 |
5 files changed, 26 insertions, 205 deletions
diff --git a/back-end/src/Cache/MemPackCacheSerializer.cs b/back-end/src/Cache/MemPackCacheSerializer.cs deleted file mode 100644 index f1ffa88..0000000 --- a/back-end/src/Cache/MemPackCacheSerializer.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (C) 2024 Vaughn Nugent -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see <https://www.gnu.org/licenses/>. - - -using System; -using System.Buffers; - -using MemoryPack; - -using VNLib.Data.Caching; - -namespace SimpleBookmark.Cache -{ - internal sealed class MemPackCacheSerializer(MemoryPackSerializerOptions? options) : ICacheObjectSerializer, ICacheObjectDeserializer - { - ///<inheritdoc/> - public T? Deserialize<T>(ReadOnlySpan<byte> objectData) => MemoryPackSerializer.Deserialize<T>(objectData, options); - - ///<inheritdoc/> - public void Serialize<T>(T obj, IBufferWriter<byte> finiteWriter) => MemoryPackSerializer.Serialize(finiteWriter, obj, options); - } -} diff --git a/back-end/src/Cache/ResultCacheEntry.cs b/back-end/src/Cache/ResultCacheEntry.cs deleted file mode 100644 index 3e23042..0000000 --- a/back-end/src/Cache/ResultCacheEntry.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (C) 2024 Vaughn Nugent -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see <https://www.gnu.org/licenses/>. - -using System; -using System.Buffers; -using System.Runtime.InteropServices; -using System.Text.Json.Serialization; - -using MemoryPack; - -namespace SimpleBookmark.Cache -{ - [MemoryPackable] - internal partial class ResultCacheEntry : IDisposable - { - [MemoryPoolFormatter<byte>] - public Memory<byte> Payload { get; set; } - - [JsonPropertyName("created")] - public DateTime Created { get; set; } - - public void Dispose() - { - //Return the array back to the pool - if (MemoryMarshal.TryGetArray(Payload, out ArraySegment<byte> segment) && segment.Array is { Length: > 0 }) - { - ArrayPool<byte>.Shared.Return(segment.Array); - Payload = default; - } - } - } -} diff --git a/back-end/src/Cache/SearchResultCache.cs b/back-end/src/Cache/SearchResultCache.cs deleted file mode 100644 index c7a263a..0000000 --- a/back-end/src/Cache/SearchResultCache.cs +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (C) 2024 Vaughn Nugent -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see <https://www.gnu.org/licenses/>. - -using System; -using System.Threading; -using System.Threading.Tasks; - -using VNLib.Net.Http; -using VNLib.Data.Caching; -using VNLib.Plugins; -using VNLib.Plugins.Extensions.Loading; -using VNLib.Plugins.Extensions.VNCache; -using VNLib.Plugins.Extensions.VNCache.DataModel; - -namespace SimpleBookmark.Cache -{ - [ConfigurationName("search_cache", Required = false)] - internal sealed class SearchResultCache - { - private readonly IEntityCache<ResultCacheEntry>? _cache; - - /// <summary> - /// Gets a value that indicates if the configuration enabled result caching - /// </summary> - public bool IsEnabled => _cache != null; - - public SearchResultCache(PluginBase plugin) : this(plugin, null) - { } - - public SearchResultCache(PluginBase plugin, IConfigScope? config) - { - string? cachePrefix = config?.GetRequiredProperty("cachePrefix", p => p.GetString()!); - bool isEnabled = config?.GetRequiredProperty("enabled", p => p.GetBoolean()) ?? true; - - if (!isEnabled) - { - return; - } - - IGlobalCacheProvider? cache = plugin.GetDefaultGlobalCache(); - if (cache != null) - { - if (cachePrefix != null) - { - _cache = cache.GetPrefixedCache(cachePrefix) - .CreateEntityCache<ResultCacheEntry>( - new MemPackCacheSerializer(null), - new MemPackCacheSerializer(null) - ); - } - else - { - //non-prefixed cache - _cache = cache.CreateEntityCache<ResultCacheEntry>( - new MemPackCacheSerializer(null), - new MemPackCacheSerializer(null) - ); - } - } - } - - public async Task<IMemoryResponseReader?> GetCachedResultAsync(string[] keys, CancellationToken cancellation) - { - ResultCacheEntry? entry = await _cache!.GetAsync($"{keys}", cancellation); - return entry is null ? null : new ResultResponseReader(entry); - } - - public Task StoreResultAsync(Memory<byte> data, string[] keys, CancellationToken cancellation) - { - //Init new entry - ResultCacheEntry entry = new() - { - Payload = data, - Created = DateTime.UtcNow - }; - - return _cache!.UpsertAsync($"{keys}", entry, cancellation); - } - - public Task DeleteEntry(string[] keys, CancellationToken cancellation) => _cache!.RemoveAsync($"{keys}", cancellation); - - private sealed class ResultResponseReader(ResultCacheEntry entry) : IMemoryResponseReader - { - private int _position; - - ///<inheritdoc/> - public int Remaining => entry.Payload.Length - _position; - - ///<inheritdoc/> - public void Advance(int written) => _position += written; - - ///<inheritdoc/> - public void Close() => entry.Dispose(); - - ///<inheritdoc/> - public ReadOnlyMemory<byte> GetMemory() => entry.Payload.Slice(_position); - } - } -} diff --git a/back-end/src/Endpoints/BookmarkEndpoint.cs b/back-end/src/Endpoints/BookmarkEndpoint.cs index 19f5118..ae2932d 100644 --- a/back-end/src/Endpoints/BookmarkEndpoint.cs +++ b/back-end/src/Endpoints/BookmarkEndpoint.cs @@ -131,7 +131,13 @@ namespace SimpleBookmark.Endpoints { //Get the collection of bookmarks List<BookmarkEntry> list = Bookmarks.ListRental.Rent(); - await Bookmarks.GetUserPageAsync(list, entity.Session.UserID, 0, (int)BmConfig.PerPersonQuota); + + await Bookmarks.GetUserPageAsync( + collection: list, + userId: entity.Session.UserID, + page: 0, + limit: (int)BmConfig.PerPersonQuota + ); //Alloc memory stream for output VnMemoryStream output = new(MemoryUtil.Shared, 16 * 1024, false); @@ -205,13 +211,13 @@ namespace SimpleBookmark.Endpoints //Get bookmarks BookmarkEntry[] bookmarks = await Bookmarks.SearchBookmarksAsync( - entity.Session.UserID, + userId : entity.Session.UserID, query, tags, (int)limit, (int)offset, - entity.EventCancellation - ); + cancellation: entity.EventCancellation + ); //Return result return VirtualOkJson(entity, bookmarks); @@ -248,11 +254,11 @@ namespace SimpleBookmark.Endpoints * and the entry does not already exist by the desired url. */ int result = await Bookmarks.AddSingleIfNotExists( - entity.Session.UserID, - newBookmark, - entity.RequestedTimeUtc.DateTime, - BmConfig.PerPersonQuota, - entity.EventCancellation + userId: entity.Session.UserID, + entry: newBookmark, + now: entity.RequestedTimeUtc.DateTime, + maxRecords: BmConfig.PerPersonQuota, + cancellation: entity.EventCancellation ); if (webm.Assert(result > -1, "You have reached your bookmark quota")) @@ -386,7 +392,12 @@ namespace SimpleBookmark.Endpoints try { //Try to update the records - ERRNO result = await Bookmarks.AddBulkAsync(sanitized, entity.Session.UserID, entity.RequestedTimeUtc, entity.EventCancellation); + ERRNO result = await Bookmarks.AddBulkAsync( + bookmarks: sanitized, + userId: entity.Session.UserID, + now: entity.RequestedTimeUtc, + cancellation: entity.EventCancellation + ); webm.Result = $"Successfully added {result} of {batch.Length} bookmarks"; webm.Success = true; diff --git a/back-end/src/SimpleBookmark.csproj b/back-end/src/SimpleBookmark.csproj index fc9b46a..382d141 100644 --- a/back-end/src/SimpleBookmark.csproj +++ b/back-end/src/SimpleBookmark.csproj @@ -33,12 +33,11 @@ </ItemGroup> <ItemGroup> - <PackageReference Include="MemoryPack" Version="1.21.1" /> - <PackageReference Include="VNLib.Plugins.Extensions.Data" Version="0.1.0-ci0063" /> - <PackageReference Include="VNLib.Plugins.Extensions.Loading" Version="0.1.0-ci0063" /> - <PackageReference Include="VNLib.Plugins.Extensions.Loading.Sql" Version="0.1.0-ci0063" /> - <PackageReference Include="VNLib.Plugins.Extensions.Validation" Version="0.1.0-ci0063" /> - <PackageReference Include="VNLib.Plugins.Extensions.VNCache" Version="0.1.0-ci0056" /> + <PackageReference Include="VNLib.Plugins.Extensions.Data" Version="0.1.0-ci0064" /> + <PackageReference Include="VNLib.Plugins.Extensions.Loading" Version="0.1.0-ci0064" /> + <PackageReference Include="VNLib.Plugins.Extensions.Loading.Sql" Version="0.1.0-ci0064" /> + <PackageReference Include="VNLib.Plugins.Extensions.Validation" Version="0.1.0-ci0064" /> + <PackageReference Include="VNLib.Plugins.Extensions.VNCache" Version="0.1.0-ci0057" /> </ItemGroup> <ItemGroup> |