-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from phillipfisher/main
Move to main
- Loading branch information
Showing
27 changed files
with
1,834 additions
and
2 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/ON.Content/SimpleCMS/Service/Data/FileSystemPageDataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Google.Protobuf; | ||
using Microsoft.Extensions.Options; | ||
using ON.Content.SimpleCMS.Service.Models; | ||
using ON.Fragments.Generic; | ||
using ON.Fragments.Page; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ON.Content.SimpleCMS.Service.Data | ||
{ | ||
public class FileSystemPageDataProvider : IPageDataProvider | ||
{ | ||
private readonly DirectoryInfo pageDir; | ||
|
||
public FileSystemPageDataProvider(IOptions<AppSettings> settings) | ||
{ | ||
var root = new DirectoryInfo(settings.Value.DataStore); | ||
root.Create(); | ||
pageDir = root.CreateSubdirectory("page"); | ||
} | ||
|
||
public Task<bool> Delete(Guid userId) | ||
{ | ||
var fd = GetPageFilePath(userId); | ||
var res = fd.Exists; | ||
fd.Delete(); | ||
return Task.FromResult(res); | ||
} | ||
|
||
public Task<bool> Exists(Guid userId) | ||
{ | ||
var fd = GetPageFilePath(userId); | ||
return Task.FromResult(fd.Exists); | ||
} | ||
|
||
public async IAsyncEnumerable<PageRecord> GetAll() | ||
{ | ||
foreach (var file in pageDir.GetFiles()) | ||
{ | ||
yield return PageRecord.Parser.ParseFrom(await File.ReadAllBytesAsync(file.FullName)); | ||
} | ||
} | ||
|
||
public async Task<PageRecord> GetById(Guid pageId) | ||
{ | ||
var fd = GetPageFilePath(pageId); | ||
if (!fd.Exists) | ||
return null; | ||
|
||
return PageRecord.Parser.ParseFrom(await File.ReadAllBytesAsync(fd.FullName)); | ||
} | ||
|
||
public async Task<PageRecord> GetByURL(string url) | ||
{ | ||
await foreach(var rec in GetAll()) | ||
{ | ||
if (rec.Public.Data.URL == url) | ||
return rec; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public async Task Save(PageRecord page) | ||
{ | ||
var id = page.Public.PageID.ToGuid(); | ||
var fd = GetPageFilePath(id); | ||
await File.WriteAllBytesAsync(fd.FullName, page.ToByteArray()); | ||
} | ||
|
||
private FileInfo GetPageFilePath(Guid pageId) | ||
{ | ||
var name = pageId.ToString(); | ||
return new FileInfo(pageDir.FullName + "/" + name); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/ON.Content/SimpleCMS/Service/Data/IPageDataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using ON.Fragments.Content; | ||
using ON.Fragments.Page; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ON.Content.SimpleCMS.Service.Data | ||
{ | ||
public interface IPageDataProvider | ||
{ | ||
IAsyncEnumerable<PageRecord> GetAll(); | ||
Task<PageRecord> GetById(Guid PageId); | ||
Task<PageRecord> GetByURL(string url); | ||
Task<bool> Delete(Guid pageId); | ||
Task<bool> Exists(Guid pageId); | ||
Task Save(PageRecord page); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/ON.Content/SimpleCMS/Service/Data/MemCachedFileSystemPageDataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using ON.Fragments.Page; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Threading.Tasks; | ||
using System.Linq; | ||
|
||
namespace ON.Content.SimpleCMS.Service.Data | ||
{ | ||
public class MemCachedFileSystemPageDataProvider : IPageDataProvider | ||
{ | ||
private readonly ConcurrentDictionary<Guid, PageRecord> cache = new(); | ||
private readonly FileSystemPageDataProvider dataProvider; | ||
|
||
public MemCachedFileSystemPageDataProvider(FileSystemPageDataProvider dataProvider) | ||
{ | ||
this.dataProvider = dataProvider; | ||
LoadCache().Wait(); | ||
} | ||
|
||
private async Task LoadCache() | ||
{ | ||
await foreach(var r in dataProvider.GetAll()) | ||
{ | ||
cache.TryAdd(r.Public.PageIDGuid, r); | ||
} | ||
} | ||
|
||
public Task<bool> Delete(Guid pageId) | ||
{ | ||
if (cache.TryRemove(pageId, out var r)) | ||
return dataProvider.Delete(pageId); | ||
|
||
return Task.FromResult(false); | ||
} | ||
|
||
public Task<bool> Exists(Guid pageId) | ||
{ | ||
return Task.FromResult(cache.ContainsKey(pageId)); | ||
} | ||
|
||
public IAsyncEnumerable<PageRecord> GetAll() | ||
{ | ||
return cache.Values.Select(v => v?.Clone()).ToAsyncEnumerable(); | ||
} | ||
|
||
public Task<PageRecord> GetById(Guid pageId) | ||
{ | ||
if (cache.TryGetValue(pageId, out var record)) | ||
return Task.FromResult(record?.Clone()); | ||
|
||
return Task.FromResult((PageRecord)null); | ||
} | ||
|
||
public Task<PageRecord> GetByURL(string url) | ||
{ | ||
return Task.FromResult(cache.Values.FirstOrDefault(r => r.Public.Data.URL == url)?.Clone()); | ||
} | ||
|
||
public async Task Save(PageRecord page) | ||
{ | ||
await dataProvider.Save(page); | ||
|
||
page = await dataProvider.GetById(page.Public.PageIDGuid); | ||
|
||
if (page == null) | ||
return; | ||
|
||
cache.AddOrUpdate(page.Public.PageIDGuid, page, (k,v) => page); | ||
} | ||
} | ||
} |
Oops, something went wrong.