Skip to content

Commit

Permalink
Added example console application
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Dec 5, 2023
1 parent 3f1d68b commit 9ccc66a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 6 deletions.
17 changes: 17 additions & 0 deletions CacheDrive.ExampleConsoleApp/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CacheDrive.Services;

namespace CacheDrive.ExampleConsoleApp;

public class App(ICacheService cacheService)
{
public async Task Run()
{
string cacheKey = "testKey";

await cacheService.SetAsync(cacheKey, "test text...");

Console.WriteLine(cacheService.TryGetValue(cacheKey, out string cachedValue)
? $"OK: cached value - {cachedValue}"
: $"NOK: cached value - {cachedValue}");
}
}
29 changes: 29 additions & 0 deletions CacheDrive.ExampleConsoleApp/CacheDrive.ExampleConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CacheDrive\CacheDrive.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions CacheDrive.ExampleConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CacheDrive.ExampleConsoleApp;
using CacheDrive.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();

services.RegisterCacheDrive(configuration, configurationSectionName: "CacheSettings");

services.AddTransient<App>();

// create service provider
var serviceProvider = services.BuildServiceProvider();

await serviceProvider.GetService<App>()!.Run();

serviceProvider.Dispose();
10 changes: 10 additions & 0 deletions CacheDrive.ExampleConsoleApp/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"CacheSettings": {
"CacheEnabled": true,
"CacheFolderName": "cache",
"CacheExpirationType": "Minutes",
"CacheExpiration": 60,
"CacheType": "MemoryAndFile",
"InitializeOnStartup": true
}
}
6 changes: 6 additions & 0 deletions CacheDrive.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheDrive", "CacheDrive\Ca
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheDrive.Tests", "CacheDrive.Tests\CacheDrive.Tests.csproj", "{AAB010D0-0A6D-4B38-AA42-06381BF0A570}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheDrive.ExampleConsoleApp", "CacheDrive.ExampleConsoleApp\CacheDrive.ExampleConsoleApp.csproj", "{F0233BC0-9112-48B9-80FD-DE1E75726A6C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -24,5 +26,9 @@ Global
{AAB010D0-0A6D-4B38-AA42-06381BF0A570}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAB010D0-0A6D-4B38-AA42-06381BF0A570}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAB010D0-0A6D-4B38-AA42-06381BF0A570}.Release|Any CPU.Build.0 = Release|Any CPU
{F0233BC0-9112-48B9-80FD-DE1E75726A6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0233BC0-9112-48B9-80FD-DE1E75726A6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0233BC0-9112-48B9-80FD-DE1E75726A6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0233BC0-9112-48B9-80FD-DE1E75726A6C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
7 changes: 6 additions & 1 deletion CacheDrive/Configuration/CacheSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@ public class CacheSettings
{
/// <summary>
/// Determines whether cache is enabled.
/// Default value is true.
/// </summary>
public bool CacheEnabled { get; set; } = true;

/// <summary>
/// Cache folder name.
/// Default value is "cache".
/// </summary>
public string CacheFolderName { get; set; } = "cache";

/// <summary>
/// In what units do we specify cache expiration.
/// In what units do we specify cache expiration.
/// Default value is Minutes.
/// </summary>
public CacheExpirationType CacheExpirationType { get; set; } = CacheExpirationType.Minutes;

/// <summary>
/// After what time the objects in the cache will expire. Based on CacheExpirationType.
/// Default value is 60.
/// </summary>
public int CacheExpiration { get; set; } = 60;

/// <summary>
/// Method of storing the cache.
/// Memory - only in memory.
/// MemoryAndFile - In memory while the application is running and in files after the application is closed.
/// Default value is Memory.
/// </summary>
public CacheType CacheType { get; set; } = CacheType.Memory;

Expand Down
44 changes: 39 additions & 5 deletions CacheDrive/Services/MemoryCacheFileStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace CacheDrive.Services;

internal class MemoryCacheFileStorageService : MemoryCacheService
internal class MemoryCacheFileStorageService : MemoryCacheService, IDisposable
{
private bool _disposed;

public MemoryCacheFileStorageService(IOptions<CacheSettings> settings, IDateService dateService)
: base(settings, dateService)
{
Expand All @@ -21,12 +23,15 @@ public MemoryCacheFileStorageService(IOptions<CacheSettings> settings, IDateServ
Initialize();
}
}

~MemoryCacheFileStorageService()

~MemoryCacheFileStorageService() => Dispose(false);

public void Dispose()
{
Flush();
Dispose(true);
GC.SuppressFinalize(this);
}

public override async Task FlushAsync()
{
foreach ((string key, CachedItem item) in Storage)
Expand Down Expand Up @@ -143,4 +148,33 @@ private void CreateCacheDirectory()
Directory.CreateDirectory(folderName);
}
}

private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

if (disposing)
{
// Dispose managed resources.
DisposeManageResource();
}

// Dispose unmanaged resources.
DisposeUnManageResource();

_disposed = true;
}

private void DisposeManageResource()
{

}

private void DisposeUnManageResource()
{
Flush();
}
}

0 comments on commit 9ccc66a

Please sign in to comment.