开发者

php banner queue fifo

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 do开发者_StackOverflow社区ne 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!


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.


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)


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';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜