Ejemplo n.º 1
0
 /**
  * 主要check流程
  *
  * @param string $key
  */
 public function check($key = NULL)
 {
     self::getkey($key);
     $ready = $this->cache->get($this->key);
     if ($ready == FALSE) {
         $this->cache->set($this->key, array(time()));
         return;
     }
     $ready_ = $ready;
     if (self::checkCountAndTime($ready_)) {
         $this->rest->error(REST_Code::STATUS_ERROR_Api_QUENCY_M);
     }
     unset($ready_);
     array_push($ready, time());
     $this->cache->set($this->key, $ready);
 }
Ejemplo n.º 2
0
 /**
  * 主要检测过程
  *
  * @param int $time
  */
 public function ckModified($time = self::DEFAULT_MODIFIED_TIME)
 {
     if (!$this->ifConfig) {
         self::config($time);
     }
     $method = $_SERVER['REQUEST_METHOD'];
     switch ($method) {
         case 'GET':
             $uri = $_SERVER['REQUEST_URI'];
             $params = 0;
             break;
         case 'POST':
             $uri = $_SERVER['REQUEST_URI'];
             $params = $_POST;
             break;
     }
     $key = self::setModifiedKey($uri, $params);
     $cache_time = $this->cache->get($key);
     if ($cache_time) {
         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
             $modified_time = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
             if ($cache_time <= $modified_time) {
                 self::mkHeader($cache_time);
                 header("HTTP/1.0 304 Not Modified");
                 die;
             }
         }
     } else {
         $now = time();
         $this->cache->set($key, $now, $this->modified_time);
     }
     self::mkHeader($now);
 }
Ejemplo n.º 3
0
 private function __construct($dbhost, $dbport, $username, $password, $dbname, $dbcharset, $cachesys, $cachetype, $cachehost, $cacheport)
 {
     try {
         $this->_dbh = new PDO('mysql:host=' . $dbhost . ';port=' . $dbport . ';dbname=' . $dbname, $username, $password);
         $this->_dbh->query('SET NAMES ' . $dbcharset);
     } catch (PDOException $e) {
         exit('<pre><b>Connection failed:</b> ' . $e->getMessage());
     }
     $this->_cache = DB_Cache::instance($cachehost, $cacheport, $cachetype, $cachesys);
     if (!$this->_cache) {
     }
 }
Ejemplo n.º 4
0
 /**
  * @static
  *
  * @param int    $cachehost
  * @param int    $cacheport
  * @param string $cacheType
  * @param string $cacheSys
  *
  * @return DB_Cache
  */
 public static function instance($cachehost = 0, $cacheport = 0, $cacheType = 'memcached', $cacheSys = '')
 {
     if (self::$self == NULL) {
         $cache_config = Yaf_Registry::get('config')->get('yaf')->get('cache');
         $_cache_system = $cache_config->system;
         $_cache_type = $cache_config->type;
         $_cache_host = $cache_config->host;
         $_cache_port = $cache_config->port;
         self::$self = new DB_Cache($_cache_system, $_cache_type, $_cache_host, $_cache_port);
     }
     return self::$self;
 }
Ejemplo n.º 5
0
 public function query($sql = false, $binds = false, $error = true)
 {
     if (!$sql) {
         DB::error('DBRQRY', 'LIBTIT', __CLASS__);
     }
     // check for an existing cached version of the sql and return it if available
     // we do this by getting the command from the sql (we search for the first space)
     // and we check if that command is one of our defined "read type" queries.
     $cmd = substr($sql = trim($sql), 0, strpos($sql, ' '));
     if ($this->cache == true && $this->_is_read($cmd) && ($cache = DB_Cache::read($sql)) !== false) {
         return $cache;
     }
     // if the sql contains bindings, parse them.
     if ($binds !== false) {
         $sql = $this->_bind($sql, $binds);
     }
     // we run the query
     if (($this->RS = $this->_query($sql, $error)) === false) {
         return false;
     }
     // if enabled, save the query for later debugging.
     if ($this->query_save === true) {
         $this->query[] = $sql;
         ++$this->queries;
     }
     // if the query was a write type we return the result_write object;
     if ($this->_is_write($cmd)) {
         // and since we're making changes to the database content,
         // we need to clean our cache. only if the option is enabled ofcourse.
         if ($this->cache === true && $this->cache_clean == true) {
             DB_Cache::delete();
         }
         $driver = 'DB_' . $this->driver . '_Result_Write';
         return new $driver($this->RS);
     }
     // load the result driver
     $driver = 'DB_' . $this->driver . '_Result_Read';
     $RES = new $driver($this->RS);
     // if query cached is enabled, we have to instantiate only the result
     // library (without the platform specific driver).
     // we do this, because we can't serialize the mysql resource variable,
     // but since we already instantiated the result class, we can assign the
     // required values by hand.
     if ($this->cache === true) {
         $CRS = new DB_Result_Read();
         $CRS->object = $RES->result();
         $CRS->array = $RES->result(true);
         $CRS->rows = $RES->rows();
         // propagate current results.
         DB_Cache::write($sql, $CRS);
     }
     return $RES;
 }