在循环内调用 redis 'get' 以显示论坛帖子视图数据 [英] Calling redis 'get' inside a loop to display forum post view data

查看:82
本文介绍了在循环内调用 redis 'get' 以显示论坛帖子视图数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Rails 重建一个论坛/板.要求之一是记录主题的视图信息.

在当前系统中,每次加载页面时都会调用数据库更新该帖子的查看计数.

我想避免这种情况,我正在考虑使用类似于这篇文章的技术来实现 redis 来记录该信息 - jQuery Redis 点击计数器跟踪缓存的 Rails 页面的视图

因此,我会向记录视图的控制器发出请求 - 通过 javascript - 然后一个 cron 作业会将 redis 使用数据移动到数据库(从 redis 中删除它).

我的困惑是,当前系统提供实时使用信息,这将是未来的期望.使用 Heroku - 按照我的计划 - 最频繁的 cron 作业将每小时运行一次,我认为这是不可接受的.

我的想法是我可以将使用信息存储在 redis 中,然后当我循环遍历主题时,我会将存储在 redis 中的使用值与从 cron 作业保存在数据库中的值结合起来.

这是一个愚蠢的想法吗?我是 redis 的新手,所以我真的不知道什么是可能的.像我建议的那样在循环中进行 redis 调用是一个巨大的禁忌吗?

解决方案

如果您确实需要旧应用程序来维护实时统计信息,并且想要使用 Redis,那么您将不得不更改遗留代码才能访问它.

这是您的代码的起点.

每次点击时,您都可以在 Redis 中检查线程的计数器.如果计数器键不存在,则激活加载.

所以这将是一种保持统计更新的方法(使用 php,phpredis 客户端):

尝试{$redis = new \Redis();$thread_id = getFromPostGet("thread_id");//假设是$key = 'ViewCounterKey:' .$thread_id;//每个线程都有一个计数器键$redis->multi();//开始交易如果 (!$redis->exists($key)) {$counter = getFromDB("count(*) where thread_id = $thread_id");//假设是$redis->set($key, $counter);}$redis->incr($key);//每次点击都会增加计数器$redis->exec();//结束交易}catch (\RedisException $e) {echo "服务器关闭";}

因此,此解决方案可以与 cron 作业结合使用,这将保留视图计数,并且每个 cron 之间的 1 小时延迟无关紧要,因为您总是在查看内存(Redis,而不是 DB).

希望这是有道理的.

I'm rebuilding a forum/board in rails. One of the requirements is that view information be recorded for a subject.

In the current system, a database call is made every time the page is loaded updating the view count for that post.

I would like to avoid that and am looking at implementing redis to record that information using a technique similar to this post - jQuery Redis hit counter to track view of cached Rails pages

So I would make a request to a controller that would record the view - via javascript - and then a cron job would move the redis usage data to the database (removing it from redis).

My quandary is that the current system offers real-time usage information so that will be the expectation moving forward. Using Heroku - as I plan - the most frequent cron jobs would run hourly, which I don't think will be acceptable.

My thought was that I could store the usage information in redis and then while I'm looping through the subjects, I would combine the usage value stored in redis with the value that had been saved in the database from the cron job.

Is this a dumb idea? I'm new to redis so I don't really know what is possible. Is it a huge no-no to do a redis call in a loop like I'm suggesting?

解决方案

If you really need the old application to mantain real-time statistics, and want to use Redis, then you would have to change legacy code to access it.

Here's a starting point for your code.

At every hit, you can check thread's counter in Redis. If the counter key doesn't exist, this activates load.

So this would be a way to keep the stats updated (using php, phpredis client):

try {
    $redis = new \Redis();
    $thread_id = getFromPostGet("thread_id"); //suppose so
    $key = 'ViewCounterKey:' . $thread_id; //each thread has a counter key

    $redis->multi(); //begin transaction
    if (!$redis->exists($key)) {
        $counter = getFromDB("count(*) where thread_id = $thread_id"); //suppose so
        $redis->set($key, $counter);
    }
    $redis->incr($key); //every hit incrs the counter
    $redis->exec(); //end transaction
}
catch (\RedisException $e) {
    echo "Server down";
}

So this solution can be put together with cron jobs, which would persist the view count, and the latency of 1h between each cron would not matter, because you're always looking into memory (Redis, not DB).

Hope that makes sense.

这篇关于在循环内调用 redis 'get' 以显示论坛帖子视图数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆