Java storage information php read/write memcache garbled

Source: Internet
Author: User

Java reads php and writes memcache content

Background: I inserted a digital record in java and could not be correctly read using php.

Today, we found that if MemCachedClient is used to insert a number or java object, it is garbled in php.

The object is understandable, and simple numbers are garbled.

For example

The code is as follows: Copy code
Mcc. set ("a", 25 );

Php is garbled, and telnet is used for query.
Get

Question mark.

The class package needs to be changed.

Finally, I found a problem. If I use a string, java and php will become generic.

The code is as follows: Copy code
Mcc. set ("a", "25 & Prime ;);

And telnet 192.168.0.1 11211 to get the correct number.

Originally, I had to perform addition operations on the value of a. I thought it would be more convenient to use int, and there was a conflict with php when reading memcache. Convert the variable type back and forth.

If you are writing to memcache using java, php reads memcache, and a general string storage, you can avoid many problems.

Not a java class package.


Java reads php and writes memcache content

Current requirement: php writes key as "my_key" and value as "my_value" to memcache, and then connects to memcache using java to read the value my_value of my_key.

Php code:

The code is as follows: Copy code

<? Php
$ Mem = new Memcache;
$ Mem-> connect ("192.168.1.20", 27688 );
$ Mem-> set ("my_key", "my_value", FALSE, 86400 );
$ My_v = $ mem-> get ("my_key ");
Var_dump ($ my_v );
$ Mem-> close ();
?>

It has been detected that php can be used in both php and memcache command lines after being written.

However, it is obtained using the following java code, but null is obtained.

The code is as follows: Copy code

Import com. danga. MemCached. MemCachedClient;
Import com. danga. MemCached. SockIOPool;

Public class CacheReader {

Static private MemCachedClient client = new MemCachedClient ();
Static {
String [] servers = {"192.168.1.20: 27688 "};
SockIOPool pool = SockIOPool. getInstance ();
Pool. setServers (servers );
Pool. initialize ();
}
Public void get (String name ){
System. out. println (client. get (name ));
}
Public static void main (String [] args ){
New CacheReader (). get ("my_key ");
}
}

At first, I thought it was my encoding problem or some parameter settings problems. Later, I thought it was possible that the addressing methods of various memcache clients were not the same. After several twists and turns, this problem was indirectly solved: I replaced the memcache Client on the java side and used net. spy. memcached. MemcachedClient. The code is as follows:

The code is as follows: Copy code

Import java. io. IOException;
Import java.net. InetSocketAddress;

Import net. spy. memcached. MemcachedClient;

Public class SpyMem {
Public static void main (String [] args ){
Try {
MemcachedClient c = new MemcachedClient (new InetSocketAddress ("192.168.1.20", 27688 ));
System. out. println (c. get ("my_key "));
C. shutdown ();
} Catch (IOException e ){
E. printStackTrace ();
   }
}
}

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.