From d101829a9097924f6a93a8eec2e9a7682cd910a9 Mon Sep 17 00:00:00 2001
From: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
Date: Thu, 7 Nov 2024 13:17:36 +0100
Subject: [PATCH] V15: Fix reload memory cache endpoint (#17446)

* Check for id later.

* Return 501 status codes from obsoleted endpoints

* Use contants

* Ensure ordering of cache refresher is not changes

---------

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
---
 .../CollectPublishedCacheController.cs        |  5 ++---
 .../StatusPublishedCacheController.cs         |  7 ++++---
 .../Implement/ContentCacheRefresher.cs        | 20 +++++++++++--------
 3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/CollectPublishedCacheController.cs b/src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/CollectPublishedCacheController.cs
index 4cbac6844674..ef759ca5a470 100644
--- a/src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/CollectPublishedCacheController.cs
+++ b/src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/CollectPublishedCacheController.cs
@@ -1,7 +1,6 @@
 using Asp.Versioning;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
-using Umbraco.Cms.Core.PublishedCache;
 
 namespace Umbraco.Cms.Api.Management.Controllers.PublishedCache;
 
@@ -11,9 +10,9 @@ public class CollectPublishedCacheController : PublishedCacheControllerBase
 {
     [HttpPost("collect")]
     [MapToApiVersion("1.0")]
-    [ProducesResponseType(StatusCodes.Status200OK)]
+    [ProducesResponseType(StatusCodes.Status501NotImplemented)]
     public async Task<IActionResult> Collect(CancellationToken cancellationToken)
     {
-        return Ok();
+        return StatusCode(StatusCodes.Status501NotImplemented);
     }
 }
diff --git a/src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/StatusPublishedCacheController.cs b/src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/StatusPublishedCacheController.cs
index aad76e7dbfba..3a9d72c12cb8 100644
--- a/src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/StatusPublishedCacheController.cs
+++ b/src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/StatusPublishedCacheController.cs
@@ -1,7 +1,6 @@
 using Asp.Versioning;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
-using Umbraco.Cms.Core.PublishedCache;
 
 namespace Umbraco.Cms.Api.Management.Controllers.PublishedCache;
 
@@ -11,7 +10,9 @@ public class StatusPublishedCacheController : PublishedCacheControllerBase
 {
     [HttpGet("status")]
     [MapToApiVersion("1.0")]
-    [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
+    [ProducesResponseType(StatusCodes.Status501NotImplemented)]
     public async Task<ActionResult<string>> Status(CancellationToken cancellationToken)
-        => await Task.FromResult(Ok("Obsoleted"));
+    {
+        return StatusCode(StatusCodes.Status501NotImplemented);
+    }
 }
diff --git a/src/Umbraco.Core/Cache/Refreshers/Implement/ContentCacheRefresher.cs b/src/Umbraco.Core/Cache/Refreshers/Implement/ContentCacheRefresher.cs
index 759e724c88ea..1f81c35912f9 100644
--- a/src/Umbraco.Core/Cache/Refreshers/Implement/ContentCacheRefresher.cs
+++ b/src/Umbraco.Core/Cache/Refreshers/Implement/ContentCacheRefresher.cs
@@ -86,15 +86,16 @@ public override void Refresh(JsonPayload[] payloads)
         var idsRemoved = new HashSet<int>();
         IAppPolicyCache isolatedCache = AppCaches.IsolatedCaches.GetOrCreate<IContent>();
 
-        foreach (JsonPayload payload in payloads.Where(x => x.Id != default))
+        foreach (JsonPayload payload in payloads)
         {
-            // By INT Id
-            isolatedCache.Clear(RepositoryCacheKeys.GetKey<IContent, int>(payload.Id));
-
-            // By GUID Key
-            isolatedCache.Clear(RepositoryCacheKeys.GetKey<IContent, Guid?>(payload.Key));
-
+            if (payload.Id != default)
+            {
+                // By INT Id
+                isolatedCache.Clear(RepositoryCacheKeys.GetKey<IContent, int>(payload.Id));
 
+                // By GUID Key
+                isolatedCache.Clear(RepositoryCacheKeys.GetKey<IContent, Guid?>(payload.Key));
+            }
 
             // remove those that are in the branch
             if (payload.ChangeTypes.HasTypesAny(TreeChangeTypes.RefreshBranch | TreeChangeTypes.Remove))
@@ -115,7 +116,10 @@ public override void Refresh(JsonPayload[] payloads)
 
             HandleNavigation(payload);
             HandlePublishedAsync(payload, CancellationToken.None).GetAwaiter().GetResult();
-            _idKeyMap.ClearCache(payload.Id);
+            if (payload.Id != default)
+            {
+                _idKeyMap.ClearCache(payload.Id);
+            }
             if (payload.Key.HasValue)
             {
                 _idKeyMap.ClearCache(payload.Key.Value);