Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,7 @@ __pycache__/
*.btm.cs
*.odx.cs
*.xsd.cs

#MACOS

.DS_Store
40 changes: 40 additions & 0 deletions EasyCaching.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A0F5CC7E-155F-4726-8DEB-E966950B3FE9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{F88D727A-9F9C-43D9-90B1-D4A02BF8BC98}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{EBB55F65-7D07-4281-8D5E-7B0CA88E1AD0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Core", "src\EasyCaching.Core\EasyCaching.Core.csproj", "{CE61FAA2-0233-451C-991D-4222ED61C84B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Memory", "src\EasyCaching.Memory\EasyCaching.Memory.csproj", "{B9490432-737B-4518-B851-9D40FD29B392}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Extensions", "src\EasyCaching.Extensions\EasyCaching.Extensions.csproj", "{679ABDB5-7218-4B4E-A632-10612750CD74}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CE61FAA2-0233-451C-991D-4222ED61C84B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE61FAA2-0233-451C-991D-4222ED61C84B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE61FAA2-0233-451C-991D-4222ED61C84B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE61FAA2-0233-451C-991D-4222ED61C84B}.Release|Any CPU.Build.0 = Release|Any CPU
{B9490432-737B-4518-B851-9D40FD29B392}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9490432-737B-4518-B851-9D40FD29B392}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9490432-737B-4518-B851-9D40FD29B392}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9490432-737B-4518-B851-9D40FD29B392}.Release|Any CPU.Build.0 = Release|Any CPU
{679ABDB5-7218-4B4E-A632-10612750CD74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{679ABDB5-7218-4B4E-A632-10612750CD74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{679ABDB5-7218-4B4E-A632-10612750CD74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{679ABDB5-7218-4B4E-A632-10612750CD74}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CE61FAA2-0233-451C-991D-4222ED61C84B} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
{B9490432-737B-4518-B851-9D40FD29B392} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
{679ABDB5-7218-4B4E-A632-10612750CD74} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
EndGlobalSection
EndGlobal
53 changes: 53 additions & 0 deletions src/EasyCaching.Core/CacheEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace EasyCaching.Core
{
using System;

/// <summary>
/// Cache entry.
/// </summary>
public class CacheEntry
{
/// <summary>
/// Initializes a new instance of the <see cref="T:EasyCaching.Core.CacheEntry"/> class.
/// </summary>
/// <param name="cacheKey">Cache key.</param>
/// <param name="cacheValue">Cache value.</param>
/// <param name="absoluteExpirationRelativeToNow">Absolute expiration relative to now.</param>
public CacheEntry(string cacheKey, object cacheValue, TimeSpan absoluteExpirationRelativeToNow)
{
if (string.IsNullOrWhiteSpace(cacheKey))
throw new ArgumentNullException(nameof(cacheKey));

if (cacheValue == null)
throw new ArgumentNullException(nameof(cacheValue));

if (absoluteExpirationRelativeToNow <= TimeSpan.Zero)
throw new ArgumentOutOfRangeException(
nameof(absoluteExpirationRelativeToNow),
absoluteExpirationRelativeToNow,
"The relative expiration value must be positive.");

this.CacheKey = cacheKey;
this.CacheValue = cacheValue;
this.AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
}

/// <summary>
/// Gets the cache key.
/// </summary>
/// <value>The cache key.</value>
public string CacheKey { get; private set; }

/// <summary>
/// Gets the cache value.
/// </summary>
/// <value>The cache value.</value>
public object CacheValue { get; private set; }

/// <summary>
/// Gets the absolute expiration relative to now.
/// </summary>
/// <value>The absolute expiration relative to now.</value>
public TimeSpan AbsoluteExpirationRelativeToNow { get; private set; }
}
}
10 changes: 10 additions & 0 deletions src/EasyCaching.Core/EasyCaching.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="Internal\" />
</ItemGroup>
</Project>
115 changes: 115 additions & 0 deletions src/EasyCaching.Core/EasyCachingManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
namespace EasyCaching.Core
{
using System;
using System.Collections.Generic;

/// <summary>
/// Easycaching manager.
/// </summary>
public class EasyCachingManager
{
/// <summary>
/// The caching providers.
/// </summary>
private readonly IEasyCachingProvider[] _cachingProviders;

/// <summary>
/// Initializes a new instance of the <see cref="T:EasyCaching.Core.EasyCachingManager"/> class.
/// </summary>
/// <param name="cachingProviders">Caching providers.</param>
public EasyCachingManager(IEasyCachingProvider[] cachingProviders)
{
if (cachingProviders == null || cachingProviders.Length <= 0)
{
throw new ArgumentNullException(nameof(cachingProviders));
}

this._cachingProviders = cachingProviders;
}

/// <summary>
/// Get cacheValue by specified cacheKey.
/// </summary>
/// <returns>The cacheValue.</returns>
/// <param name="cacheKey">Cache key.</param>
public object Get(string cacheKey)
{
if (string.IsNullOrWhiteSpace(cacheKey))
{
throw new ArgumentNullException(nameof(cacheKey));
}

object result = null;

var missed = new HashSet<int>();

for (int i = 0; i < _cachingProviders.Length; i++)
{
var cachingProvider = _cachingProviders[i];

var cacheValue = cachingProvider.Get(cacheKey);

if (cacheValue != null)
{
result = cacheValue;
break;
}
else
{
missed.Add(i);
}
}

if (result == null)
{
result = new EmptyCachingObject();
}

//handle missed cache
foreach (var item in missed)
{
var cachingProvider = _cachingProviders[item];

var cacheEntry = new CacheEntry(cacheKey,
result,
result.GetType().Equals(typeof(EmptyCachingObject))
? TimeSpan.FromSeconds(120)
: TimeSpan.FromSeconds(3600 + new Random().Next(1, 120)));
cachingProvider.Set(cacheEntry);
}

return result;
}

/// <summary>
/// Set the specified cacheKey, cacheValue and absoluteExpirationRelativeToNow.
/// </summary>
/// <returns>The set.</returns>
/// <param name="cacheKey">Cache key.</param>
/// <param name="cacheValue">Cache value.</param>
/// <param name="absoluteExpirationRelativeToNow">Absolute expiration relative to now.</param>
public void Set(string cacheKey, object cacheValue, TimeSpan absoluteExpirationRelativeToNow)
{
var cacheEntry = new CacheEntry(cacheKey, cacheValue, absoluteExpirationRelativeToNow);
this.Set(cacheEntry);
}

/// <summary>
/// Set the specified cacheEntry.
/// </summary>
/// <returns>The set.</returns>
/// <param name="cacheEntry">Cache entry.</param>
public void Set(CacheEntry cacheEntry)
{
for (int i = 0; i < _cachingProviders.Length; i++)
{
var cachingProvider = _cachingProviders[i];

cacheEntry.AbsoluteExpirationRelativeToNow.Add(TimeSpan.FromSeconds(new Random().Next(1, 120)));

cachingProvider.Set(cacheEntry);
}
}

}
}
10 changes: 10 additions & 0 deletions src/EasyCaching.Core/EmptyCachingObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EasyCaching.Core
{
/// <summary>
/// Empty caching object.
/// </summary>
public class EmptyCachingObject
{

}
}
22 changes: 22 additions & 0 deletions src/EasyCaching.Core/IEasyCachingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace EasyCaching.Core
{
/// <summary>
/// EasyCaching provider.
/// </summary>
public interface IEasyCachingProvider
{
/// <summary>
/// Set the specified cacheEntry.
/// </summary>
/// <returns></returns>
/// <param name="cacheEntry">Cache entry.</param>
void Set(CacheEntry cacheEntry);

/// <summary>
/// Get the specified cacheKey.
/// </summary>
/// <returns>The cache value.</returns>
/// <param name="cacheKey">Cache key.</param>
object Get(string cacheKey);
}
}
14 changes: 14 additions & 0 deletions src/EasyCaching.Core/Internal/ICachable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace EasyCaching.Core.Internal
{
/// <summary>
/// Cachable.
/// </summary>
public interface ICachable
{
/// <summary>
/// Gets the cache key.
/// </summary>
/// <value>The cache key.</value>
string CacheKey { get; }
}
}
Loading