Skip to content

Commit

Permalink
Fixed CacheEnabled property.
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagdynia committed Dec 11, 2023
1 parent 16a1496 commit 7102fd0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
23 changes: 19 additions & 4 deletions CacheDrive/Services/MemoryCacheFileStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ internal class MemoryCacheFileStorageService : MemoryCacheService, IDisposable
public MemoryCacheFileStorageService(IOptions<CacheSettings> settings, IDateService dateService)
: base(settings, dateService)
{
CreateCacheDirectory();

if (settings.Value.InitializeOnStartup)
if (settings.Value.CacheEnabled)
{
Initialize();
CreateCacheDirectory();

if (settings.Value.InitializeOnStartup)
{
Initialize();
}
}
}

Expand All @@ -34,6 +37,12 @@ public void Dispose()

public override async Task FlushAsync()
{
if (!CacheSettings.CacheEnabled)
{
await Task.CompletedTask;
return;
}

foreach ((string key, CachedItem item) in Storage)
{
if (!item.Dirty || item.Expired(DateService))
Expand Down Expand Up @@ -68,6 +77,12 @@ private void Flush()

public override async Task InitializeAsync()
{
if (!CacheSettings.CacheEnabled)
{
await Task.CompletedTask;
return;
}

string[] files = GetCacheFiles();

foreach (string file in files)
Expand Down
62 changes: 58 additions & 4 deletions CacheDrive/Services/MemoryCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public virtual Task FlushAsync()

public bool HasItem(string key)
{
if (!CacheSettings.CacheEnabled)
{
return true;
}

if (Storage.TryGetValue(key, out CachedItem cachedItem))
{
return !cachedItem.Expired(_dateService);
Expand All @@ -44,6 +49,12 @@ public bool HasItem(string key)

public bool TryGetValue<T>(string key, out T value)
{
if (!CacheSettings.CacheEnabled)
{
value = default;
return false;
}

if (TryGetCacheableValue(key, out CacheableItem<T> result))
{
if (result == null)
Expand Down Expand Up @@ -86,6 +97,11 @@ public bool TryGetValue<T>(string key, out T value)

public T Get<T>(string key)
{
if (!CacheSettings.CacheEnabled)
{
return default;
}

CacheableItem<T> cachedItem = GetCacheable<CacheableItem<T>>(key);

if (cachedItem is null)
Expand All @@ -98,6 +114,11 @@ public T Get<T>(string key)

public async Task<T> GetAsync<T>(string key)
{
if (!CacheSettings.CacheEnabled)
{
return await Task.FromResult<T>(default);
}

CacheableItem<T> cachedItem = await GetCacheableAsync<CacheableItem<T>>(key);

if (cachedItem is null)
Expand Down Expand Up @@ -146,11 +167,21 @@ public async Task<T> GetAsync<T>(string key)

public void Set<T>(string key, T value, int expirySeconds = 0)
{
if (!CacheSettings.CacheEnabled)
{
return;
}

SetCacheableItem(CacheableItem<T>.Create(key, value), expirySeconds);
}

public Task SetAsync<T>(string key, T value, int expirySeconds = 0)
{
if (!CacheSettings.CacheEnabled)
{
return Task.FromResult<T>(default);
}

return SetAsync(CacheableItem<T>.Create(key, value), expirySeconds);
}

Expand Down Expand Up @@ -193,21 +224,44 @@ public Task SetAsync<T>(string key, T value, int expirySeconds = 0)

internal void Set(CachedItem cachedItem)
{
if (!CacheSettings.CacheEnabled)
{
return;
}

Storage.AddOrUpdate(cachedItem.Key, _ => cachedItem, (_, _) => cachedItem);
}

internal Task SetAsync(CachedItem cachedItem)
{
if (!CacheSettings.CacheEnabled)
{
return Task.CompletedTask;
}
Storage.AddOrUpdate(cachedItem.Key, _ => cachedItem, (_, _) => cachedItem);
return Task.CompletedTask;
}

public bool Delete<T>(string key)
=> key is not null && Storage.TryRemove(CacheableItem<T>.GetCacheKey(key), out _);

{
if (!CacheSettings.CacheEnabled)
{
return false;
}

return key is not null && Storage.TryRemove(CacheableItem<T>.GetCacheKey(key), out _);
}

public Task<bool> DeleteAsync<T>(string key)
=> key is null ? Task.FromResult(false) : DeleteAsync(CacheableItem<T>.GetCacheKey(key));

{
if (!CacheSettings.CacheEnabled)
{
return Task.FromResult(false);
}

return key is null ? Task.FromResult(false) : DeleteAsync(CacheableItem<T>.GetCacheKey(key));
}

private Task<bool> DeleteAsync(string key)
=> key is null ? Task.FromResult(false) : Task.FromResult(Storage.TryRemove(key, out _));

Expand Down

0 comments on commit 7102fd0

Please sign in to comment.