繁体   English   中英

Asp.Net核心缓存标记帮助器

[英]Asp.Net Core Cache Tag Helper

我正在尝试缓存包含固定的仪表板和用户个人资料图像的顶部导航栏,我使用“缓存”标签帮助程序来缓存顶部的导航栏,但是当用户更改固定的仪表板或其个人资料图像时,我想使缓存无效加载新的个人资料图片。

    <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>

我使用了varby-by和ProfileChanged属性,当用户更新配置文件时,该属性更改为true,但是当我遇到一个奇怪的行为时,即任何页面上的第一次刷新均显示新更改,而其他行为则没有,我希望更改后的第一个请求将为其他页面再次重新缓存导航。

您对工作流程感到困惑,因此请考虑逐步进行此操作。

在此示例中,我做了一个简单的.net核心项目,其中razor页面在路由查询中接受2个参数a int abool b

在剃刀视图中,我将具有以下内容:

<cache vary-by="@a">
    Time at @a @DateTime.Now;
</cache>
<br />
<cache vary-by="@b">
    Time at @b @DateTime.Now;
</cache>

现在让我们逐步了解一些结果

  1. 第一个请求,查询: ?a=1&b=true时间为1 01/10/2018 19:31:11 ; 时间为真01/10/2018 19:31:11 ;
  2. 第二个请求,查询: ?a=2&b=true时间为2 01/10/2018 19:31:20 ; 时间为真01/10/2018 19:31:11 ;
  3. 第三个请求,查询: ?a=2&b=false时间2/1/10/2018 19:31:20 ; False的时间01/10/2018 19:31:32 ;
  4. 第四次请求,查询: ?a=1&b=false时间1/1/10/2018 19:31:11 ; False的时间01/10/2018 19:31:32 ;

现在解决您的问题。 你必须设置ProfileChanged值时间戳,否则,如果你不把它保存时的默认值的数据库ProfileChanged重新设置为false ,你将留下的钥匙以前缓存的结果刷新后false 哪些默认情况下存储20分钟

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM