zoukankan      html  css  js  c++  java
  • yii2 memcache 跨平台交互 键和值不一样

    1 首先在配置文件中加载

    webasicconfigweb.php
    ........
        'components' => [
            'request' => [
                // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
                'cookieValidationKey' => 'zhaoyang',
            ],
            'mecache' => [
                'class' => 'yiicachingMemCache',
                'useMemcached' =>0, //这里简单说明一下 0是memcache, 1是memcached 两个是php里不同的扩展
                'servers' => [
                    [
                        'host' => 'localhost',
                        'port' => 11211,
                    ]
                ],
            ],
            'user' => [
                'identityClass' => 'appmodelsUser',
                'enableAutoLogin' => true,
            ],
    ............
    

    2 调用memcache

     $mecache = Yii::$app->mecache; 
       $mecache->set("key","value", 60);
       $mecache->get("key");
    

    一般这么用是可以的,一点问题都没有

    但是我与java 那边的memcache交出了问题

    java 写的memcache,我拿不到

    php 写的memcache, java拿不到

    3 解决方法

    webasicvendoryiisoftyii2cachingCache.php
    //重写2个方法, 读写都调用这个方法
    
    public function myset($key, $value, $duration = 0, $dependency = null)
    {
        return $this->setValue($key, $value, $duration);
    }
    
    public function myget($key)
    {
        return $this->getValue($key);
    }
    

    下面解释为什么这么做, 简单来讲, 程序做了加密, 去掉加密的那一层, php java 都读写统一就行了

    4 分析

      ① $mecache = Yii::$app->mecache;

      ② asicvendoryiisoftyii2cachingMemCache.php 

      ③ class MemCache extends Cache  即成自cache    asicvendoryiisoftyii2cachingCache.php 

      ④ 查看原来yii2 的set 和 get 都做了加密处理

        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 && $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);
        }
    

      ⑤ 按照上面的方法添加两个没有加密的直接读写 的memcache  问题就解决了

  • 相关阅读:
    AD9 如何画4层pcb板
    在Altium Designer 2009下如何添加Logo图
    [置顶] 整数拆分 整合算法
    altium designer 中的top/bottom solder和top/bottom paste mask
    vs2012 与 win7 不兼容的问题
    poj1742 Coins
    poj3181 Dollar Dayz
    poj1065 Wooden Sticks
    poj1631 Bridging signals
    poj3666 Making the Grade
  • 原文地址:https://www.cnblogs.com/zhaoyang-1989/p/5406059.html
Copyright © 2011-2022 走看看