Thank you for the suggestion Moritz, but with the import placed inside the 
__call__ methode, I still get the same error....

 

On Wednesday, 31 July 2013 09:59:20 UTC+1, Moritz Schlarb wrote:
>
> I'd suspect it's simply a matter of importing scope! I had that a while 
> back, too... 
>
> Try placing from tg import cache inside the __call__ method and see if it 
> works then!
>
> Am Mittwoch, 31. Juli 2013 10:20:27 UTC+2 schrieb Rob:
>>
>> Using TG v2.2.2 I am trying to access the beaker cache from a custom 
>> middleware object but I'm getting an error:
>>  "TypeError: No object (name: cache) has been registered for this thread."
>>
>> As per the docs, I am using `wrap_app` in the  construction of 
>> `make_base_app`
>> Stripped down to its bare essentials, I have:
>>
>> from tg import cache
>> from proj.config.app_cfg import base_config
>> from proj.config.environment import load_environment
>>
>> class MyMiddleware(object):
>>     base_path = "/my_route"
>>
>>     def __init__(self, app):        
>>         self.app = app
>>
>>     def __call__(self, environment, start_response):
>>         path = environment.get('PATH_INFO')
>>         if not path.startswith(self.basepath):
>>             return self.app(environment, start_response)
>>
>>         # access cache here.
>>         # c = environment.get('beaker.cache')   
>>         # rg = c.regions             # this works
>>         
>>         # rg = cache.regions         # should be available from the 
>> import but throws:
>>                                      # TypeError: No object (name: cache) 
>> registered in the thread
>>
>>         status = '200 OK'
>>         response_headers = [('Content-type','text/plain')]
>>         start_response(status, response_headers)
>>         return ['Hello world!\n']
>>
>>
>> def make_app(global_conf, full_stack=True, **app_conf):
>>     app = make_base_app(global_conf,
>>                         full_stack=True, 
>>                         wrap_app=MyMiddleware,
>>                         **app_conf
>>                         )
>>     
>> In the __call__() of the custom middleware I am able to access the cache, 
>> using:
>>     c = environment.get('beaker.cache')
>>
>> But my understanding from the docs is that I should be able to access 
>> this via the `from tg import cache` -- and I need to be able to access the 
>> cache from a separate function, called by the __call__().  In other words, 
>> the cache (in the stack) should be available to any controllers/methods 
>> post setup of the custom middleware.  
>>
>> My understanding of the docs is that, having used `wrap_app` I should 
>> have access to the full stack (including the cache) --- but I am unable to 
>> access it... 
>>
>> How am I able to access the cache?  (Is my method above close?!)
>>
>> Many thanks,
>> Rob
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to