Sitecore - 以编程方式清除用户的缓存

Dav*_*ers 4 asp.net caching sitecore sitecore8

我有一个自定义角色提供程序,用户可以在执行某些操作时更改特定用户的角色.

这导致了一些问题:

  1. 如果将角色添加到用户(允许他们访问页面),则在我使用手动清除sitecore缓存之前,这不会生效 /sitecore/admin/cache.aspx

  2. 我们正在缓存用户呈现的菜单栏.但是当权限更改时,可能会添加新项目/删除项目,但这不会反映出来,因为它来自缓存版本.

有没有办法可以通过编程方式清除特定用户的sitecore缓存?

Ric*_*eal 5

是 - Sitecore执行此操作的方法是将用户详细信息添加到缓存键以进行渲染.这是存储在html缓存中,因此要清除它,您需要获取html缓存并清除包含用户名的所有条目.

这个代码片段会这样做:

// Need to clear the cache for the header and the user profile....
var htmlCache = CacheManager.GetHtmlCache(Context.Site);

// Remove all cache keys that contain the currently logged in user.
var cacheKey = $"#login:True_#user:{Context.GetUserName()}";
htmlCache.RemoveKeysContaining(cacheKey);
Run Code Online (Sandbox Code Playgroud)

这将清除当前登录用户的html缓存中的所有条目.如果要清除其他用户,只需更改Context.GetUserName()以获取要清除的特定用户.

  • 那是一些Sitecore忍者福在那里. (2认同)