What Sitecore Climber mentioned for Static Renderings in a layout is correct. However I think you also asked how you would vary the cache by a page (or section). VaryBySection doesn't exist, neither does VaryByUrl. But you can add custom Vary By logic pretty easily. On my blog: https://dylanyoung.dev/insights/customizing-html-caching-with-sitecore-xm-with-mvc/ I talk about how you can do this.
So to achieve a Vary By Url, you would need to extend the GenerateCacheKey pipeline in RenderRendering processor group, using code like this:
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
using Sitecore.Mvc.Presentation;
using Sitecore.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Site.Infrastructure.Pipelines.Renderings
{
public class GenerateCacheKey :
Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey
{
private const string VARYBYURL = "VaryByUrl";
protected override string GenerateKey(Rendering rendering, RenderRenderingArgs args)
{
string cacheKey = base.GenerateKey(rendering, args);
Item renderingItem = rendering.RenderingItem.InnerItem;
if (!string.IsNullOrEmpty(renderingItem[VARYBYURL]))
{
cacheKey += "_#url:" + Sitecore.Context.RawUrl;
}
return cacheKey;
}
}
}
And you would patch it by using the following code:
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<mvc.renderRendering>
<processor patch:instead="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey, Sitecore.Mvc']"
type="Site.Infrastructure.Pipelines.Renderings.GenerateCacheKey, Site.Infrastructure" />
</mvc.renderRendering>
</pipelines>
</sitecore>
</configuration>
You would need to extend /sitecore/templates/System/Layout/Sections/Caching to include the checkbox option to "VaryByUrl" for the rendering that you've defined.