java save information php read and write memcache garbled problem_PHP tutorial

WBOY
Release: 2016-07-13 10:57:45
Original
1068 people have browsed it

We use java to write the data and then use php to read it out. However, when we use php to read it out, we find that the problem of garbled characters in memcache occurs. Now I will introduce to you the solution to the garbled characters in memcache.

java reads php and writes memcache content

Story background: I used java to insert a digital record, but it could not be read correctly using php.

Today I discovered that if you use MemCachedClient to insert a number or a Java object, it will be garbled when called out in PHP.

The object can be understood, but simple numbers will be garbled

as

 代码如下 复制代码
mcc.set("a",25);

The output in php is garbled, and if you use telnet to query
get a

A question mark comes out.

After flipping through it, I said I wanted to change the category.

Finally, I found a problem. If you use strings instead, then java and php can be used interchangeably.

The code is as follows Copy code
mcc.set("a","25 ″);
 代码如下 复制代码
mcc.set("a","25″);

And telnet 192.168.0.1 11211 can get the correct number.

Originally, I wanted to add the value a, and thought it would be more convenient to use int, but it actually conflicted with PHP's reading of memcache. Let's do variable type conversion back and forth.

If you are also using java to write to memcache, and php to read out of memcache, using the same string storage can avoid many problems.

Class packages that are not Java will not work.


java reads php and writes memcache content

Current requirements: PHP writes the key as "my_key" and the value as "my_value" into memcache, and then uses java to connect to memcache to read the value of my_key, my_value.

The code on the PHP side is:

The code is as follows Copy code
 代码如下 复制代码

$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();
?>

                    $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(); ?>

After testing, after writing in php, it can be obtained very well using php and the memcache command line.

However, when using the following java code to get it, null is obtained

public class CacheReader {
The code is as follows
 代码如下 复制代码

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");
}
}

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();
   }
}
}

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
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 a problem with my encoding or some parameter settings. Later, I thought maybe the addressing methods of various memcache clients were different. After many twists and turns, this problem was solved indirectly: I changed 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();
}
}
}
http://www.bkjia.com/PHPjc/632069.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632069.htmlTechArticleWe use java to write the data and then use php to read it out, but when we use php to read it out, we find that there is a problem in memcache The garbled code problem has arisen. Let me introduce to you the solution to the memcache garbled code. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template