/** * 関数を呼び出す * * 関数名と引数から決定されるキーに対応する値がKVSにキャッシュされていれば * それを返し、なければ関数を呼び出し、結果をKVSにキャッシュする。 * * @param callable $function * @param array $parameters * @return mixed * @throws InvalidArgumentException */ public function invoke($function, array $parameters = array()) { if (!is_callable($function)) { throw new InvalidArgumentException('Non-callable value was given'); } // 関数名 if (is_string($function)) { $name = $function; if (strpos($function, '::') !== false) { $function = explode('::', $function, 2); } } elseif (is_object($function)) { $name = get_class($function) . '->__invoke'; } elseif (is_object($function[0])) { $name = get_class($function[0]) . '->' . $function[1]; } else { $name = $function[0] . '::' . $function[1]; } // キー $key = strtolower($name) . '('; if ($n = count($parameters)) { $key .= $n . ':' . md5(serialize($parameters)); } else { $key .= 'void'; } $key .= ')'; // キャッシュを取得 $record = $this->_kvs->getRaw($key); if ($record && !$record->isExpired($this->_lifeTime)) { return $this->_kvs->getCodec()->decodeValue($record->value); } // なければ関数を実行 if ($n) { if ($n == 1 && !is_array($function)) { $value = $function(reset($parameters)); } else { $value = call_user_func_array($function, $parameters); } } elseif (is_array($function)) { $value = call_user_func($function); } else { $value = $function(); } // キャッシュに保存 if ($record) { $this->_kvs->update($key, $value); } else { $this->_kvs->set($key, $value); } return $value; }
/** * 書き込みデータの保存形式を変更する * * @param string $post_db_path * @return void */ function _100120_0700_convert_post_data_store($post_db_path) { if (!file_exists($post_db_path)) { return; } $oldKvs = P2KeyValueStore::getStore($post_db_path, P2KeyValueStore::CODEC_SERIALIZING); $newKvs = PostDataStore::getKVS(); foreach ($oldKvs as $key => $value) { $newKvs->set($key, $value); } if ($oldKvs->getTableName() != $newKvs->getTableName()) { $oldKvs->prepare('DROP TABLE $__table')->execute(); } // プリペアードステートメントを破棄するために optimize() $newKvs->optimize(); }
/** * データを保存するP2KeyValueStoreオブジェクトを取得する * * @param string $databasePath * @param string $codec * @param string $tableName * @return P2KeyValueStore */ protected static function _getKVS($databasePath, $codec = P2KeyValueStore::CODEC_SERIALIZING, $tableName = null) { global $_conf; $id = $codec . ':' . $databasePath; if (array_key_exists($id, self::$_kvs)) { return self::$_kvs[$id]; } if (!file_exists($databasePath) && !is_dir(dirname($databasePath))) { FileCtl::mkdirFor($databasePath); } try { $kvs = P2KeyValueStore::getStore($databasePath, $codec, $tableName); self::$_kvs[$id] = $kvs; } catch (Exception $e) { p2die(get_class($e) . ': ' . $e->getMessage()); } return $kvs; }
/** * cachedGetHostByAddr/cachedGetHostByName のキャッシュエンジン */ private static function _cachedGetHost($remote, $function) { global $_conf; $lifeTime = (int) $GLOBALS['_HOSTCHKCONF']['gethostby_lifetime']; if ($lifeTime <= 0) { return $function($remote); } if (!file_exists($_conf['hostcheck_db_path'])) { FileCtl::mkdirFor($_conf['hostcheck_db_path']); } $kvs = P2KeyValueStore::getStore($_conf['hostcheck_db_path']); $result = $kvs->get($remote, $lifeTime); if ($result !== null) { return $result; } $result = $function($remote); $kvs->set($remote, $result); return $result; }
break; // データベースを最適化する // データベースを最適化する case 'optimizeDB': // SQLite2 の画像キャッシュデータベースをVACUUM if ($db->dsn['phptype'] == 'sqlite') { $result = $db->query('VACUUM'); if (DB::isError($result)) { P2Util::pushInfoHtml($result->getMessage()); } else { P2Util::pushInfoHtml('<p>画像データベースを最適化しました。</p>'); } } // SQLite3 の一覧表示用データキャッシュをVACUUM,REINDX if ($viewer_cache_exists) { $kvs = P2KeyValueStore::getStore($_conf['iv2_cache_db_path'], P2KeyValueStore::CODEC_SERIALIZING); $kvs->optimize(); unset($kvs); P2Util::pushInfoHtml('<p>一覧表\示用のデータキャッシュを最適化しました。</p>'); } break; // 未定義のリクエスト // 未定義のリクエスト default: P2Util::pushInfoHtml('<p>未定義のリクエストです。</p>'); } if (isset($removed_files)) { $flexy->setData('removedFiles', $removed_files); } } // }}}
/** * コンストラクタ * * @param string $rootUri * @param string $loginId * @param string $loginPass * @param string $cookieSaveDir * @param bool $ignoreCookieAddr * @throws P2Exception */ public function __construct($rootUri, $loginId, $loginPass, $cookieSaveDir, $ignoreCookieAddr = false) { if (!preg_match('!^https?://.+/$!', $rootUri)) { throw new Exception('Invalid root URI was given.'); } $this->_rootUri = $rootUri; try { $cookieSavePath = $cookieSaveDir . DIRECTORY_SEPARATOR . self::COOKIE_STORE_NAME; $cookieStore = P2KeyValueStore::getStore($cookieSavePath, P2KeyValueStore::CODEC_SERIALIZING); } catch (Exception $e) { throw new P2Exception(get_class($e) . ': ' . $e->getMessage()); } if ($cookieManager = $cookieStore->get($loginId)) { if (!$cookieManager instanceof HTTP_Client_CookieManager) { $cookieStore->delete($loginId); throw new Exception('Cannot restore the cookie manager.'); } } else { $cookieManager = new HTTP_Client_CookieManager(); } $this->_loginId = $loginId; $this->_loginPass = $loginPass; $this->_cookieStore = $cookieStore; $this->_cookieManager = $cookieManager; $this->_ignoreCookieAddr = $ignoreCookieAddr; $defaultHeaders = array('User-Agent' => self::HTTP_USER_AGENT); $this->_httpClient = new HTTP_Client(null, $defaultHeaders, $cookieManager); }
/** * コンストラクタ * * @param P2KeyValueStore $kvs */ public function __construct(P2KeyValueStore $kvs) { $this->_kvs = $kvs; $this->_ids = $kvs->getIds(); $this->_currentId = false; }