在memcache服务器上的会话中缓存大数据(cache large data in session on memcache server)

编程入门 行业动态 更新时间:2024-10-26 04:27:41
在memcache服务器上的会话中缓存大数据(cache large data in session on memcache server)

我为我的php服务器设置了两个memcached服务器来缓存会话中的一些数据库数据。 当我选择带有20列数据的超过20000条记录并缓存到php会话中时,执行session_start()需要1秒多的时间; 为什么? 我怎样才能加快它?

I had set two memcached server for my php server to cache some database data in session. When I select more than 20000 records with 20 columns data and cache into php session, it would take more than 1 second to do session_start(); Why? And how can I speed up it?

最满意答案

我认为启动会话需要很长时间,因为memcache和PHP必须为每个请求反序列化数十万个表单元。 我打赌它比你刚使用数据库需要更长的时间。

您的实现效率非常低:如果单个用户需要访问其会话,则会加载有关所有用户的数据。 要显着加快速度,请更改在缓存中存储值的方式; 只存储当前用户的数据,而不是整个表!

而不是这样建立缓存:

$SESSION['data'] = [ 'ID_1'=>['name'=>'jane', 'email'=>'...'], 'ID_2'=>['name'=>'john', 'email'=>'...'], ... //20,000 rows ];

这样做:

$SESSION['data'] = [ 'ID_1'=>['name'=>'jane', 'email'=>'...']//a single row ];

现在,当您调用session_start()仅加载当前用户的行,而不是整个表。

附录

我的回答是针对浏览他的网页的个人用户。 如果管理员要求脚本过滤所有记录,我只需使用SELECT语句访问数据库。 确保仅选择所需的列和行。 它可能比从缓存中加载整个表更快。

无论如何,您可以通过注释掉session_start()和所有$_SESSION引用来快速测试它,并查询数据库以获取所需的信息。

I think it takes a long time to start a session because memcache and PHP have to unserialize hundreds of thousands of table cells for every request. I bet it takes longer than if you just used a database.

Your implementation is really inefficient: if a single user needs access to her session, you load data about all users. To speed things up significantly, change how you store values in the cache; store only the current user's data, not the whole table!

Instead of building the cache this way:

$SESSION['data'] = [ 'ID_1'=>['name'=>'jane', 'email'=>'...'], 'ID_2'=>['name'=>'john', 'email'=>'...'], ... //20,000 rows ];

Just do this:

$SESSION['data'] = [ 'ID_1'=>['name'=>'jane', 'email'=>'...']//a single row ];

Now when you call session_start() only the current users' row is loaded, not the whole table.

Addendum

My response was addressing an individual user browsing his pages. In the case of an admin who requires the script to filter through all records, I would just access the database with SELECT statements. Make sure to select only the columns and the rows needed. It will probably be faster than loading the whole table from the cache.

Anyway, you can test it very quickly by commenting out session_start() and all $_SESSION references, and querying the database for the info you need.

更多推荐

本文发布于:2023-08-04 15:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   器上   数据   memcache   cache

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!