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 ...
分享到:
相关推荐
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...
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 ...
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 ...
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 ...
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...
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**:...
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...
It mimics the Microsoft Outlook's Vertical Menu bar which appears on the left of the screen<END><br>35 , progbar.zip This is a great replacement for the standard VB progress bar. Its smooth, ...
Online Weather, News headlines, Online Horoscopes, Movie Reviews etc.<END><br>44 , TelDialOut1.zip TelDialOut is a program that dials a phone number from an application using the modem. I had ...
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. ...
例句:On the one hand, online shopping offers convenience, but on the other hand, it raises concerns about privacy and security. 一方面,网上购物提供了便利,但另一方面,它引发了关于隐私和安全的担忧。 ...
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 ...
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 ...
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...
indexOf String.indexOf() Infinity Infinity -Infinity -Infinity INSERT Key.INSERT insertBefore XML.insertBefore() install CustomActions.install() instanceof instanceof int int interface...
- `The line is bad/busy.`:线路不好/占线。 - `Sorry, I can’t hear you.`:对不起,我听不到你说话。 3. 重点句式: - `As soon as`引导的时间状语从句,表示一…就…。 以上就是PPT教案中的主要内容,这些...
Checking Your Code Online ........................................................................................................ 6 More Info Links on ...
16. **New Line(换行)** - 功能:插入换行符。 - 示例:`\n` 17. **Number(数字类型)** - 功能:转换为数字类型。 - 示例:`Number("123");` 18. **Parse Float(解析浮点数)** - 功能:将字符串转换为...