Skip to content

Commit

Permalink
Make Cached<K, V> implicitly convertible to V
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-sunset committed Oct 7, 2022
1 parent dc24be0 commit b88a278
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 107 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SalesReport GetReport(Guid companyId)
{
if (Cached<SalesReport>.TryGet(companyId, out var cached))
{
return cached.Value;
return cached;
}

var report = // Expensive operation: retrieve and compute data
Expand All @@ -54,7 +54,7 @@ public async Task<Picture> GetPictureOfTheDay(DateOnly date, FeedKind kind, bool
{
if (Cached<Picture>.TryGet(date, kind, compressed, out var cached))
{
return cached.Value;
return cached;
}

var api = GetApiService(kind);
Expand All @@ -76,7 +76,7 @@ public SalesReport GetReport(Guid companyId)
{
if (Cached<SalesReport>.TryGet(companyId, out var cached))
{
return cached.Value;
return cached;
}
...
return cached.Save(report, TimeSpan.FromMinutes(60), limit: 500_000);
Expand Down Expand Up @@ -134,7 +134,7 @@ var userNote = Cached.GetOrCompute(userId, GetUserNote, TimeSpan.FromMinutes(5))
// This is how it looks for TryGet
if (Cached<UserNote>.TryGet(userId, out var cached))
{
return cached.Value;
return cached;
}
...
return cached.Save(userNote, TimeSpan.FromMinutes(5));
Expand Down
2 changes: 1 addition & 1 deletion src/FastCache.Benchmarks/Comparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public string TryGetCached()
{
if (Cached<string>.TryGet(ItemKey, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand Down
4 changes: 2 additions & 2 deletions src/FastCache.Benchmarks/ComparisonMultiArg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public string TryGetCachedTwo()
{
if (Cached<string>.TryGet(ItemKey, ItemKey, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -40,7 +40,7 @@ public string TryGetCachedFour()
{
if (Cached<string>.TryGet(ItemKey, ItemKey, ItemKey, ItemKey, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand Down
28 changes: 14 additions & 14 deletions src/FastCache.Benchmarks/Defaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public string TryGetSingle()
{
if (Cached<string>.TryGet("one", out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -46,7 +46,7 @@ public string TryGetTwo()
{
if (Cached<string>.TryGet("one", "two", out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -57,7 +57,7 @@ public string TryGetThree()
{
if (Cached<string>.TryGet("one", "two", "three", out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -68,7 +68,7 @@ public string TryGetFour()
{
if (Cached<string>.TryGet("one", "two", "three", "four", out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -79,7 +79,7 @@ public string TryGetSeven()
{
if (Cached<string>.TryGet("one", "two", "three", "four", "five", "six", "seven", out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -90,7 +90,7 @@ public string TryGetSingleInt()
{
if (Cached<string>.TryGet(1, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -101,7 +101,7 @@ public string TryGetTwoInt()
{
if (Cached<string>.TryGet(1, 2, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -112,7 +112,7 @@ public string TryGetThreeInt()
{
if (Cached<string>.TryGet(1, 2, 3, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -123,7 +123,7 @@ public string TryGetFourInt()
{
if (Cached<string>.TryGet(1, 2, 3, 4, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -134,7 +134,7 @@ public string TryGetSevenInt()
{
if (Cached<string>.TryGet(1, 2, 3, 4, 5, 6, 7, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -145,7 +145,7 @@ public string TryGetTwoMixed()
{
if (Cached<string>.TryGet("one", 2, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -156,7 +156,7 @@ public string TryGetThreeMixed()
{
if (Cached<string>.TryGet("one", 2, "three", out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -167,7 +167,7 @@ public string TryGetFourMixed()
{
if (Cached<string>.TryGet("one", 2, "three", 4, out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand All @@ -178,7 +178,7 @@ public string TryGetSevenMixed()
{
if (Cached<string>.TryGet("one", 2, "three", 4, "five", 6, "seven", out var cached))
{
return cached.Value;
return cached;
}

return Unreachable<string>();
Expand Down
4 changes: 2 additions & 2 deletions src/FastCache.Benchmarks/Reads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void ReadSlice(int i)
{
if (Cached<string>.TryGet(key, out var cached))
{
ret = cached.Value;
ret = cached;
}
else
{
Expand All @@ -72,7 +72,7 @@ public string TryGetST()
{
if (Cached<string>.TryGet(key, out var cached))
{
value = cached.Value;
value = cached;
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions src/FastCache.Cached/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ internal static void ReportEvictions<T>(uint count)
var now = TimeUtils.Now;
uint totalRemoved = 0;

void CheckAndRemove(K key, long timestamp)
void CheckAndRemove(KeyValuePair<K, CachedInner<V>> kvp)
{
var (key, timestamp) = (kvp.Key, kvp.Value._timestamp);
ref var count = ref totalRemoved;

if (now > timestamp)
Expand All @@ -336,7 +337,7 @@ void CheckAndRemove(K key, long timestamp)
CacheStaticHolder<K, V>.Store
.AsParallel()
.AsUnordered()
.ForAll(item => CheckAndRemove(item.Key, item.Value._timestamp));
.ForAll(CheckAndRemove);

return totalRemoved;
}
Expand Down
Loading

0 comments on commit b88a278

Please sign in to comment.