Skip to content

Commit

Permalink
Merge pull request #7 from aheintz/bugfix/ensure-release-lock
Browse files Browse the repository at this point in the history
Bugfix/ensure release lock
  • Loading branch information
aheintz authored Aug 19, 2024
2 parents 22f8fcc + 80376f1 commit f88507d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
All notable changes to this project will be documented in this file. See [versionize](https://github.com/versionize/versionize) for commit guidelines.


<a name="0.0.6"></a>
## [0.0.6](https://www.github.com/aheintz/hzcache/releases/tag/v0.0.6) (2024-03-28)

<a name="0.0.3"></a>
## [0.0.3](https://www.github.com/aheintz/hzcache/releases/tag/v0.0.3) (2024-03-20)

Expand Down
21 changes: 14 additions & 7 deletions HzMemoryCache/HzMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,23 @@ public void Set<T>(string key, T? value, TimeSpan ttl)
if (factoryLock is null)
{
options.logger?.LogDebug("Could not acquire lock for key {Key}, returning default value", key);
return value;
throw new Exception($"Could not acquire lock for key {key}");
}

value = valueFactory(key);
var ttlValue = new TTLValue(key, value, ttl, updateChecksumAndSerializeQueue, options.notificationType, (tv, objectData) =>
try
{
NotifyItemChange(key, CacheItemChangeType.AddOrUpdate, tv, objectData);
});
dictionary[key] = ttlValue;
ReleaseLock(factoryLock, "GET", key);
value = valueFactory(key);
var ttlValue = new TTLValue(key, value, ttl, updateChecksumAndSerializeQueue, options.notificationType, (tv, objectData) =>
{
NotifyItemChange(key, CacheItemChangeType.AddOrUpdate, tv, objectData);
});
dictionary[key] = ttlValue;
}
finally
{
ReleaseLock(factoryLock, "GET", key);
}

return value;
}

Expand Down
14 changes: 7 additions & 7 deletions HzMemoryCache/HzMemoryCache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/aheintz/hzcache</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>0.0.3</Version>
<Version>0.0.6</Version>
<PackageTags>cache;caching;MemoryCache</PackageTags>
<Description>Fast in-memory cache for .NET with change notifications (for distributed cache invalidation)</Description>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
Expand All @@ -31,12 +31,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8"/>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0"/>
<PackageReference Include="System.Reactive" Version="6.0.0"/>
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="8.0.0"/>
<PackageReference Include="Utf8Json" Version="1.3.7"/>
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="8.0.0" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>

</Project>
23 changes: 15 additions & 8 deletions HzMemoryCache/HzMemoryCacheAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,28 @@ public Task SetAsync<T>(string key, T? value, TimeSpan ttl)

options.logger?.LogDebug("Cache miss for key {Key}, calling value factory", key);

var factoryLock = memoryLocker.AcquireLock(options.applicationCachePrefix, options.instanceId, "GET", key, TimeSpan.FromMilliseconds(maxMsToWaitForFactory),
var factoryLock = await memoryLocker.AcquireLockAsync(options.applicationCachePrefix, options.instanceId, "GET", key, TimeSpan.FromMilliseconds(maxMsToWaitForFactory),
options.logger, CancellationToken.None);
if (factoryLock is null)
{
options.logger?.LogDebug("Could not acquire lock for key {Key}, returning default value", key);
return value;
throw new Exception($"Could not acquire lock for key {key}");
}

value = await valueFactory(key);
var ttlValue = new TTLValue(key, value, ttl, updateChecksumAndSerializeQueue, options.notificationType, (tv, objectData) =>
try
{
NotifyItemChange(key, CacheItemChangeType.AddOrUpdate, tv, objectData);
});
dictionary[key] = ttlValue;
ReleaseLock(factoryLock, "GET", key);
value = await valueFactory(key);
var ttlValue = new TTLValue(key, value, ttl, updateChecksumAndSerializeQueue, options.notificationType, (tv, objectData) =>
{
NotifyItemChange(key, CacheItemChangeType.AddOrUpdate, tv, objectData);
});
dictionary[key] = ttlValue;
}
finally
{
ReleaseLock(factoryLock, "GET", key);
}

return value;
}

Expand Down
10 changes: 5 additions & 5 deletions RedisBackedHzCache/RedisBackedHzCache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Authors>Anders Heintz</Authors>
<Product>HzCache</Product>
<RepositoryUrl>https://github.com/aheintz/hzcache</RepositoryUrl>
<Version>0.0.3</Version>
<Version>0.0.6</Version>
<Description>Distributed cache invalidation using Redis Pub/Sub for HzCache</Description>
<LangVersion>latestmajor</LangVersion>
<OutputType>Library</OutputType>
Expand All @@ -17,13 +17,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8"/>
<PackageReference Include="StackExchange.Redis" Version="2.7.33"/>
<PackageReference Include="Utf8Json" Version="1.3.7"/>
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8" />
<PackageReference Include="StackExchange.Redis" Version="2.7.33" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>

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

</Project>
8 changes: 4 additions & 4 deletions RedisIDistributedCache/RedisIDistributedCache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Authors>Anders Heintz</Authors>
<Product>HzCache</Product>
<RepositoryUrl>https://github.com/aheintz/hzcache</RepositoryUrl>
<Version>0.0.3</Version>
<Version>0.0.6</Version>
<Description>Very simple IDistributedCache implementation for redis</Description>
<LangVersion>latestmajor</LangVersion>
<OutputType>Library</OutputType>
Expand All @@ -17,9 +17,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0"/>
<PackageReference Include="StackExchange.Redis" Version="2.7.33"/>
<PackageReference Include="Utf8Json" Version="1.3.7"/>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.7.33" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>

</Project>

0 comments on commit f88507d

Please sign in to comment.