Skip to content

Commit

Permalink
Fix distributed cache null guard
Browse files Browse the repository at this point in the history
  • Loading branch information
cristipufu committed Feb 28, 2019
1 parent 34f5c25 commit fcb66fb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/AspNetCoreRateLimit/Store/DistributedCacheRateLimitStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ public DistributedCacheRateLimitStore(IDistributedCache cache)

public Task SetAsync(string id, T entry, TimeSpan? expirationTime = null, CancellationToken cancellationToken = default)
{
return _cache.SetStringAsync(id,
JsonConvert.SerializeObject(entry),
expirationTime.HasValue ? new DistributedCacheEntryOptions().SetAbsoluteExpiration(expirationTime.Value) : null,
cancellationToken);
var options = new DistributedCacheEntryOptions();

if (expirationTime.HasValue)
{
options.SetAbsoluteExpiration(expirationTime.Value);
}

return _cache.SetStringAsync(id, JsonConvert.SerializeObject(entry), options, cancellationToken);
}

public async Task<bool> ExistsAsync(string id, CancellationToken cancellationToken = default)
Expand Down
2 changes: 1 addition & 1 deletion src/AspNetCoreRateLimit/Store/MemoryCacheRateLimitStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Task RemoveAsync(string id, CancellationToken cancellationToken = default

public Task SetAsync(string id, T entry, TimeSpan? expirationTime = null, CancellationToken cancellationToken = default)
{
MemoryCacheEntryOptions options = new MemoryCacheEntryOptions
var options = new MemoryCacheEntryOptions
{
Priority = CacheItemPriority.NeverRemove
};
Expand Down

4 comments on commit fcb66fb

@TrieBr
Copy link

@TrieBr TrieBr commented on fcb66fb Mar 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristipufu When will this be pushed to NuGet? I am unable to use Redis cache without this fix

@michaelarner
Copy link

@michaelarner michaelarner commented on fcb66fb Mar 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristipufu @TrieBr I think I have the same issue -- using Distributed SQL cache, I crash in main with a null reference exception

@cristipufu
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry guys for publishing so late. You can get the latest 3.0.3 version now

@michaelarner
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristipufu Many thanks!!!!

Please sign in to comment.