dongqie4402 2012-11-17 03:38
浏览 51

如何处理用户特定的时区?

I have a forum with just under 400K users. The timezone for posts is set to Central Standard Time. I'm probably going to save timezone offsets in the database, like, +3, -8, etc, in relation to GMT. What I want to know is: Should I cache these using an op code cache like APC or xcache or file-system based caching? My goal is to avoid having continuous calls to databases for each persons timezone.

  • 写回答

1条回答 默认 最新

  • drvpvmz16241016 2012-11-23 02:54
    关注

    Having hit this issue many times before, near-term, I'd store them in the $_SESSION var as presumably on login, you'll call the DB for authentication. Eg, on authentication success, set:

    $_SESSION['timezone'] = '3'; // +3 hours from GMT
    

    That way, it's available during the entire session without having to make a single call to the DB and use it where you need to.

    If you need to overwrite it, you can:

    $_SESSION['timezone'] = '-5'; // -5 hours from GMT
    

    Or unset it at any point:

    unset($_SESSION['timezone']);
    

    Whilst you have 400K users, we don't know your setup/network/concurrent user peak or average load and therefore its hard to say if you need to look at something like distributed object caching or other method, which are mentioned below.

    Background

    Why use sessions?

    Using the PHP $_SESSION enables you to store much information instead of sporadically or frequently requesting data from a database. You can also store a large amount of data in a session, but common sense dictates that you should store as little as possible/what you need. This is because:

    By default in PHP, sessions are stored on disk (in the /tmp directory) and this will scale to a certain degree before you start to see degradation in performance - based upon how your platform is designed.

    What about an opcode cache/accelerator?

    eAccelerator, APC and others (you can find a list of PHP opcode caching engines here), won't (apart from one or two with specific functionality) improve the call/return of such variables (from the DB) as PHP is an interpreted language.

    This means that each time a PHP generated page is requested, the server must read in the various files needed and "compile" them into something the machine can understand (opcode) at runtime.

    PHP opcode cache mechanisms/engines preserve this generated code in cache so that it need only be generated a single time to server hundreds or millions of subsequent requests, thus, installing an opcode cache will reduce the time it takes to generate a page (sometimes by up to 90%).

    It's probably a good idea to use a PHP opcode cache, but not to reduce continual calls to a DB... That isn't their core purpose.

    What about larger more active/high-load sites?

    Larger sites with more traffic and a much higher load due to user-concurrecy often use memory-based solutions for storing key pieces of frequently-used and user-oriented data (including time-zone!) - more specifically "Memory-based DB tables" or "Distributed memory object caching", which can be distributed amongst one or more servers to balance out the load...

    MySQL has memory/heap tables (which are created in memory rather than on disk), but still require SQL queries to return data/information. Needless to say they are far quicker than accessing data stored on disk.

    Probably the most popular memory-based caching solution in the PHP domain is memcached, which is widely used by many companies/projects and described as:

    A high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web applications by alleviating database load... memcached allows you to take memory from parts of your system where you have more than you need and make it accessible to areas where you have less than you need.

    In the longer term (and with significant growth), such an approach is advisable., but without understanding a platform's load and pinch-points/bottlenecks, I'd suggest PHP $_SESSION by default and work from there.

    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000