php 横幅队列 fifo

发布于 2024-12-03 11:09:27 字数 279 浏览 2 评论 0原文

我刚刚开始在我的网站上投放广告,我希望能够为 ad_a 提供 1000 次浏览,为 ad_b 提供 2000 次浏览,假设为 ad_c 提供 10000 次浏览。

如果当时仅查看一个页面,则可以轻松更新数据库并计算出每个广告还剩多少个页面可供查看,但可以同时访问多个页面,这会使事情变得更加复杂。

我正在考虑写一个队列来管理它,请求将在数据库上一一完成。我不确定这是否是最好的想法,从来没有做过这种编码,我正在寻找一系列行为、逻辑步骤、如果有规范的话在数据库中创建什么样的表。

非常感谢您的帮助!

I'm just starting to put ad on my website and I would like to be able to give 1000 view to ad_a , 2000 to ad_b and let say 10000 to ad_c.

If only one page was view at the time it would be easy to update a DB and work out how many are left to view for each ad, but serval pages can be access at the same time and this make things more complicated.

I was thinking of writting a queue to manage it and request will be done one by one on the database. I'm not sure if this is the best idea or not, never done this kind of coding and I'm looking for a line of conduct, logical steps, what kind of table to create in the db if there is specification.

Many thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日久见人心 2024-12-10 11:09:27

您可以使用 Memcached 来存储当前的浏览次数。 Memcached 快速且轻便,您不会遇到性能问题。另外,更复杂的事情是有一个“队列”,正如你所说,一些并行进程会更新它,放入要显示的横幅,这样你就可以将它们混合起来。

You could use Memcached to store the current count of views. Memcached is fast and light, you will not have performance problems. Also, something more complicated would be to have a "queue", as you say, and some parallel process updates it putting in there what banner to show, so you could mix them up.

高速公鹿 2024-12-10 11:09:27

事实上,一个页面可以被多次查看并不一定是一个问题,这就是为什么锁定表 的用途。

LOCK TABLES ads WRITE;
SELECT ad_id FROM ads WHERE ad_views_remaining > 0 LIMIT 1;
UPDATE ads SET ad_views_remaining = ad_views_remaining -1 WHERE ad_name = THAT_AD_ID_YOU_SELECTED_BEFORE;
UNLOCK TABLES;

这样,在更新之前没有人可以读取该表。
(这个例子是针对 MySQL 的,我确信大多数其他 RDBMS 也支持锁)

The fact that one page may be viewed many times at once doesn't have to be a problem, that's why LOCK TABLES is for.

LOCK TABLES ads WRITE;
SELECT ad_id FROM ads WHERE ad_views_remaining > 0 LIMIT 1;
UPDATE ads SET ad_views_remaining = ad_views_remaining -1 WHERE ad_name = THAT_AD_ID_YOU_SELECTED_BEFORE;
UNLOCK TABLES;

This way no one can read the table until it's updated.
(this example is for MySQL, I'm sure that most other RDBMSs support locks as well)

旧情勿念 2024-12-10 11:09:27

兰特呢?

$r = rand(1, 13);
if ( $r == 1 )
    echo 'ad_a';
if ( $r > 1 and $r < 4 )
    echo 'ad_b';
if ( $r > 3 )
    echo 'ad_c';

What about rand?

$r = rand(1, 13);
if ( $r == 1 )
    echo 'ad_a';
if ( $r > 1 and $r < 4 )
    echo 'ad_b';
if ( $r > 3 )
    echo 'ad_c';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文