From acfc7d5b70646003d662779996be2ff3333331ed Mon Sep 17 00:00:00 2001 From: Jan Ouborny Date: Sat, 5 Aug 2017 13:53:38 +0200 Subject: [PATCH] Add interface and first file system implementation --- RiotSharp/Persistence/FileSystemStore.cs | 76 ++++++++++++++++++++++++ RiotSharp/Persistence/IResponseStore.cs | 14 +++++ RiotSharp/Persistence/ResponseWrapper.cs | 12 ++++ 3 files changed, 102 insertions(+) create mode 100644 RiotSharp/Persistence/FileSystemStore.cs create mode 100644 RiotSharp/Persistence/IResponseStore.cs create mode 100644 RiotSharp/Persistence/ResponseWrapper.cs diff --git a/RiotSharp/Persistence/FileSystemStore.cs b/RiotSharp/Persistence/FileSystemStore.cs new file mode 100644 index 00000000..81754eed --- /dev/null +++ b/RiotSharp/Persistence/FileSystemStore.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace RiotSharp.Persistence +{ + public class FileSystemStore : IResponseStore + { + private string directoryPath; + private TimeSpan RetentionTime = new TimeSpan(7, 0, 0, 0); + private TimeSpan ExpirationTime = new TimeSpan(1, 0, 0, 0); + + public FileSystemStore(string directoryPath) + { + if (!Directory.Exists(directoryPath)) + throw new ArgumentException("Destination directory does not exist."); + this.directoryPath = directoryPath; + } + + public Response Get(string key) + { + try + { + var fileEntries = Directory.GetFiles(directoryPath); + foreach(var fileName in fileEntries.Where(x => x.Contains(key))) + { + var fileNameParts = fileName.Split('_'); + if (fileNameParts.First() != key) + continue; + var creationTime = new DateTime(Convert.ToInt64(fileNameParts.Last())); + var timeDifference = DateTime.Now.Subtract(creationTime); + if (timeDifference > ExpirationTime) + { + if (timeDifference > RetentionTime) + File.Delete(Path.Combine(directoryPath, fileName)); + + return null; + } + else + { + using (var fileStream = new FileStream(Path.Combine(directoryPath, fileName), FileMode.Open)) + { + //return fileStream.Re + return null; + } + } + } + return null; + } + catch + { + return null; + } + } + + public void Save(string key, string response) + { + var fileName = key + "_" + DateTime.Now.ToBinary(); + using (var fileStream = new FileStream(Path.Combine(directoryPath + fileName), FileMode.Create)) + { + byte[] info = new UTF8Encoding(true).GetBytes(response); + fileStream.Write(info, 0, info.Length); + } + } + + /// + /// Delete all entries past the retention time + /// + private void CleanUp() + { + + } + } +} diff --git a/RiotSharp/Persistence/IResponseStore.cs b/RiotSharp/Persistence/IResponseStore.cs new file mode 100644 index 00000000..bb3385b8 --- /dev/null +++ b/RiotSharp/Persistence/IResponseStore.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RiotSharp.Persistence +{ + public interface IResponseStore + { + + void Save(string key, string response); + + Response Get(string key); + } +} diff --git a/RiotSharp/Persistence/ResponseWrapper.cs b/RiotSharp/Persistence/ResponseWrapper.cs new file mode 100644 index 00000000..7021f662 --- /dev/null +++ b/RiotSharp/Persistence/ResponseWrapper.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RiotSharp.Persistence +{ + public class Response + { + public int ExpirationTime { get; set; } + public string Data { get; set; } + } +}