title: Caching functions in Aiohttp-Asyncio
body: <p>I'm trying to cache a function from aiohttp using memcached. Caching I want to do as a decorator. 
  An example of calling the decorator looks like this:</p>
  
  <pre><code>@cache("main_page", expire=25)
  @asyncio.coroutine
  def page(request):
      return aiohttp_jinja2.render_template('index', request, {} )
  </code></pre>
  
  <p>The function decorator which deals with caching looks like this:</p>
  
  <pre><code>def cache_key(name, kwargs):
      key = b'\xff'.join(bytes(k, 'utf-8') + b'\xff' + pickle.dumps(v) for k, v in kwargs.items())
      key = bytes(hashlib.sha1(key).hexdigest(), 'ascii')
      return key
  
  
  def cache(name, expire=0):
      def decorator(func):
          @asyncio.coroutine
          def wrapper(request=None, **kwargs):
              assert isinstance(request, (aiohttp.web_reqrep.Request, type(None))), type(request)
              args = [r for r in [request] if isinstance(r, aiohttp.web_reqrep.Request)]
              assert isinstance(mc, aiomcache.Client)
              key = cache_key(name, kwargs)
              value = yield from mc.get(key)
              if value is None:
                  value = yield from func(*args, **kwargs)
                  yield from mc.set(key, pickle.dumps(value,   protocol=pickle.HIGHEST_PROTOCOL), exptime=expire)
              else:
                  value = pickle.loads(value)
              print(value)
              return value
  
          return wrapper
  
      return decorator
  </code></pre>
  
  <p>But I get error in console:</p>
  
  <pre><code>  File "/usr/local/lib/python3.4/dist-packages/aiohttp_debugtoolbar/panels/headers.py", line 25, in process_response
      sorted(response.headers.items())]
    File "aiohttp/_multidict.pyx", line 464, in aiohttp._multidict._ItemsView.__iter__ (aiohttp/_multidict.c:8990)
  TypeError: 'NoneType' object is not iterable
  </code></pre>
  
  <p>As far as I understand the question:</p>
  
  <blockquote>
    <p>How to serialize/deserialize Response, if possible, and if impossible,
    what to do for caching result - there are some workaround ?</p>
  </blockquote>