Thread Data Sharing / Zaehlen der Abrufe: IPC::Shareable unter mod_perl handler?!? (11 answers)
Opened by Airport1 at 2005-07-22 19:28

pq
 2005-07-24 19:28
#31137 #31137
User since
2003-08-04
12209 Artikel
Admin1
[Homepage]
user image
es ist ja nicht so, als ob perl eine faq hätte. =)
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
       I still don't get locking.  I just want to increment the
      number in the file.  How can I do this?

      Didn't anyone ever tell you web-page hit counters were
      useless?  They don't count number of hits, they're a waste
      of time, and they serve only to stroke the writer's van-
      ity.  It's better to pick a random number; they're more
      realistic.

      Anyway, this is what you can do if you can't help your-
      self.

     use Fcntl qw(:DEFAULT :flock);
     sysopen(FH, "numfile", O_RDWR|O_CREAT)       or die "can't opennumfile: $!";
     flock(FH, LOCK_EX)                           or die "can't flock numfile: $!";
     $num = <FH> || 0;
     seek(FH, 0, 0)                               or die "can't rewind numfile: $!";
      truncate(FH, 0)                              or die "can't truncate numfile: $!";
     (print FH $num+1, "\n")                      or die "can't write numfile: $!";
     close FH                                     or die "can't close numfile: $!";

      Here's a much better web-page hit counter:

          $hits = int( (time() - 850_000_000) / rand(1_000) );


probier's erst mal aus.
wenn dir das zuviel wird, dann erstell dir eine globale variable, also pro apache-
prozess, und zähl da hoch. bei 100 schreibst du in die counterdatei (+100) und
setzt den counter im apache wieder zurück.
bei bedarf kannst du die 100 beliebig anpassen.\n\n

<!--EDIT|pq|1122218998-->
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread Data Sharing / Zaehlen der Abrufe: IPC::Shareable unter mod_perl handler?!?