From: Clayton P. <cl...@si...> - 2007-01-04 22:08:23
|
We created a script that you can run via zopectl run that deletes carts that are older than a day and have no orders associated with them. <code block> from AccessControl.SecurityManagement import newSecurityManager from DateTime import DateTime mysite = app.mysite uf = app['acl_users'] zopeuser = uf.getUser('zope').__of__(uf) newSecurityManager(None,zopeuser) cart_manager = mysite.portal_cartmanager carts = cart_manager.contentValues() remove_carts = [] yesterday = DateTime() - 1 for cart in carts: cart_date = DateTime(cart.CreationDate()) orders = cart.getBackReferenceImpl() if cart_date < yesterday and not orders: remove_carts.append(cart.getId()) cart_manager.manage_delObjects(remove_carts) get_transaction().setUser('zope') get_transaction().note('/mysite') get_transaction().note('Removed unneeded carts from the system.') get_transaction().commit() </code block> You will have to add a Zope user to the root of your instance and then run this script via cron once a day using zopectl run. Then you may want to also have another cron job using zeopack to keep the Data.fs size down. I don't know what the plans are for the future of the cart objects but i think they are not designed right. The fact that the orders depend on the 'Product' still existing and being referenced to by a cart is really not the right way to go in my opinion. Change the price or delete the item and your order details could also change. I haven't looked at the code in a while so maybe this has changed? Best, Clayton -- S i x F e e t U p , I n c . | "Nowhere to go but open source" Silicon Valley: +1 (650) 401-8579 x603 Midwest: +1 (317) 861-5948 x603 Toll-Free: 1-866-SIX-FEET mailto:cl...@si... http://www.sixfeetup.com | Zope/Plone Custom Development Nuno Maltez wrote: > Hello, > > We've been using Plone Mall and noticed the the Cart's default behaviour can > lead to bloating the ZODB. > > The thing is, every time CartManager.getOrCreateCart() is called (cart_view, > addotcart, etc...) a new Cart is created (if there is none associated with > the current session) in the ZODB. This means that we end up with a huge > number of Cart object in the Data.fs that are completely useless because the > user never actually made the order, or maybe because it's just an indexing > engine crawling the site, I don't know. The result: we ended up with over > 35 000 Cart objects on a database in 3/4 months, and only a couple of them > were part of an order - in this case the on-line shop is just a marginal > feature of the website, but I imagine it would be a large problem as well if > it were it's central business. > > Using fsdump and adding up all the transactions associated with cart_view > reaches the total: > > /portal_ccdrc/cart_view: 5915959019 > > that's almost 6GB in an 8GB Data.fs (I think these numbers are correct). > > > We thought of 2 possible solutions: > > a) Create the Cart on the SESSION, something like > REQUEST.SESSION['cart'] = cart > and only add it to portal_cartmanager whenever an Order is completed. > However, this runs into a few problems.Permissions (ownership, > probably) do not work if the Cart is in the session, and a contained > CartItem can't access the catalogs (reference_catalog and probably > portal_catalog as it seems to be a CatalogAware object). > At least it's not just a simple change, but seems that a more deep rewrite of > PloneMall's default objects would be needed. > > b) Make a simple script, run by a cron job, that searches portal_cartmanager > for all carts that: > 1. are not referenced by an order > 2. are not associated with an active session > deletes those Carts and packs the ZODB. This is more of a workaround than a > real solution, but it would suffice for now. Finding (1) is easy, but (2) > seems more of a problem as session_data_manager doesn't seem to use > REQUEST.SESSION.getId() as a key to store the SESSION objects, so I can't > just use getSessionDataByKey(key=cart.getId()) to know it that cart is being > used in an active session. > > > So, before I spend more time trying to solve this, I would like to ask if > anyone else has faced this problem before. Is there a fix already? If not, > what seems to be the best solution for those with more experience using > PloneMall? Or am I just missing something obvious? > > Thanks in advance, > Nuno |