PHP writes memcached and c reads garbled characters

Source: Internet
Author: User

When using PHP to encrypt memcached and using C to retrieve data from memcached, the retrieved value will be the same. The useful PHP libraries found on Baidu are, I used Java to extract the keyword of zookeeper. I can see an article about the solution:

In practice, a problem occurs: The value retrieved from memcached is always invalid. After a large number of threads, I found a rule: the plane size is less than 64 KB, And the retrieved value does not have a problem. Once this value is exceeded, it becomes invalid. 64 K is a very sensitive size. However, the maximum size of a single memcached storage value is 1 MB. 64 KB is small. As a result, I directly used the memcached server command to store the KB value to memcached. It is clear that the problem is on the client side.

(Original) I checked the memcached client for Java. One method is setcompressenable (Boolean compressenable ), this parameter indicates the size of the response (set by the setcompressthreshold (long compressthreshold) method. So I set MCC. setcompressenable (false); reset again, succeeded !! Haha ..

Read the aboveArticleThen I tried again. It is true that the set value in PHP won't be stored in a certain element group.ProgramWill cancel the operation.

 

The generation of memcached data written in PHP is as follows:

$ Servers = array (
Array ('ip1, 11211, 1 ),
Array ('ip2', 11222, 1 ),
);
$ MC = NULL;

Function initmemd ()
{
Global $ MC;
Global $ servers;
If (count ($ servers) = 0) return false;

$ MC = new memcached ();
If (false ===$ Mc-> addservers ($ servers ))
{
Print_r ("add mem servers error! ");
Return false;
}
$ Ret = $ Mc-> setoption (memcached: opt_compression, false );
If ($ ret = false)
{
Print_r ("set memcached compression option False Error! ");
Return false;
}
Return true;

}

 

Function pushtomemd ($ items)
{
Global $ MC;
$ Mc-> setmulti ($ items );
If ($ Mc-> getresultcode () = memcached: res_notstored ){
Print_r ("set memcached error ");
}
}

The program using C producer is as follows:

# include "stdio. H "
# include" stdlib. H "
# include" string. H "
# include" libmemcached/memcached. H "
// GCC 1.C-I/usr/local/libmemcached/include-L/usr/local/libmemcached/lib/-L/usr/local/lib-lmemcached
int main (INT argc, char * argv [])
{< br> memcached_st * MEMC;
memcached_return RC;
memcached_server_st * servers;


// Connect multi Server
MEMC = memcached_create (null );
Servers = memcached_server_list_append (null, "IP1", 11211, & rc );
// Servers = memcached_server_list_append (servers, "ip2", 11211, & rc );

Rc = memcached_server_push (MEMC, servers );
Memcached_server_free (servers );

// Save Multi Data
Size_t I;
Char * keys [] = {"key1", "key2", "key3 "};
Size_t key_length [] = {4, 4, 4 };
Char * Values [] = {"value1", "value2", "value3 "};
Size_t val_length [] = {6, 6, 6 };
For (I = 0; I <1; I ++)
{
Rc = memcached_set (MEMC, keys [I], key_length [I], values [I], val_length [I], (time_t) 180, (uint32_t) 0 );
If (rc = memcached_success)
{
Printf ("save key: % s data: % s success \ n", keys [I], values [I]);
}
}

Char return_key [10000];

size_t return_key_length;
char * return_value;
size_t return_value_length;
uint32_t flags;
rc = memcached_mget (MEMC, keys, key_length, 1 );
while (return_value = memcached_fetch (MEMC, return_key, & return_key_length, & return_value_length, & flags, & rc) {
If (rc = memcached_success)
{< br> printf ("Fetch key: % s data: % s, Len = % d \ n", return_key, return_value, return_value_length );
}< BR >}< br>

// Delete Multi Data
For (I = 0; I <3; I ++)
{
Rc = memcached_set (MEMC, keys [I], key_length [I], values [I], val_length [I], (time_t) 180, (uint32_t) 0 );
Rc = memcached_delete (MEMC, keys [I], key_length [I], (time_t) 0 );
If (rc = memcached_success)
{
Printf ("Delete % s successn", keys [I], values [I]);
}
}
// Free
Memcached_free (MEMC );
Return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.