caching and user permissions xperience 13

lawrence whittemore asked on February 25, 2022 18:37

I noticed that if I use cache and CheckPermissions() with the page retriever, the content that is cached is specific to the first user that hit the page. So the next user that hits the page sees the cached version for the first user even if they shouldn't see all the content. If our site needs to check permissions for every user on everything, should we not use cache? I've tried adding a cache key based on the active user and it works, but didn't know if this would create cache file that would be 2 huge given the number of users and number of pages on the site.

Recent Answers


Brian McKeiver answered on February 26, 2022 22:26

Lawrence, adding a cache key per user would be normal here if you have item level security. In terms of the size of the memory required to handle that, you should most likely be fine. But if your query does return 1,000s of items at a time, or 100,000s of items and you have 1,000s or 100,000s of users that it could be something to worry about. Do you know your typical amounts for number of pages and number of users?

1 votesVote for this answer Mark as a Correct answer

Eugene Paden answered on February 28, 2022 20:29

Per user caching works but will not easily scale. Ideally, cached data should be something that can be used by more than one user.

Is there any way you can make your content permissions based on roles? That way you can cache content based on roles instead on a per user basis.

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on February 28, 2022 20:36

How would i get the role info into the cache key?

0 votesVote for this answer Mark as a Correct answer

Eugene Paden answered on February 28, 2022 21:21 (last edited on February 28, 2022 21:21)

this might help Reference Document Query methods see section on CheckPermissions.

Since you can't create a CMSActionContext based on a role (AFAIK), you can create a dummy user for a role, e.g. Role: Writer, DummyUser: DummyWriter and add DummyWriter to the Writer role. You can then retrieve based on the context of the DummyWriter.

It gets complicated when a user is a member of more than 1 role. You can create a DummyUser for each Role combination and then just use it.

1 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on March 2, 2022 10:44

Hi Lawrence,

I will double Brian's voice here, you should likely be okay with the memory consumption, but there are couple of extra options to consider when optimizing the memory consumed by cache:

  • The main thing is to cache only necessary fields, therefore it's better to cache dry POCO objects instead of CMS objects requested via API (TreeNodes and Info objects contain a lot of system fields that you aren't using but those are cached)
  • When caching info per user, consider shorter time periods for storing this cached information. The recommendation is to align this closer to the average session duration. So than when the user finishes their session, their cached information doesn't stay in memory for too long.
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.