Hi,

Are there any Cache::* modules that are thread safe and can be used with
Threading?

Using Cache::Memory (even specifying the same NameSpace), two or more
threads can't access the same keys in the cache,

Using Cache::File, there are issues with the indexes (documented too under
Caveats,

Neither can be :shared, Invalid value for shared scalar

I'm basically just looking for a key->value type of way to share data
between the threads, but I would prefer not to use an external resource
such as memcached or redis.

Master thread basically uses Thread::Queues to enqueue work received via an
IO::Socket::INET, slaves process the work (while the client socket waits),
and then the master thread returns the result from the queued work back
over the TCP socket.

With IO::Select and IO::Sockets, this is the rundown of the client loop...

while (1) {
  foreach my $ClientConnection ($Select->can_read(1)) {
    if ($ClientConnection == $ServerSocket) {
      $ClientConnection = $ServerSocket->accept();
      $Select->add($ClientConnection);
    } else {
      my $Data = $ClientConnection->getline();
      unless (defined($Data) && length($Data)) {
        $Select->remove($ClientConnection);
        $ClientConnection->shutdown(2);
        close $ClientConnection;
        next;
      }
      if ($RequestQueue->pending() > $MaxConnections*4) {
        $ClientConnection->send("400 Too busy\r\n");
        next;
      } else {
        $Data =~ s/[^ -~]//g;
        $RequestQueue->enqueue($Data);
        my $Processed = 0;
        my $TimeStamp = time();
        if (defined($Data) && length($Data)) {
          do {
            # $Cache->set is called in the worker threads
            my $Response = $Cache->get($Data);
            if ($Response && $Response ne "") {
              $ClientConnection->send($Response);
              $Cache->remove($Data);
              $Processed=1;
            }
          } until ($Processed==1 || time() - $TimeStamp >= 2);
        }
      }
    }
  }
}



-- 

Regards,
Chris Knipe

Reply via email to