سؤال

أريد إنشاء تطبيق ويب مع نموذج يستمر بين طلبات HTTP.من ما أفهم لغات مثل PHP يعامل كل طلب HTTP كقسم اتصال جديد باستثناء بعض المتغيرات العالمية مثل الجلسة؛لذلك في كل مرة يقوم فيها المستخدم بتغيير الصفحات، يتم تحميل جميع فئات PHP الخاصة بي في الذاكرة مرة أخرى (وكل طلب AJAX هذا أيضا) - تتطلب مني بناء من قاعدة البيانات في كل مرة.

هل أنا مخطئ أو أحاول أن أجعل دائرة تناسبها في مربع؟يبدو أن Memcached حلا جيدا للحفاظ على نموذجي في الذاكرة بين طلبات الصفحة ولكن لا يزال يحتاج إلى تحميل ذاكرة التخزين المؤقت.بدا PHP CLI واعدة ولكن بعد النظر في ذلك، يبدو أنه سيكون أكثر مشكلة مما كانت عليه.أي اقتراحات؟

هل كانت مفيدة؟

المحلول

You should avoid requiring a persistent state in your web application; HTTP is stateless and you need to design your business logic around that. Also, PHP isn't very memory-leak safe since it isn't meant to act as a daemon or run for extended periods. You shouldn't be maintaining a database of information in PHP variables either. You can cache expensive query results in memcache and retrieve them with very little latency.

You can serialize your model, store it in a session (or memcache), and deserialize it on the next request in order to keep relevant variables within scope between requests. If you share a bit more about the specifics of your application, perhaps we can help you figure out the best way to handle this.

نصائح أخرى

I do agree that one should try to avoid sharing states between requests. But in rare cases, such as to implement a simple message queue over HTTP, It's desirable to have this feature in hand.

For php, one could use IPC via the php_shmop extension.

For nodejs, I found this

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top