2

I'm migrating from memcache to memcached and one of the differences I've noticed is that the compression flag seems to be set for all set() calls, not on an individual basis like with memcache.

Is this a per-connection setting, or can I toggle this on and off? If it's the latter, what would be the best way to go about setting the compression flag for each set() call?

Currently I'm thinking of doing something like this:

<?php
    if ($compress) $mc->setOption(Memcached::OPT_COMPRESSION,true);

    $return = $mc->set($key,$object,$expiration);

    if ($compress) $mc->setOption(Memcached::OPT_COMPRESSION,false);

Is this the right approach?

I'm also setting up $mc like this, if it makes any difference:

<?php
    $mc = new Memcached("cl");
    $mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE,true);
    $mc->setOption(Memcached::OPT_BINARY_PROTOCOL,true);

    if (count($mc->getServerList()) === 0) {
        $mc->addServers(array (
            array ($GLOBALS["mc"]["host"],$GLOBALS["mc"]["port"]),
        ));
    }

Thanks!

PureForm
  • 1,003
  • 8
  • 15
  • I'm interested in caching techniques. Why do you switch to memcached - what are the advantages. Just quick list or link would be great. thanks – NickSoft Apr 13 '11 at 18:16
  • @NickSoft, here's a good article on that :-) http://stackoverflow.com/questions/1442411/using-memcache-vs-memcached-with-php – PureForm Apr 14 '11 at 05:01

1 Answers1

2

Taking a look at the signature of the two methods and also reading the source of Memcached, I think it is not possible to set it for each set.

See php-memcached-dev on github, look for php_memcached.c

(What a stupid name for the client by the way. Memcached used to mean the server daemon, now it means server and client, you just can't google it any more.)

gphilip
  • 1,114
  • 15
  • 33
  • Good to know... Sucks since I store some already compressed binary data in there, as well as strings, ints etc. – PureForm Apr 14 '11 at 04:59
  • and Memcached doesn't handle them automagically? does it return raw compressed data? if so, amybe you can get them all and save them again with Memcached http://www.darkcoding.net/software/memcached-list-all-keys/ – gphilip Apr 20 '11 at 10:27
  • I don't follow. The data are png and jpeg images, so it seems pointless to me to compress them while set()ting them in MC :-) – PureForm Apr 21 '11 at 06:11