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

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

<pre><code>{% cache using key "myImportantUserCache" ~ currentUser.id %}
</code></pre>

CC BY-SA 3.0
2014-08-22T18:13:47.117Z
Have I told you lately, that I love you?
Paul
CC BY-SA 3.0
2014-08-22T18:22:20.583Z
Not since last night! <wink>
Brad Bell
CC BY-SA 3.0
2014-08-24T11:34:20.647Z
Awesome, I've been waiting for this! :)
André Elvan
CC BY-SA 3.0
2014-08-22T18:12:33.673Z
2014-08-22T18:12:33.673Z
Brad Bell
<p>You could add user dependent content in between your <code>cache</code> 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):</p>

<pre><code>{% cache %}
    &lt;div class="hidden"&gt;{{ currentUser.id }}&lt;/div&gt;
{% endcache %}
</code></pre>

<p>.</p>

<p>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 <a href="https://craftcms.stackexchange.com/q/2059/125">comment</a><sup>1</sup>).</p>

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

<p>and then use <a href="http://buildwithcraft.com/docs/routing#dynamic-routes" rel="nofollow noreferrer">dynamic routes</a> to make them work:<br>
URI format:<code>&lt;*&gt;/blog/&lt;*&gt;</code> → Template: <code>blog/_entry.html</code></p>

<p>.</p>

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

CC BY-SA 3.0
2014-08-22T10:47:00.970Z
2014-08-22T10:47:00.970Z
2017-04-13T12:51:00.253Z
carlcs
<p>Is there a way to cache content with the (% cache %} tag on a per-user base?</p>

<p>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.</p>

<users><cache>
How to cache content per user?
# How to cache content per user?

(Asked by: username_0 on 2014-08-22)

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.

**Tags:** users, cache


---

## 2 answers

### Answer by username_1 on 2014-08-22. Score: 4 | Accepted answer

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

> **Comments:**
> **username_0**: Have I told you lately, that I love you?  
> **username_1**: Not since last night! <wink>  
> **username_2**: Awesome, I've been waiting for this! :)  

---

### Answer by username_3 on 2014-08-22. Score: 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 %}
    {{ currentUser.id }}
{% 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:
{{ entry.title }}
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."


---

