ArticleDirectory
- Serialization Problems
- Auto-increment Problem
Tokyo tyrant (ttserver) series-memcache Protocol
[Author: Sun Li link:Http://www.cnblogs.com/sunli/Updated on:]
Part 1[Tokyo tyrant (ttserver) series-startup parameters and configuration]
Use ttserver via memcache protocol to connect to the instance we started in Section 1 through Telnet 127.0.0.1 1978 Telnet. Add the data whose key is key1 and whose value is value1 through add. Get the data through get key1. If you cannot understand it, you can search for the memcache protocol.
Add key1 1 0 6value1storedget key1value key1 0 6value1end |
Use PHP
$ Mem = New Memcache (); $ Mem-> Connect ( "192.168.15.178" , 1978 ); $ Mem-> Add ( "Key2" , "Value2" ); Print_r ($ mem-> Get ( "Key2" )); Echo "<HR>" ; $ Mem-> Add ( "Key3" , Array ( "Value3" => "This is value3" )); Print_r ($ mem-> Get ( "Key3" )); ?> |
Output after running:
Value2 A: 1: {s: 6: "value3"; s: 14: "This is value3 ";} |
Serialization issues that need attention
If you are familiar with the memcache protocol or you have used the memcache of PHP to use ttserver, you may immediately find the problem above.
For example, if key3 is an array, but we get a serialized string without automatic deserialization, it will be automatically deserialized on the memcached server.
The above Telnet example shows that when we add key1, the flag parameter is set to 1, but when we get back, the returned flag parameter is 0. In fact, ttserver does not store the flag parameter and uses 0 in a unified manner. This causes PHP to not automatically deserialize the parameter. Of course, if you use the compressed parameter, the same problem may occur.
How to solve this problem, if you want to modify Code It is inconvenient. We can control it in PHP or on our client. For example, values are stored after they are serialized in a unified manner, and deserialization is performed when values are obtained. Auto-increment Problem
// use ttserver auto-increment $ mem = New memcache (); $ mem-> connect ( " 192.168.15.178 " , 1978 ); var_dump ($ mem-> increment ( " incr " ); // The result is int (1) ?> |
// Use memcache auto-Increment $ Mem =NewMemcache (); $ Mem->Connect("192.168.15.178",11211); Var_dump ($ mem->Increment("Incr")); // The result is bool (false) ?> |
We can see that the same code is used in memcache to return a failure (false). We can see this sentence in the PHP Manual:"
Memcache: increment ()
Does not Create an item if
It didn't exist. "But the same is true for ttserver. Pay special attention to this.