Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions RiotSharp/Persistence/FileSystemStore.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

/// <summary>
/// Delete all entries past the retention time
/// </summary>
private void CleanUp()
{

}
}
}
14 changes: 14 additions & 0 deletions RiotSharp/Persistence/IResponseStore.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions RiotSharp/Persistence/ResponseWrapper.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}