Hey Sean,

2009/7/2 Sean Chittenden <[email protected]>:
> It'd be super neat if Wt had either an an arbitrary object resource
> pool that is per-thread or a thread-safe per-WServer variation.  I've
> hacked in a memcache and soci connection pool in to Wt, but it'd be
> nice if there was a formal API for grabbing an object that was
> returned to the pool when it went out of scope.
>
> An example of this in action would be to store a pointer to a Memcache
> or database connection, or a Memcache Pool/factory object.  The API
> I've made for myself has all of WApplication and Widget classes
> maintaining a reference to an object pool so that I can do things like
> this:
>
> void
> MyWidget::foo() {
>   Memcache mc(mc_pool());
>   std::string val = mc.get("my_key");
>   // Do something with val and mc is automatically returned to the pool
> }

What we usually do is store this information in the specialized
WApplication object,
and then provide a static method MyApplication::instance() which
returns this MyApplication *object like this:

MyAppliation *MyApplication::instance() {
  return dynamic_cast<WApplication *>(WApplication::instance());
}

(Internally, WApplication::instance() does indeed use thread specific
storage to store the session that is bound to the current thread. Note
that this may change arbitrarily between events).

In this way you could write:

MyWidget::foo() {
  Memcache mc(MyApplication::instance()->mc_pool());
  std::string val = mc.get("my_key");
  // Do something with val and mc is automatically returned to the pool
 }

without needing to store information in the widgets.

Would this approach not meet your needs ?

Regards,
koen

------------------------------------------------------------------------------
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to