Skip to content

Commit

Permalink
fix(api): lower case all zone values
Browse files Browse the repository at this point in the history
remove unused redis analytics
  • Loading branch information
steveoh committed Aug 13, 2024
1 parent fb924cf commit a3188cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
40 changes: 8 additions & 32 deletions src/api/Cache/RedisCacheRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ public async Task<IReadOnlyCollection<GridLinkable>> 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<GridLinkable> ?? [];
}

// 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) {
Expand All @@ -52,25 +51,13 @@ public async Task<IReadOnlyCollection<GridLinkable>> 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 [];
Expand All @@ -83,16 +70,15 @@ public async Task<IReadOnlyCollection<GridLinkable>> 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<GridLinkable> ?? [];
}

// 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) {
Expand All @@ -103,23 +89,13 @@ public async Task<IReadOnlyCollection<GridLinkable>> 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 [];
Expand Down
10 changes: 5 additions & 5 deletions src/api/Services/Hosted/CacheHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}
}

0 comments on commit a3188cd

Please sign in to comment.