Skip to content

Commit f3b3520

Browse files
authored
Start caching certain level and user attributes (#1047)
This PR adds a `CacheService`, which temporarily caches `GameAsset`s requested, aswell as level-user relations, user-user-relations, levels' skill rewards and tags in-memory. This way, various level and user attributes, which otherwise require extra DB queries per level/user during serialization, will be looked up in the `CacheService` first before falling back to looking them up in the database. They will be requested from DB the first time, added to cache, and then taken from there for all other requests in the near future. This results in various level/user related API requests, like level lists and especially photo lists, being way faster (for me, requesting a list of 23 photos was reduced from ~110ms to ~50ms). Cached data, which is deemed "expired", will be refetched from DB the next time it's requested from the `CacheService`. Alternatively, it will be removed from cache by a `Thread`, which automatically removes all expired data from the service every few minutes, to prevent unused data from staying there potentially indefinitely. Certain actions, like e.g. hearting a level, will also update the cache (alongside DB) to reflect that change.
2 parents 5b595f8 + 0589c62 commit f3b3520

42 files changed

Lines changed: 665 additions & 69 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Refresh.Common/RefreshContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ public enum RefreshContext
1212
Aipi,
1313
Presence,
1414
Database,
15+
CacheService,
1516
}

Refresh.Core/Extensions/GameAssetExtensions.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ private static string TransformImage(this GameAsset asset, TokenGame game, IData
151151
dataContext.DataStore,
152152
_ => null,
153153
() => asset.AsMainlinePhotoHash,
154-
hash => dataContext.Database.SetMainlinePhotoHash(asset, hash),
154+
hash =>
155+
{
156+
dataContext.Database.SetMainlinePhotoHash(asset, hash);
157+
dataContext.Cache.CacheAsset(asset.AssetHash, asset);
158+
},
155159
() => throw new NotSupportedException(),
156160
_ => throw new NotSupportedException()
157161
);
@@ -172,9 +176,17 @@ private static string TransformImage(this GameAsset asset, TokenGame game, IData
172176
dataContext.DataStore,
173177
CropToIcon,
174178
() => asset.AsMainlineIconHash,
175-
hash => dataContext.Database.SetMainlineIconHash(asset, hash),
179+
hash =>
180+
{
181+
dataContext.Database.SetMainlineIconHash(asset, hash);
182+
dataContext.Cache.CacheAsset(asset.AssetHash, asset);
183+
},
176184
() => asset.AsMipIconHash,
177-
hash => dataContext.Database.SetMipIconHash(asset, hash)
185+
hash =>
186+
{
187+
dataContext.Database.SetMipIconHash(asset, hash);
188+
dataContext.Cache.CacheAsset(asset.AssetHash, asset);
189+
}
178190
);
179191
}
180192

Refresh.Core/Extensions/PhotoExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public static SerializedPhoto FromGamePhoto(GamePhoto photo, DataContext dataCon
1515
// NOTE: we usually would do `if psp, prepend psp/ to the hashes`,
1616
// but since we are converting the psp TGA assets to PNG in FillInExtraData, we don't need to!
1717
// also, I think the game would get mad if we did that
18-
LargeHash = dataContext.Database.GetAssetFromHash(photo.LargeAsset.AssetHash)?.GetAsPhoto(dataContext.Game, dataContext) ?? photo.LargeAsset.AssetHash,
19-
MediumHash = dataContext.Database.GetAssetFromHash(photo.MediumAsset.AssetHash)?.GetAsPhoto(dataContext.Game, dataContext) ?? photo.MediumAsset.AssetHash,
20-
SmallHash = dataContext.Database.GetAssetFromHash(photo.SmallAsset.AssetHash)?.GetAsPhoto(dataContext.Game, dataContext) ?? photo.SmallAsset.AssetHash,
18+
LargeHash = photo.LargeAsset.GetAsPhoto(dataContext.Game, dataContext) ?? photo.LargeAsset.AssetHash,
19+
MediumHash = photo.MediumAsset.GetAsPhoto(dataContext.Game, dataContext) ?? photo.MediumAsset.AssetHash,
20+
SmallHash = photo.SmallAsset.GetAsPhoto(dataContext.Game, dataContext) ?? photo.SmallAsset.AssetHash,
2121
PlanHash = photo.PlanHash,
2222
PhotoSubjects = [],
2323
};

0 commit comments

Comments
 (0)