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 ...
分享到:
相关推荐
Beginning 3D Game Development with Unity ...use of standard assets to investigate topics rarely covered in online tutorials or books. All of the necessary art assets to complete the project are provided.
The other involving a TTP relies on the security and robustness of server, which required the TTP to be online anytime. 3 Paper Preparation A novel RFID security protocol An RFID security method ...
You will be going beyond the basic Unity functionality and use of standard assets to investigate topics rarely covered in online tutorials or books. All of the necessary art assets to complete the ...
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 ...
Turning on the same protective systems in people should, by 2056, create the first class of 100-year-olds who are as vigorous and productive as today’s people in their 60s” Aliens Colin Pillinger,...
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 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 ...
<br>11. A)The girls got on well with each other. B)It's understandable that girls don't get along. C)She was angry eith the other young stars. D)The girls lacked the courage to fight....
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...
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. ...
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...
Checking Your Code Online ........................................................................................................ 6 More Info Links on ...