【发布时间】:2018-10-01 14:21:51
【问题描述】:
我正在尝试缓存包含固定仪表板和用户个人资料图像的顶部导航栏,我正在使用缓存标记帮助程序来缓存顶部导航栏,但是当用户更改固定仪表板或他的个人资料图像时,我想使其无效缓存以加载新的个人资料图片。
<cache vary-by="@userService.ProfileChanged">
<header class="header">
<nav class="navbar fixed-top navbar-light bg-light d-flex justify-content-between">
<ul class="main-nav-icons list-unstyled d-flex flex-fill mr-3">
<cache vary-by="@userService.PinnedDashboardsChanged">
@await Component.InvokeAsync("UserDashboards", new { type = "pinned" })
</cache>
<li><button class="btn btn-unstyled left-nav-toggler"><i class="glyphicons glyphicons-option-horizontal text-primary1"></i></button></li>
</ul>
<cache vary-by="@userService.ProfileChanged">
<ul class="d-flex justify-content-end align-items-center user-menu mb-0 flex-fill list-unstyled">
<li>
<ul class="user-menu-details d-flex mr-2 list-unstyled mb-0 flex-column flex-md-row">
<li class="list-inline-item">@(userService.User.UserTypeId == 1 ? "Dios Kernel" : userService.User.Environment.Name)</li>
<li class="list-inline-item"> <a href="@Url.Action("Index", "Profile")">@userService.User.FullName</a></li>
</ul>
</li>
<li class="list-inline-item user-avatar">
<div class="dropdown">
<a class="dropdown-toggle avatar-img" data-toggle="dropdown" style="background-image:url('@(string.IsNullOrEmpty(userService.User.ProfilePhoto) ? "/images/user-pic.png" : userService.User.ProfilePhoto)')">
@*<img src="@(string.IsNullOrEmpty(userService.User.ProfilePhoto) ? "/images/user-pic.png" : userService.User.ProfilePhoto)" alt="@userService.User.FullName">*@
</a>
<div class="dropdown-menu">
<a class="dropdown-item" onclick="editProfile('@Url.Action("_Edit","Profile")')">
<i class="fas fa-user-alt"></i>
Edit Profile
</a>
<a class="dropdown-item" onclick="resetPassword('@Url.Action("_ResetPassword","Account")')">
<i class="fas fa-lock"></i>
Change Password
</a>
<a class="dropdown-item" href="@Url.Action("Logout","Account")">
<i class="fas fa-sign-out-alt"></i>
Logout
</a>
</div>
</div>
</li>
</ul>
</cache>
</nav>
</header>
</cache>
我使用了 vary-by 并使用了 ProfileChanged 属性,当用户更新配置文件时我将其更改为 true,但是当我遇到一个奇怪的行为时,即任何页面上的第一次刷新显示新的更改但另一个没有,我希望更改后的第一个请求将再次为任何其他页面重新缓存导航。
【问题讨论】:
-
如果
ProfileChanged是bool,它将在更改后立即默认回到false,这将带来缓存的结果,当它是false。您应该尝试存储上次更新时间并按日期更改。 -
框架默认回false?实际上,我只在一个请求中调用它一次,一旦它以真值调用,我就将其更改为 false。我认为 ProfileChanged 应该是一个时间戳,一旦用户更新配置文件,我就会更改它
-
我的理解是缓存发生在服务器上,这意味着如果一个请求更新了缓存版本,那么无论 ProfileChanged 的值如何,所有请求都应该获得新的缓存版本
-
当
ProfileChanged=false时显示A,当ProfileChanged=true时显示B。我问你这是不是一个布尔值,所以我们可以确认在ProfileChanged变回false之后,因为在你刷新页面后它不再处于更新状态。缓存发生在服务器上,但将值视为指向存储缓存结果的地址的指针,所以是的,使用时间戳,所以指针总是不同的。 -
我认为你是对的,但我的意思是如果服务器缓存了一个组件视图,那么如果任何页面包含该组件,则在服务器上刷新它,那么为什么其他页面会获得该组件的旧状态跨度>
标签: .net asp.net-mvc asp.net-core .net-core