Restrict node for anonymous user programmatically First of all I've seen lot's of threads about restriction, but didn't find what I'm looking for.

So, my goal is to restrict the access for a specific node to anonymous users and when they try to enter that node, it will redirect them to the user/login page.

I was using Content Access module till now. It works like it should, but the problem is when this module is enabled, block caching is not working. As I know the Context module has the same issue. 

I have one big menu block and without caching, the website is 4 times slower. When the Content Access module is disabled, the block is cached and the performance is great.

Let's say the url of the page is 'apple'.

I'm looking for a way to restrict only this page programmatically, without breaking the block caching.

I'm using Drupal 7.
hook_init:

&lt;?php
// Get the current active node object if present.
$node = menu_get_object();
// Redirect to login page if NID == 42 and user is anonymous.
// User will then go back to node page due to destination being set.
if (   user_is_anonymous() 
    &amp;&amp; !empty($node) 
    &amp;&amp; !empty($node-&gt;nid) 
    &amp;&amp; $node-&gt;nid == 42
    ) {
  drupal_goto('user/login', array('query' =&gt; array('destination' =&gt; 'node/' . $node-&gt;nid)), 307);
}
?&gt;


hook_boot:

&lt;?php
$args = arg();
if (   user_is_anonymous()
    &amp;&amp; isset($args[0]) &amp;&amp; $args[0] == 'node'
    &amp;&amp; isset($args[1]) &amp;&amp; is_numeric($args[1])
    ) {
  drupal_goto('user/login', array('query' =&gt; array('destination' =&gt; 'node/' . $node-&gt;nid)), 307);
}
?&gt;