`
flowercat
  • 浏览: 362287 次
社区版块
存档分类
最新评论

get the number of online by Play

    博客分类:
  • java
阅读更多

It's not an easy problem to solve because HTTP is a stateless protocol. A
user is never really "Connected" to your application.
The best you can do is to count requests made by distinct users during the X
last minutes.

One solution is to do like Loïc shown you. But using the cache in this way
is not very safe ... be aware of races conditions ...
You can keep the Users list as a static instance of your application but it
will only work if there is only one application instance
(however it's totally acceptable for small applications ...).

But using the cache can be a very nice way to handle this requirement. The
easy way is to use a counter and to increment it for each new
user. But the difficult part is to decrement the counter when the user is
"disconnected" (because you don't really know it). Using 2 counters
you can however handle it properly and to let the Cache automatically
discard older counters ...

Something like :

    @Before
    static void countUsers() {
        // Track 2 counters of 1mn each
        Integer lastMinute = (int)System.currentTimeMillis() / 60000;

        // Counter keys
        String counter1Key = lastMinute + "";
        String counter2Key = (lastMinute-1) + "";

        // Intialize counters
        if(Cache.get(counter1Key) == null) Cache.add(counter1Key, 0L,
"2mn");
        if(Cache.get(counter2Key) == null) Cache.add(counter2Key, 0L,
"2mn");

        // Check if this user is already count
        if(!session.contains("lastCount") ||
Integer.parseInt(session.get("lastCount")) < lastMinute - 1) {
            Cache.incr(counter1Key);
            session.put("lastCount", lastMinute);
        }

        // Now count users
        Long counter1 = Cache.get(counter1Key, Long.class);
        Long counter2 = Cache.get(counter2Key, Long.class);

        // Save the result
        renderArgs.put("nbUsers", counter1 + counter2);
    }

In this way I count all users who have made a request during the last 2
minutes ...

分享到:
评论

相关推荐

    Handbook of Research on Soft Computing and Nature-Inspired Algorithms

    The Handbook of Research on Soft Computing and Nature-Inspired Algorithms is an essential source for the latest scholarly research on applications of nature-inspired computing and soft computational ...

    王小平版遗传算法的光盘源代码

    that population, else if `none is specified on the command line, the parameters will retain the default values. Remember you must specify either a population params file or `none on the command line...

    MFC+Windows程序设计

    Later this year, I plan to post a brand new chapter on MFC DLLs that you can read and comment on online. With the huge volume of computer books vying for buyers' attention in bookstores today, I ...

    VC技术内幕第五版.chm

    Later this year, I plan to post a brand new chapter on MFC DLLs that you can read and comment on online. With the huge volume of computer books vying for buyers' attention in bookstores today, I ...

    vs 2005 10款插件

    The goal of GhostDoc is to automate the tedious parts of writing XML comments by looking at the name of your class or method, as well as any parameters, and making an educated guess as to how the ...

    XML Processing with Perl, Python, and PHP (2002).pdf

    If I want to view the information online, can I con- vert it easily? If I build an application that provides me with access to the information, how do I go about storing my preferences? How do I make...

    csapp-shell-labcsapp-shell-labcsapp-shell-lab

    Your task is to complete the remaining empty functions listed below, along with an estimate of the approximate number of lines of code for each function based on the reference solution: 1. **eval**:...

    CE中文版-启点CE过NP中文.exe

    Compare memory: Added a limit to the number of address values shown per row (can be changed) Address List: When the option to deactivate children is set, the children will get deactivated first Memory...

    VB编程资源大全(英文源码 控件)

    It mimics the Microsoft Outlook's Vertical Menu bar which appears on the left of the screen&lt;END&gt;&lt;br&gt;35 , progbar.zip This is a great replacement for the standard VB progress bar. Its smooth, ...

    VB编程资源大全(英文源码 网络)

    Online Weather, News headlines, Online Horoscopes, Movie Reviews etc.&lt;END&gt;&lt;br&gt;44 , TelDialOut1.zip TelDialOut is a program that dials a phone number from an application using the modem. I had ...

    ethminer-0.16.0.dev3

    For Maxwell 2 GPUs: There is a way of mining ETH at Win7/8/Linux speeds on Win10, by downgrading the GPU driver to a Win7 one (350.12 recommended) and using a build that was created using CUDA 6.5. ...

    F:\课程\英语\英语六级\四、六级作文35个加分句型.doc

    例句:On the one hand, online shopping offers convenience, but on the other hand, it raises concerns about privacy and security. 一方面,网上购物提供了便利,但另一方面,它引发了关于隐私和安全的担忧。 ...

    VB编程资源大全(英文源码 其它)

    prngmit.zip This Random Number Generator is for everybody who is tired of using the "Randomize" and "Rnd" in VB and VBScript and having to watch the SAME "Random" numbers appear (The problem is ...

    apktool documentation

    The number that the framework is named during install corresponds to the pkgId of the application. These values should range from 1 to 9. Any APK that installs itself as 127 is 0x7F which is an ...

    uhdd.sys源码

    It uses only 656 to 752 bytes of upper memory depending on the system and it can also load in 640K DOS memory RDISK is a simple and small RAMdisk driver for use when resizing or other features are...

    flash action script 经典字典教程大全,学flash必备

    indexOf String.indexOf() Infinity Infinity -Infinity -Infinity INSERT Key.INSERT insertBefore XML.insertBefore() install CustomActions.install() instanceof instanceof int int interface...

    Im not an OperatorPPT教案.pptx

    - `The line is bad/busy.`:线路不好/占线。 - `Sorry, I can’t hear you.`:对不起,我听不到你说话。 3. 重点句式: - `As soon as`引导的时间状语从句,表示一…就…。 以上就是PPT教案中的主要内容,这些...

    Making Games with Python & PyGame.pdf(with code)

    Checking Your Code Online ........................................................................................................ 6 More Info Links on ...

    Flash脚本代码大全

    16. **New Line(换行)** - 功能:插入换行符。 - 示例:`\n` 17. **Number(数字类型)** - 功能:转换为数字类型。 - 示例:`Number("123");` 18. **Parse Float(解析浮点数)** - 功能:将字符串转换为...

Global site tag (gtag.js) - Google Analytics