Cache data in PHP SESSION, or query from db each time?

后端 未结 2 969
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 06:34

Is it \"better\" (more efficient, faster, more secure, etc) to (A) cache data that is used on every page load in the $_SESSION array (but still querying a table for a flag t

2条回答
  •  有刺的猬
    2021-01-12 07:22

    It depends on what you're session handler is. Your session handler could be MySQL, and thus the question would not be which is better, but how to optimize your session handling.

    The default PHP session handler is files, but it can be changed to mysql quite easily.

    If you're talking about non-user specific data, then just save it to the DB. Worry about optimizing if you run into problems later. It is usually much more beneficial to use a better design pattern then thinking about optimizing before hand. Design your code so you can easily use a different handler for storage, and you won't have optimizing problems later.

    If it is user specific, use the session, but use an appropriate session handler if necessary.

提交回复
热议问题