Welcome! Log In Create A New Profile

Advanced

Problem with php memcached

Posted by hedbanger83 
Problem with php memcached
May 02, 2012 01:55PM
Hi,

I have nginx installed working with memcached and php fastcgi.

I can get the php script to add something to the cache just fine, however, depending on how long the string is that I am caching, memcache_set seems to be serializing the string. When I open the page in a browser and it takes the page from memcached, its all messed up looking. It looks like if I am viewing a binary file as text. Again this only happens if the string if very long. The string I am testing with is 12KB in length. However, if I split the string in half, roughly 6KB it works perfectly fine..

I have attached a picture of the the strange output...


here is part of my nginx.conf
------------------------------------------------------------------------------------------------------------------
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
index index.html index.php;
}

location ~* \.php$ {
default_type text/html;
set $memcached_key "index.php";
memcached_pass 127.0.0.1:11211;
error_page 404 405 502 @cache_miss;
}

location @cache_miss {
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}

}
------------------------------------------------------------------------------------------------------------


And the php file...(index.php)
-------------------------------------------------------------------------------------------------------------

$pinfo = "12KB LONG STRING";

$memcache_obj = memcache_connect('127.0.0.1', 11211);

if(!$var = memcache_get($memcache_obj, 'index.php')){
memcache_set($memcache_obj, 'index.php', $pinfo, FALSE, 30);
echo $memcache_obj->get('index.php');
}
--------------------------------------------------------------------------------------------------------



The "echo" in the php file, which will echo the cached data immediately after storing it, works perfect. If I open the page in a browser for the first time (before that string is added the memcache) it displays the VERY LONG STRING perfectly.

However, If I refresh the brower within 30 seconds, the string (coming from memcached) comes out all screwy looking (looks like binary data represented as text)


This is driving me absolutely crazy, anyone have any tips on what I might be doing wrong?

Thanks
Attachments:
open | download - weird_output.jpg (146.1 KB)
Re: Problem with php memcached
May 02, 2012 02:11PM
I figured it out.

I added:

ini_set('memcache.compress_threshold',100000);

to the top of the script which adds data to memcached.

Apparently the default is 20000, and my string was larger than that. I guess it compresses data larger than this amount.

After changing, I tested and works perfectly.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 137
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready