tags:

views:

157

answers:

6

Hi

I'm trying to prevent caching by appending a '? t=' to the end of my JS files. What's the fastest way to get such a number? time() or rand() or something else?

+3  A: 

Try:

'?t=' . mt_rand(time());
Sarfraz
Why not just `time()`? Choosing a random number between 1 and time makes it slower... I don't think he wanted to combine them, he just wants the least impact choice.
animuson
^^what he said.
Thwyrm
@animuson: Yes, good point, updated `mt_rand` is better choice in this case.
Sarfraz
A: 
srand(time());
echo '?t=' . rand(); 
nathan
This isn't any more random than time(), as it wil provide the same semi random number for 1 second at the time.
Ivar Bonsaksen
+5  A: 

If you're only preventing caching, time() would be sufficient.

Ivar Bonsaksen
Is that more efficient than rand or mt_rand?
Thwyrm
Without any testing or spesification to back up my claim, I dare say yes.
Ivar Bonsaksen
I don't really know how the time function works to figure out the number of seconds, but I'd assume it'd be faster than running a random number algorithm, probably not by much though. It does, however, guarantee that you'll never get a repeat result which is possible using a random number function.
animuson
+2  A: 

Don't use rand(), use mt_rand().

It uses a random number generator with known characteristics using the Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.

nickf
It's not silly. You are
Thwyrm
could you guys quit bullying around? the question is not stupid, nor is this answer. grow up
seanizer
+3  A: 

time() and mt_rand() are pretty similar in terms of efficiency in PHP—you select one or the other based on what factors you need it for:

  • Just hard to guess: use mt_rand() (e.g., generating use salt)
  • Get a unique identifier that is hard to guess: use mt_rand(1, 931415926536); (e.g., generating session id)
  • (obviously) keep records: use time() (e.g., prevent caching, logs et cetera)

If you really want to know, time() is slightly faster—but you really don't need to worry about it. (It's the difference between one or two small parts of a second.)

(mt_rand() is about 4 times as fast as rand())

You probably know this already, but be sure to always profile your code before making optimizations; often it'll run slowly for reasons completely different than what you expected.

aharon
time() is slightly faster than? Rand or mtrand?
Thwyrm
time() is slightly faster than both. Plus, for cache control there is a small possibility that you'll generate the same random number twice; this is not the case (unless your machine is very fast and you have multiple requests per second) for time().
aharon
Thanks :) 15char
Thwyrm
Using `mt_rand(1, time())` makes no sense. All that you're doing with that is adding an extra function call and reducing the range of possible numbers (making it more likely for a collision).
nickf
@nickf, good point.
aharon
just call `mt_rand()` without parameters
nickf
+3  A: 

Call me old-fashioned, but preventing caching is something that can and should be achieved by using HTTP headers, not unique URLs. If you serve the file dynamically through PHP:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

otherwise use a .htaccess file in apache (or similar config in any other web server):

<FilesMatch "\.js$">
Header set Cache-Control "no-cache, must-revalidate"
Header set Expires "Sat, 26 Jul 1997 05:00:00 GMT"
</FilesMatch>
seanizer