Skip to content

Commit

Permalink
CLUNL.CacheManager.0.0.1.0
Browse files Browse the repository at this point in the history
Initial Release.
CLUNL.0.0.6.0
Added a logger.
  • Loading branch information
creeperlv committed Feb 15, 2022
1 parent d2988f2 commit e9ec60c
Show file tree
Hide file tree
Showing 9 changed files with 924 additions and 43 deletions.
28 changes: 28 additions & 0 deletions CLUNL.CacheManager/CLUNL.CacheManager.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.0.1.0</Version>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Copyright>Copyright (C) 2022 Creeper Lv</Copyright>
<PackageLicenseExpression></PackageLicenseExpression>
<PackageProjectUrl>https://github.com/creeperlv/CLUNL</PackageProjectUrl>
<RepositoryUrl>https://github.com/creeperlv/CLUNL</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Authors>Creeper Lv</Authors>
<Company>Creeper Lv</Company>
<Description>CacheManager of Creeper Lv's Universal dotNet Library</Description>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Creeper Lv's Universal dotNet Library\CLUNL.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
109 changes: 109 additions & 0 deletions CLUNL.CacheManager/Cache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using CLUNL.Diagnostics;
using System;
using System.IO;
using System.Threading.Tasks;

namespace CLUNL.CacheManager
{
/// <summary>
/// A cache entry.
/// </summary>
public class Cache
{
/// <summary>
/// The url of the request.
/// </summary>
public string RequestUrl;
/// <summary>
/// The path were the item is stored.
/// </summary>
public string StorePath;
/// <summary>
/// The time of when the cache is updated.
/// </summary>
public DateTime RequestDate;
/// <summary>
/// Requeset count.
/// </summary>
public int RequestCount;
/// <summary>
/// Obtain the file.
/// </summary>
/// <returns></returns>
public FileInfo ObtainFile()
{
RequestCount++;
return new FileInfo(StorePath);
}
/// <summary>
/// Update the file.
/// </summary>
/// <returns></returns>
public async Task<FileInfo> UpdateFile()
{
Logger.WriteLine("Updating cache...");
var f = new FileInfo(StorePath);
if (!f.Directory.Exists) f.Directory.Create();
try
{
using (var t = await Cacher.client.GetAsync(RequestUrl))
{
if (File.Exists(StorePath))
File.Delete(StorePath);
using (var __f = File.Create(StorePath))
{
await t.Content.CopyToAsync(__f);

}
}


}
catch (Exception)
{
return null;
}
RequestDate = DateTime.Now;
RequestCount = 0;
Logger.WriteLine($"Cached -> {StorePath}");
return f;
}
/// <summary>
/// If the cache is valid.
/// </summary>
/// <returns></returns>
public bool isValid()
{
if (File.Exists(StorePath))
{
return !isExpired();
}
else
{
Logger.WriteLine("Invalid Cache: Cache fild not found.");
return false;
}
}
/// <summary>
/// If the cache is expired.
/// </summary>
/// <returns></returns>
public bool isExpired()
{

var NOW = DateTime.Now;
var __delta = NOW - this.RequestDate;
if (__delta.Duration() > TimeSpan.FromDays(1))
{
Logger.WriteLine(MessageLevel.Warn, "Cache Expired for time.");
return true;
}
if (RequestCount > 5)
{
Logger.WriteLine(MessageLevel.Warn, "Cache Expired for using count.");
return true;
}
return false;
}
}
}
17 changes: 17 additions & 0 deletions CLUNL.CacheManager/CacheList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;

namespace CLUNL.CacheManager
{
/// <summary>
/// The list of caches.
/// </summary>
[Serializable]
public class CacheList
{
/// <summary>
/// Domains
/// </summary>
public List<Domain> Domains = new List<Domain>();
}
}
Loading

0 comments on commit e9ec60c

Please sign in to comment.