memcached problems...

JG nginx-forum at nginx.us
Thu Sep 3 01:37:38 MSD 2009


hello again,

ok, i feed memcached now with this perl script....

--------- code ----------

#!/usr/bin/perl
# Usual suspects
use strict;
use warnings;
# Additional modules needed
use File::Find;
use Cache::Memcached;

### Create Memcache connection
my $cache = new Cache::Memcached {
        'servers' => [
                'localhost:11211',
        ],
        'compress_threshold' => 10_000,
} or die("Failed to connect to memcache server");

my @content;

### Define root location to search, alter as appropriate
#my $dir = "/home/www/htdocs/";
my $dir="$ARGV[0]" if $ARGV[0];

#$dir.="/$ARGV[0]" if $ARGV[0];
#print "$dir\n";
### Go find those files
find(\&Wanted, $dir);

### Process the found files
foreach my $file(@content){
        open (my $source,"<$file");
        read $source,my $contents,(stat($file))[7];
        $file =~ s/^$dir//;
        if ($cache->get($file)){
                my $compare = $cache->get($file);
                if ($compare ne $contents){
                        print "Cached file $file is not up to date, updating cache";
                        $cache->delete($file);
                        $cache->set($file,$contents);
                }
        } else {
                $cache->set($file,$contents)
        }
}

sub Wanted
{
        # only operate on image files
        /.jpg$/ or /.gif$/ or /.png$/ or return;
                push (@content,$File::Find::name);
}

#!/usr/bin/perl
# Usual suspects
use strict;
use warnings;
# Additional modules needed
use File::Find;
use Cache::Memcached;

### Create Memcache connection
my $cache = new Cache::Memcached {
        'servers' => [
                'localhost:11211',
        ],
        'compress_threshold' => 10_000,
} or die("Failed to connect to memcache server");

my @content;

### Define root location to search, alter as appropriate
#my $dir = "/home/www/htdocs/";
my $dir="$ARGV[0]" if $ARGV[0];

#$dir.="/$ARGV[0]" if $ARGV[0];
#print "$dir\n";
### Go find those files
find(\&Wanted, $dir);

### Process the found files
foreach my $file(@content){
        open (my $source,"<$file");
        read $source,my $contents,(stat($file))[7];
        $file =~ s/^$dir//;
        if ($cache->get($file)){
                my $compare = $cache->get($file);
                if ($compare ne $contents){
                        print "Cached file $file is not up to date, updating cache";
                        $cache->delete($file);
                        $cache->set($file,$contents);
                }
        } else {
                $cache->set($file,$contents)
        }
}

sub Wanted
{
        # only operate on image files
        /.jpg$/ or /.gif$/ or /.png$/ or return;
                push (@content,$File::Find::name);
}

----------------------------------

to push jpg gif etc into memcached which works as far is i can see, but nginx still doesnt try to fetch stuff from memcached

heres the part of my config 

--------- config --------------


        location / {
            root   /home/www/htdocs/;
            index  index.html index.htm;

        }

        location ~* \.(jpg|png|gif)$ {
            expires     max;
            set $memcached_key $uri;
            memcached_pass     127.0.0.1:11211;
            error_page 404 502 504 @fetch;
        }

        location ~ \.php$ {
            root           /home/www/htdocs;
            set $memcached_key $uri;
            memcached_pass     127.0.0.1:11211;
            proxy_intercept_errors  on;
            error_page 404 502 504 @fetch;

            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/www/htdocs$fastcgi_script_name;
            include        fastcgi_params;
        }

        location @fetch {
            internal;
            expires max;

            proxy_pass http://backend;
            break;
        }

-------- /code --------------

for my understanding nginx should first check if the file is in memcached, and if it gets a 404 there it should went to @fetch which is a 2nd definition looking like this

------- config ------------

upstream backend {
    server 127.0.0.1:82;
    }

   server {
        listen       127.0.0.1:82;
        server_name  localhost;


        location / {
            root   /home/www/htdocs/;
            index  index.html index.htm;
        }

        location ~ \.php$ {
            root           /home/www/htdocs;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/www/htdocs$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
-------------------------------

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,5434,5473#msg-5473






More information about the nginx mailing list