示例#1
0
文件: mem.php 项目: laiello/hecart
 /**
  * hash数组,将数组中的某个字段值索引成数组的KEY
  *
  * @param array  $arr         需要HASH的数组
  * @param string $key         要定的数组字段名
  * @param array  $mem_type    需要HASH的数组
  * @param int    $expire      有效期以分钟为单位,为0时则永不过期只有当MEM服务器关闭才过期
  * @return array|mixed 返回数据
  */
 public function &mem_hash($arr, $key, $mem_type, $expire = 30)
 {
     if (empty($arr) || !is_array($arr)) {
         return $arr;
     }
     $mem_type = "{$mem_type}-{$key}";
     $res = $this->mem->get(__FUNCTION__, $mem_type);
     if (!empty($res)) {
         return $res;
     }
     /**
      * 将数据进行hash
      */
     $res = array();
     foreach ($arr as $v) {
         if (isset($v[$key])) {
             $res[$v[$key]] = $v;
         }
     }
     /**
      * 存储数据并返回数据
      */
     $this->mem->set(__FUNCTION__, $mem_type, $res, $expire);
     return $res;
 }
示例#2
0
 /**
  * 写入SESSION内容
  *
  * @param string $sid   会话唯一标识
  * @param string $sdata 会话内容
  * @return boolean
  */
 public function write($sid, $sdata)
 {
     /**
      * SESSION数据为空则清除先前数据
      */
     if (empty($sdata)) {
         $this->destroy($sid);
         return false;
     }
     /**
      * 以数据库方式来处理SESSION
      */
     if ($this->_type == 'db' || $this->_type == 'mdb') {
         $expires = time() + $this->_life_time;
         //SESSION的有效期
         $sql = "REPLACE INTO {$this->_opt} (sId, sData, sIp, sExpires) VALUES ('{$sid}', '{$sdata}', '{$this->_ip}', {$expires})";
         $this->_db->query($sql);
         return $this->_db->affected_rows() > 0 ? true : false;
     }
     /**
      * 以Memcache缓冲方式来处理SESSION
      */
     if ($this->_type == 'mem') {
         $expires = $this->_life_time / 60;
         //SESSION的有效期
         return $this->_mem->set('session', $sid, $sdata, $expires);
     }
     /**
      * 以文件系统的方式来处理SESSION
      */
     if ($this->_type == 'dir') {
         $sfile = "{$this->_path}/{$sid[0]}";
         wcore_fso::make_dir($sfile);
         //处理SESSION存储的路径
         $sfile = "{$sfile}/{$this->_prefix}-{$sid}";
     } else {
         $sfile = "{$this->_path}/{$this->_prefix}-{$sid}";
     }
     return file_put_contents($sfile, $sdata);
 }