kgabryje commented on code in PR #35264:
URL: https://github.com/apache/superset/pull/35264#discussion_r2517321968
##########
superset-frontend/src/utils/cachedSupersetGet.ts:
##########
@@ -27,3 +27,66 @@ export const cachedSupersetGet = cacheWrapper(
supersetGetCache,
({ endpoint }) => endpoint || '',
);
+
+/**
+ * Clear cached responses for dataset-related endpoints
+ * @param datasetId - The ID of the dataset to clear from cache
+ */
+export function clearDatasetCache(datasetId: number | string): void {
+ if (datasetId === null || datasetId === undefined || datasetId === '')
return;
+
+ const keysToDelete: string[] = [];
+ const datasetIdStr = String(datasetId);
+
+ supersetGetCache.forEach((_value, key) => {
+ // Match exact dataset ID patterns:
+ // - /api/v1/dataset/123 (exact match or end of URL)
+ // - /api/v1/dataset/123/ (with trailing slash)
+ // - /api/v1/dataset/123? (with query params)
+ const patterns = [
+ `/api/v1/dataset/${datasetIdStr}`,
+ `/api/v1/dataset/${datasetIdStr}/`,
+ `/api/v1/dataset/${datasetIdStr}?`,
+ ];
+
+ for (const pattern of patterns) {
+ if (key.includes(pattern)) {
+ // Additional check to ensure we don't match longer IDs
+ const afterPattern = key.substring(
+ key.indexOf(pattern) + pattern.length,
+ );
+ // If pattern ends with slash or query, it's already precise
+ if (pattern.endsWith('/') || pattern.endsWith('?')) {
+ keysToDelete.push(key);
+ break;
+ }
+ // For the base pattern, ensure nothing follows or only valid
separators
+ if (
+ afterPattern === '' ||
+ afterPattern.startsWith('/') ||
+ afterPattern.startsWith('?')
+ ) {
+ keysToDelete.push(key);
+ break;
+ }
+ }
+ }
+ });
+
+ keysToDelete.forEach(key => supersetGetCache.delete(key));
+}
+
+/**
+ * Clear all cached dataset responses
+ */
+export function clearAllDatasetCache(): void {
+ const keysToDelete: string[] = [];
+
+ supersetGetCache.forEach((_value, key) => {
+ if (key.includes('/api/v1/dataset/')) {
+ keysToDelete.push(key);
+ }
+ });
+
+ keysToDelete.forEach(key => supersetGetCache.delete(key));
Review Comment:
nit: I think we could do it in 1 loop? According to Claude it's safe to
delete map entries while iterating over it.
```
for (const key of supersetGetCache.keys()) {
if (key.includes('/api/v1/dataset/')) {
supersetGetCache.delete(key);
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]