2

Is there a way to cache content with the (% cache %} tag on a per-user base?

If user A is logged in, his content gets cached. User B logs in, but his content should not interfere with that of User A.

4

In the upcoming Craft 2.2 release, we've added a using key parameter to the {% cache %} tag for for situations like this.

So you'd be able to do something like this for per user caching:

{% cache using key "myImportantUserCache" ~ currentUser.id %}
3
  • Have I told you lately, that I love you? – Paul Aug 22 '14 at 18:13
  • Not since last night! <wink> – Brad Bell Aug 22 '14 at 18:22
  • Awesome, I've been waiting for this! :) – André Elvan Aug 24 '14 at 11:34
1

You could add user dependent content in between your cache tags to make the cache invalidate itself every time the page is requested by a new user. This probably doesn't help you much (but depends on the use case):

{% cache %}
    <div class="hidden">{{ currentUser.id }}</div>
{% endcache %}

.

Another idea is to have user specific URLs. That'd work because each URL gets its own cache entry in the DB (see Brad's comment1).

For the pages in question, add user specific info to the URL in your links:
<a href="/{{ currentUser.id }}/blog/{{ entry.slug }}">{{ entry.title }}</a>

and then use dynamic routes to make them work:
URI format:<*>/blog/<*> → Template: blog/_entry.html

.

1: "The cache, by default will cache it's contents on a per URL and per locale basis."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.