-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Release. CLUNL.0.0.6.0 Added a logger.
- Loading branch information
Showing
9 changed files
with
924 additions
and
43 deletions.
There are no files selected for viewing
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,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> |
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,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; | ||
} | ||
} | ||
} |
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,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>(); | ||
} | ||
} |
Oops, something went wrong.