I wanted to add a live picture rotation to my site and i could not find any other way to do it so i decided that i need a cgi script that will: 1. Delete the first picture in the rotation (e.g. pic1.jpg) 2. Rename the rest of the pictures (e.g. rename pic2.开发者_Go百科jpg to pic1.jpg, pic3.jpg to pic2.jpg, pic4.jpg to pic3.jpg, etc...) 3. Do this every 5 minutes so that the viewers of my site will all be viewing the same picture pretty much at the sime time. Any help will be much appreciated, Thanks.
To make this work "so that the viewers of my site will all be viewing the same picture pretty much at the same time", you need to use different urls for each image or make sure you are telling browsers and proxies not to cache the pictures...and not caching is a really bad idea; your viewers will not appreciate it, nor will your server.
Sounds like a pretty bad idea, frankly. File operations are relatively slow, and tends to step on itself in a highly-concurrent application like a webapp.
Look for another way. How about using the current time as a key to choose among pictures?
currentImageIndex = currentTimeRoundedToTheNearestFiveMinutes %
totalNumberOfImages
Edited with more detail on request:
Basically, take the current time, and round it to the nearest five minutes. Doing something like currentHour / 12
, using integer math, will give you this; otherwise, truncate the result. Then use the modulo operator (%
in Perl, and many languages - handy operator that newcomers tend to overlook) to produce a number from 0
to n-1
, where n
is the total number of images you're serving. Then you can refer to a mapping table to go from that index to a filename.
Since you say in a comment that you don't need caching, rather than change the filenames of each file, why not have your page point to a file which is a symlink and then change the pointer of the symlink every few minutes. Seems like it would do what you need without the overhead of major file operations.
精彩评论