get the number of online by Play

HTTP在线用户计数
本文介绍了一种利用缓存机制来统计在过去两分钟内活跃用户的解决方案。通过跟踪两个一分钟的计数器,并在每次用户请求时更新这些计数器,可以有效避免状态保持问题,并自动清除过时的数据。

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 ...

Table Tennis Score 30 Author 陈越 Organization 浙江大学 A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours. Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day. One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players. Input Specification: Each input file contains one test case. For each case, the first line contains an integer N (≤10000) - the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS - the arriving time, P - the playing time in minutes of a pair of players, and tag - which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players' info, there are 2 positive integers: K (≤100) - the number of tables, and M (< K) - the number of VIP tables. The last line contains M table numbers. Output Specification: For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed. Sample Input: 10 20:52:00 10 0 08:00:00 20 0 08:02:00 30 0 20:51:00 10 0 08:10:00 30 0 08:12:00 10 1 20:40:00 13 0 08:01:30 15 1 20:53:00 10 1 20:54:00 10 0 3 1 2 Sample Output: 08:00:00 08:00:00 0 08:01:30 08:01:30 0 08:02:00 08:02:00 0 08:12:00 08:16:30 5 08:10:00 08:20:00 10 20:40:00 20:40:00 0 20:51:00 20:51:00 0 20:52:00 20:52:00 0 20:53:00 20:53:00 0 4 3 2 C(gcc)
最新发布
10-26
I. Inés and her compitas time limit per test1 second memory limit per test1024 megabytes Inés regularly plays her favorite game: "Don't compete, make compitas". This game is played in rounds, and in each round, players must choose between two options: Option 1 : Share X units of gold among the players who choose this option (rounding down). That is, if k players choose this option, each receives ⌊Xk⌋ units of gold. Option 2 : Receive Y units of gold directly, without sharing. Inés will play with N other players in a game of M rounds. She knows in advance which option each of the other players will choose in each round. With this information, she wants to decide her strategy to maximize the gold she receives in each round. Your task is to help Inés make the best decision in each round to achieve her goal, and also, determine the total gold that each player will receive. In case both options in a round are equally convenient for Inés, she will choose option 1 (sharing), as she finds it the more fun option. Note: ⌊x⌋ is the largest integer that is less than or equal to x . For example, ⌊2.5⌋=2 , ⌊π⌋=3 , and ⌊5⌋=5 . Input A line with two integers N and M (1≤N,M≤100 ), which indicate respectively the number of players playing with Inés and the number of rounds. Each player playing with Inés is identified by a distinct integer between 1 and N . The following 2M lines describe the rounds, using two consecutive lines per round. The first line of each round contains two integers X and Y (1≤X,Y≤105 ), which indicate respectively the amount of gold for options 1 and 2 in that round. The second line of each round contains N integers A1,A2,…,AN (Ai=1 or Ai=2 ), indicating that player i chooses option Ai in that round. Output A single line with N+1 integers, representing the total gold received by each of the players (including Inés) if Inés decides her strategy as explained. The first N integers represent the gold of each of the N players (excluding Inés), ordered by player, while the last integer represents Inés's gold.
09-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值