Skip to content

Commit

Permalink
Merge pull request #24 from phillipfisher/main
Browse files Browse the repository at this point in the history
Move to main
  • Loading branch information
phillipfisher authored Jul 30, 2024
2 parents d06a014 + 51d4988 commit 5c64535
Show file tree
Hide file tree
Showing 27 changed files with 1,834 additions and 2 deletions.
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 src/ON.Content/SimpleCMS/Service/Data/IPageDataProvider.cs
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);
}
}
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);
}
}
}
Loading

0 comments on commit 5c64535

Please sign in to comment.