diff --git a/src/api/Cache/RedisCacheRepository.cs b/src/api/Cache/RedisCacheRepository.cs index d51664e9..5ba9a9a8 100644 --- a/src/api/Cache/RedisCacheRepository.cs +++ b/src/api/Cache/RedisCacheRepository.cs @@ -32,16 +32,15 @@ public async Task> FindGridsForPlaceAsync(stri } placeName = placeName.ToLowerInvariant().Trim(); + var path = $"map/place/{placeName}"; // if the place name result is in memory already return it - if (_placeNameCache.TryGetValue(placeName, out var places)) { - await _db.StringIncrementAsync($"analytics:grid-hit:{placeName.ToLowerInvariant()}", 1, CommandFlags.FireAndForget); - + if (_placeNameCache.TryGetValue(path, out var places)) { return places as IReadOnlyCollection ?? []; } // the item hasn't been looked up recently so look it up in the cache - var place = await _db.StringGetAsync(placeName.ToLowerInvariant()); + var place = await _db.StringGetAsync(path); // if the place name is not found, add it to the not found cache but check if the cache is full if (!place.IsNullOrEmpty) { @@ -52,25 +51,13 @@ public async Task> FindGridsForPlaceAsync(stri placeGridLinks.Add(new PlaceGridLink(placeName, parts[0], Convert.ToInt32(parts[1]))); } - _placeNameCache.Set(placeName, placeGridLinks, new MemoryCacheEntryOptions() + _placeNameCache.Set(path, placeGridLinks, new MemoryCacheEntryOptions() .SetPriority(CacheItemPriority.NeverRemove) .SetSize(1)); - await _db.StringIncrementAsync($"analytics:grid-hit:{placeName.ToLowerInvariant()}", 1, CommandFlags.FireAndForget); - return placeGridLinks; } - // check to see if the cache is full - var count = await _db.StringGetAsync("places"); - - // if the cache has a value then the place isn't in our list - if (count != RedisValue.Null) { - await _db.StringIncrementAsync($"analytics:grid-miss:{placeName.ToLowerInvariant()}", 1, CommandFlags.FireAndForget); - - return []; - } - // TODO! hydrate the cache from BigQuery return []; @@ -83,16 +70,15 @@ public async Task> FindGridsForZipCodeAsync(st } zipCode = zipCode.ToLowerInvariant().Trim(); + var path = $"map/zip/{zipCode}"; // if the place name result is in memory already return it - if (_zipCodeCache.TryGetValue(zipCode, out var places)) { - await _db.StringIncrementAsync($"analytics:zip-hit:{zipCode.ToLowerInvariant()}", 1, CommandFlags.FireAndForget); - + if (_zipCodeCache.TryGetValue(path, out var places)) { return places as IReadOnlyCollection ?? []; } // the item hasn't been looked up recently so look it up in the cache - var zip = await _db.StringGetAsync(zipCode.ToLowerInvariant()); + var zip = await _db.StringGetAsync(zipCode); // if the place name is not found, add it to the not found cache but check if the cache is full if (!zip.IsNullOrEmpty) { @@ -103,23 +89,13 @@ public async Task> FindGridsForZipCodeAsync(st zipGridLinks.Add(new ZipGridLink(Convert.ToInt32(zipCode), parts[0], Convert.ToInt32(parts[1]))); } - _zipCodeCache.Set(zipCode, zipGridLinks, new MemoryCacheEntryOptions() + _zipCodeCache.Set(path, zipGridLinks, new MemoryCacheEntryOptions() .SetPriority(CacheItemPriority.NeverRemove) .SetSize(1)); return zipGridLinks; } - // check to see if the cache is full - var count = await _db.StringGetAsync("zips"); - - // if the cache has a value then the place isn't in our list - if (count != RedisValue.Null) { - await _db.StringIncrementAsync($"analytics:grid-miss:{zipCode.ToLowerInvariant()}", 1, CommandFlags.FireAndForget); - - return []; - } - // TODO! hydrate the cache from BigQuery return []; diff --git a/src/api/Services/Hosted/CacheHostedService.cs b/src/api/Services/Hosted/CacheHostedService.cs index 175286ad..2e1a9f9c 100644 --- a/src/api/Services/Hosted/CacheHostedService.cs +++ b/src/api/Services/Hosted/CacheHostedService.cs @@ -86,7 +86,7 @@ private async Task HydrateCacheFromBigQueryAsync(IDatabase db, CancellationToken if (type == "place") { var zone = row["Zone"] as string ?? string.Empty; - var item = new PlaceGridLink(zone, addressSystem, weight); + var item = new PlaceGridLink(zone.ToLowerInvariant(), addressSystem, weight); if (places.TryGetValue(item.Key, out var value)) { value.Add(item); @@ -120,11 +120,11 @@ private async Task HydrateCacheFromBigQueryAsync(IDatabase db, CancellationToken } foreach (var (key, value) in places) { - await db.StringSetAsync(key, string.Join(';', value)); + await db.StringSetAsync($"map/place/{key}", string.Join(';', value)); } foreach (var (key, value) in zips) { - await db.StringSetAsync(key, string.Join(';', value)); + await db.StringSetAsync($"map/zip/{key}", string.Join(';', value)); } if (places.Count == 0 || zips.Count == 0) { @@ -133,7 +133,7 @@ private async Task HydrateCacheFromBigQueryAsync(IDatabase db, CancellationToken } Console.WriteLine("Setting key values places: " + places.Count + " zips: " + zips.Count); - await db.StringSetAsync("places", places.Count); - await db.StringSetAsync("zips", zips.Count); + await db.StringSetAsync("map/places", DateTime.Now.ToString()); + await db.StringSetAsync("map/zips", DateTime.Now.ToString()); } }