Am 04.08.2011 02:32, schrieb Stas Malyshev:
I'm not sure I understand why you need week refs there - can't you just
always use $prodDb->getProduct(1) and when you don't need it anymore
just do $prodDb->drop(1)? Or let it drop it whenever it wants to?

My Project requires lots of DB-Record <=> PHP-Object mapping, as the objects get used quite dynamically I can't know when and how often an object will be used. To make sure there's only one instance of an Object in Memory per Session, I cache them in an associative-array, the key being the record-id (actually a PHP-Object consists of many records). Then I am using the Singleton-Pattern like Library::getObject($id) which returns a new (fetched freshly from the DB) or an already used object (from the cache). This works very well and keeps me from expensively reconstructing objects from the DB. What it does not work well with is Garbage collection, since the an object will always be referenced at least once (by the cache). The longer a script is running, the higher the chance that it will run out of memory (fast). I do want to keep those objects for as long as possible, but not if the price is an "out of memory" fatal error.

My solution to this was neither nice nor correct, but I could not think of anything else: Whenever I'd create a fresh object I'd check memory against its limit and if it runs close, I will discard the oldest objects from the cache, always running the risk that my singleton is suddenly not so single anymore, because a discarded object was still referenced somewhere else.

So either I get to know if the object is actually in use somewhere else than just the cache (using the zval-refcount method) or PHP provides me with something like WeakReference or even better: SoftReference, which would give me more control as to when it would be collected.

I am open to any suggestions how I could solve my problem without WeakReference or zval-refcount (short of keeping a ref-count in userland).


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to