Re: [SQLObject] caching an expensive property
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Oleg B. <ph...@ph...> - 2014-12-10 00:02:43
|
Hi! On Wed, Dec 10, 2014 at 12:47:17AM +0100, "Fetchinson ." <fet...@go...> wrote: > What's the best strategy for some kind of caching of an expensive property? > > Let's say I have the following: > > class obj( SQLObject ): > somefield = StringCol( ) > someotherfield = StringCol( ) > > def _get_expensivestuff( self ): > # takes a long time > return result > > And if my obj instance is alive for a long time and expensivestuff > gets accessed many times I'd like to just compute result once, store > it somewhere (where?) and return it from there. > > I'm aware of memoizing the result of a function using a suitable > decorator but I'm concerned about thread safety and I'm not terribly > knowledgeable about the internals of sqlobject. > > Any ideas would help a lot. Thanks, > Daniel I hope you know that SQLObject converts _get_* and _set_* methods to properties so you shouldn't decorate the methods. Also most caching decorators are not thread-safe so you'd better implement your own caching with proper locking. Oleg. -- Oleg Broytman http://phdru.name/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |