1 First load in configuration file
web\basic\config\web.php ... ' components ' = = [ ' request ' = = [ //!!! Insert a secret key in the following (if it's empty)-This is Requi Red by Cookie validation ' cookievalidationkey ' = ' Zhaoyang ', ], ' mecache ' + = [ ' class ' = ' Yii\caching\memcache ', ' usememcached ' =>0, ' servers ' = = [ ' host ' = ' localhost ', ' Port ' = = 11211, ] ], ], ' user ' = [ ' identityclass ' = ' app\models\user ', ' Enableautologin ' = True, ],............
2 Calling Memcache
$mecache = Yii:: $app->mecache; $mecache->set ("Key", "value", "$"); $mecache->get ("key");
It's possible to use it in general, no problem at all.
But I memcache the problem with the Java side.
Java writes memcache, I can't get it.
PHP writes the memcache, Java can't get
3 workaround
Web\basic\vendor\yiisoft\yii2\caching\cache.php//overrides 2 methods, both read and write call this method public function MySet ($key, $value, $duration = 0, $ Dependency = null) { return $this->setvalue ($key, $value, $duration);} Public Function Myget ($key) { return $this->getvalue ($key);}
The following explains why, in short, the program is encrypted, remove the encryption layer, PHP Java read and write unified on the line
4 analysis
① $mecache = Yii:: $app->mecache;
②\basic\vendor\yiisoft\yii2\caching\memcache.php
③class MemCache extends Cache is a self-cache \basic\vendor\yiisoft\yii2\caching\cache.php
④ View the original Yii2 set and get are encrypted
Public function Get ($key) {$key = $this->buildkey ($key); $value = $this->getvalue ($key); if ($value = = = False | | $this->serializer = = = False) {return $value; } elseif ($this->serializer = = = = NULL) {$value = Unserialize ($value); } else {$value = Call_user_func ($this->serializer[1], $value); } if (Is_array ($value) &&! ( $value [1] instanceof Dependency && $value [1]->gethaschanged ($this)) {return $value [0]; } else {return false; }} Public function set ($key, $value, $duration = 0, $dependency = null) {if ($dependency!== null && Amp $this->serializer!== false) {$dependency->evaluatedependency ($this); } if ($this->serializer = = = null) {$value = serialize ([$value, $dependency]); } elseif ($this->serializer!== false) {$value = Call_user_func($this->serializer[0], [$value, $dependency]); } $key = $this->buildkey ($key); return $this->setvalue ($key, $value, $duration); }
⑤ the above method to add two non-encrypted direct read and write memcache problem solved
Yii2 Memcache Cross-platform interaction values cannot be unified