Example #1
0
 /**
  * 保存一个配置,支持批量设置
  *
  * @param string/array $key 关键字
  * @param mixed $value 值
  * @param string $type 类型,长度32以内
  * @param boolean $auto_clear_cache 自动清除缓存
  * @return boolean
  */
 public function set($key, $value, $type = '', $auto_clear_cache = true)
 {
     $db = new Database($this->database);
     $type = (string) $type;
     try {
         if (is_array($key)) {
             # 批量设置
             $tr = $db->transaction();
             $tr->start();
             try {
                 # 先尝试删除旧数据
                 $db->where('type', $type)->and_where_open();
                 foreach ($key as $k) {
                     $db->or_where('key_md5', md5($k));
                 }
                 $db->and_where_close()->delete($this->tablename);
                 # 设置数据
                 foreach ($key as $i => $k) {
                     $data = array('type' => $type, 'key_md5' => md5($k), 'key_name' => $k, 'value' => $this->data_format($value[$i]));
                     $db->values($data);
                 }
                 $db->columns(array('type', 'key_md5', 'key_name', 'value'));
                 $db->insert($this->tablename);
                 $tr->commit();
                 if (is_array($this->config)) {
                     foreach ($key as $i => $k) {
                         $this->config[$type][$k] = $value[$i];
                     }
                 }
                 if ($auto_clear_cache) {
                     $this->clear_cache($type);
                 }
                 return true;
             } catch (Exception $e) {
                 $tr->rollback();
                 return false;
             }
         } else {
             $data = array('type' => $type, 'key_md5' => md5($key), 'key_name' => $key, 'value' => $this->data_format($value));
             $status = $db->replace($this->tablename, $data);
             $status = $status[1];
             if ($status) {
                 if (is_array($this->config)) {
                     $this->config[$type][$key] = $value;
                 }
                 if ($auto_clear_cache) {
                     $this->clear_cache($type);
                 }
                 return true;
             } else {
                 return false;
             }
         }
     } catch (Exception $e) {
         if (IS_DEBUG) {
             throw $e;
         }
         return false;
     }
 }
Example #2
0
 /**
  * 存数据
  *
  * @param string/array $key 支持多存
  * @param $data Value 多存时此项可空
  * @param $lifetime 有效期,默认3600,即1小时,0表示不限制
  * @return boolean
  */
 public function set($key, $value = null)
 {
     if (IS_DEBUG) {
         Core::debug()->info($key, 'database storage set key');
     }
     if (is_array($key)) {
         foreach ($key as $k => $v) {
             $k = $this->prefix . $k;
             $this->_format_data($value[$k]);
             $data = array(md5($k), $k, $value[$k]);
             $this->_handler->values($data);
         }
     } else {
         $key = $this->prefix . $key;
         $this->_format_data($value);
         $data = array(md5($key), $key, $value);
         $this->_handler->values($data);
     }
     $rs = $this->_handler->columns(array('key', 'key_string', 'value'))->replace($this->tablename);
     if ($rs[0]) {
         return true;
     } else {
         return false;
     }
 }
Example #3
0
 /**
  * 存数据
  *
  * @param string/array $key 支持多存
  * @param $data Value 多存时此项可空
  * @param $lifetime 有效期,默认3600,即1小时,0表示最大值30天(2592000)
  * @return boolean
  */
 public function set($key, $value = null, $lifetime = 3600)
 {
     if (!\is_array($key)) {
         $key = array($key => $value);
     }
     foreach ($key as $k => $item) {
         $data = array('key' => \md5($k), 'key_str' => $k, 'value' => $value, 'expire_time' => $lifetime > 0 ? \TIME + $lifetime : 0);
         $this->db->values($data);
     }
     try {
         $this->db->insert($this->tablename);
         return true;
     } catch (\Exception $e) {
         \Core::debug()->error($e->getMessage());
         return false;
     }
 }
Example #4
0
 /**
  * 存数据
  *
  * @param string/array $key 支持多存
  * @param $data Value 多存时此项可空
  * @param $lifetime 有效期,默认3600,即1小时,0表示最大值30天(2592000)
  * @return boolean
  */
 public function set($key, $value = null, $lifetime = 3600)
 {
     if (IS_DEBUG) {
         Core::debug()->info($key, 'database cache set key');
     }
     if ($lifetime > 0) {
         $lifetime += TIME;
     }
     if (is_array($key)) {
         foreach ($key as $k) {
             if (is_numeric($value[$k])) {
                 $data = array(md5($k), $k, '', $value[$k], $lifetime);
             } else {
                 if ($this->_compress) {
                     $value[$k] = gzcompress($value[$k], 9);
                 }
                 $data = array(md5($k), $k, serialize($value[$k]), null, $lifetime);
             }
             $this->_handler->values($data);
         }
     } else {
         if (is_numeric($value)) {
             # 对于数值型数据,存在number字段里
             $data = array(md5($key), $key, '', $value, $lifetime);
         } else {
             if ($this->_compress) {
                 $value = gzcompress($value, 9);
             }
             $data = array(md5($key), $key, serialize($value), null, $lifetime);
         }
         $this->_handler->values($data);
     }
     $rs = $this->_handler->columns(array('key', 'key_string', 'value', 'number', 'expire'))->replace($this->tablename);
     if ($rs[0]) {
         return true;
     } else {
         return false;
     }
 }
Example #5
0
 /**
  * 存数据
  *
  * @param string/array $key 支持多存
  * @param $data Value 多存时此项可空
  * @param $lifetime 有效期,默认3600,即1小时,0表示不限制
  * @return boolean
  */
 public function set($key, $value = null, $lifetime = 3600)
 {
     if (IS_DEBUG) {
         Core::debug()->info($key, 'database cache set key');
     }
     if ($lifetime > 0) {
         $lifetime += TIME;
     }
     if (is_array($key)) {
         foreach ($key as $k => $v) {
             $k = $this->prefix . $k;
             if (is_numeric($v)) {
                 $data = array(md5($k), $k, '', $v, $lifetime);
             } else {
                 $this->_format_data($value[$k]);
                 $data = array(md5($k), $k, $value[$k], 0, $lifetime);
             }
             $this->_handler->values($data);
         }
     } else {
         $key = $this->prefix . $key;
         if (is_numeric($value)) {
             # 对于数值型数据,存在number字段里
             $data = array(md5($key), $key, '', $value, $lifetime);
         } else {
             $this->_format_data($value);
             $data = array(md5($key), $key, $value, 0, $lifetime);
         }
         $this->_handler->values($data);
     }
     $rs = $this->_handler->columns(array('key', 'key_string', 'value', 'number', 'expire'))->replace($this->tablename);
     if ($rs[0]) {
         return true;
     } else {
         return false;
     }
 }