title: Caching a function containing some constants, in Django
body: <p>In a Django website I maintain (v 1.7), I divide every 24-hr day into various time-windows, and then depending on which time window a user logged in at, I process some stuff for the said user. It doesn't matter what date it is, only the time-window matters. </p>
  
  <p>Currently, the time-windows and their related processing is done in a function in my <strong>views.py</strong> like so:</p>
  
  <pre><code>from datetime import time as time_object
  
  def WhichWindow(time_now):
      window1_least = time_object(hour=0, minute=0)
      window1_most = time_object(hour=3, minute=0) 
      window2_least = time_object(hour=3, minute=0)
      window2_most = time_object(hour=6, minute=30)
      window3_least = time_object(hour=6, minute=30) 
      window3_most = time_object(hour=11, minute=50)
      window4_least = time_object(hour=11, minute=50)
      window4_most = time_object(hour=14, minute=55)
      window5_least = time_object(hour=14, minute=55)
      window5_most = time_object(hour=16, minute=55)
      window6_least = time_object(hour=16, minute=55)
      window6_most = time_object(hour=20, minute=0)
      window7_least = time_object(hour=20, minute=0)
      window7_most = time_object(hour=0, minute=0)
      if window1_least &lt;= time_now &lt; window1_most:
          variable1 = #something
          variable2 = #something
          variable3 = #something
      elif window2_least &lt;= time_now &lt; window2_most:
          variable1 = #something
          variable2 = #something
          variable3 = #something
      elif window3_least &lt;= time_now &lt; window3_most:
          variable1 = #something
          variable2 = #something
          variable3 = #something
      elif window4_least &lt;= time_now &lt; window4_most:
          variable1 = #something
          variable2 = #something
          variable3 = #something
      elif window5_least &lt;= time_now &lt; window5_most:
          variable1 = #something
          variable2 = #something
          variable3 = #something
      elif window6_least &lt;= time_now &lt; window6_most:
          variable1 = #something
          variable2 = #something
          variable3 = #something
      elif window7_least &lt;= time_now &lt; window7_most:
          variable1 = #something
          variable2 = #something
          variable3 = #something
      else:
          variable1 = #something
          variable2 = #something
          variable3 = #something
      return variable1, variable2, variable3
  </code></pre>
  
  <p><em>Note: <code>time_now</code> in the function is essentially <code>datetime.now().time()</code>.</em> </p>
  
  <p>The function above is accessed very frequently for every user. I feel if I <code>cache</code> my time-window definitions, it's going to be a performance boost. </p>
  
  <p>What should I do? Should I simply add the <code>@cache_page(60 * 60 * 24)</code> decorator on top of this function? I feel if I do that, that'll cache the result of this function as well, whereas I solely want to cache the time-window definitions. So what should I do? </p>
  
  <p>Please advise the best performing way to accomplish this, alongwith an illustrative example. I'm a newbie and still wrapping my head around most of these concepts.   </p>