【问题标题】:Save very large strings in Realtime Database - Best Practice? [closed]在实时数据库中保存非常大的字符串 - 最佳实践? [关闭]
【发布时间】:2020-06-24 22:42:16
【问题描述】:

我正在使用 Angular 和 Firebase 实时数据库,我需要存储一些非常大的数据。例如,值必须是:

key: ""\"\\n // Meta-Box Generator\\n // How to use: $meta_value = get_post_meta( $post_id, $field_id, true );\\n // Example: get_post_meta( get_the_ID(), \\\"my_metabox_field\\\", true );\\n\\n class MeinecooleMetaboxMetabox {\\n\\n private $screens = array('qwdq');\\n\\n private $fields = array(\\n array(\\n 'label' => 'Your Label',\\n 'id' => 'text_yourlabel',\\n 'type' => 'text',\\n ) \\n );\\n\\n public function __construct() {\\n add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );\\n add_action( 'save_post', array( $this, 'save_fields' ) );\\n }\\n\\n public function add_meta_boxes() {\\n foreach ( $this->screens as $s ) {\\n add_meta_box(\\n 'MeinecooleMetabox',\\n __( 'Meine coole Metabox', 'textdomain' ),\\n array( $this, 'meta_box_callback' ),\\n $s,\\n 'normal',\\n 'default'\\n );\\n }\\n }\\n\\n public function meta_box_callback( $post ) {\\n wp_nonce_field( 'MeinecooleMetabox_data', 'MeinecooleMetabox_nonce' ); \\n $this->field_generator( $post );\\n }\\n\\n public function field_generator( $post ) {\\n $output = '';\\n foreach ( $this->fields as $field ) {\\n $label = '<label for=\\\"' . $field['id'] . '\\\">' . $field['label'] . '</label>';\\n $meta_value = get_post_meta( $post->ID, $field['id'], true );\\n if ( empty( $meta_value ) ) {\\n if ( isset( $field['default'] ) ) {\\n $meta_value = $field['default'];\\n }\\n }\\n switch ( $field['type'] ) {\\n default:\\n $input = sprintf(\\n '<input %s id=\\\"%s\\\" name=\\\"%s\\\" type=\\\"%s\\\" value=\\\"%s\\\">',\\n $field['type'] !== 'color' ? 'style=\\\"width: 100%\\\"' : '',\\n $field['id'],\\n $field['id'],\\n $field['type'],\\n $meta_value\\n );\\n }\\n $output .= $this->format_rows( $label, $input );\\n }\\n echo '<table class=\\\"form-table\\\"><tbody>' . $output . '</tbody></table>';\\n }\\n\\n public function format_rows( $label, $input ) {\\n return '<div style=\\\"margin-top: 10px;\\\"><strong>'.$label.'</strong></div><div>'.$input.'</div>';\\n }\\n\\n \\n\\n public function save_fields( $post_id ) {\\n if ( !isset( $_POST['MeinecooleMetabox_nonce'] ) ) {\\n return $post_id;\\n }\\n $nonce = $_POST['MeinecooleMetabox_nonce'];\\n if ( !wp_verify_nonce( $nonce, 'MeinecooleMetabox_data' ) ) {\\n return $post_id;\\n }\\n if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {\\n return $post_id;\\n }\\n foreach ( $this->fields as $field ) {\\n if ( isset( $_POST[ $field['id'] ] ) ) {\\n switch ( $field['type'] ) {\\n case 'email':\\n $_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );\\n break;\\n case 'text':\\n $_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );\\n break;\\n }\\n update_post_meta( $post_id, $field['id'], $_POST[ $field['id'] ] );\\n } else if ( $field['type'] === 'checkbox' ) {\\n update_post_meta( $post_id, $field['id'], '0' );\\n }\\n }\\n }\\n\\n }\\n\\n if (class_exists('MeinecooleMetaboxMetabox')) {\\n new MeinecooleMetaboxMetabox;\\n };\\n\\n \""

这只是一个例子。我必须在数据库中保存一些更大的字符串。

是否有防止保存此类大数据的最佳做法? (也是定价原因。我认为大数据使其非常昂贵)或者不会那么戏剧化?将它们作为查询字符串传递不起作用。我收到 400 错误。

您对此有何看法?

【问题讨论】:

    标签: angular firebase firebase-realtime-database


    【解决方案1】:

    您在此处显示的内容并不是很大。实时数据库可以很好地处理。

    如果您有数百 KB 或以上的大数据,则可能值得将其存储在 blob 存储产品中,例如 Cloud Storage for Firebase,并在数据库中存储对该数据的引用。这完全取决于您、您的需求和偏好。没有正确或错误的方法来做到这一点。如果您有性能或成本方面的问题,我建议您使用pricing page 上的内容进行基准测试并计算成本。

    【讨论】:

    • 感谢您的回答。我更新了上面的问题并输入了尽可能多的数据。这也不会太大而无法保存在数据库中吗?你怎么看?
    • 它仍然没有那么大。您可能想阅读文档中的数据库限制。字符串的最大大小为 10 MB,与您现在拥有的相比,这是巨大的。 firebase.google.com/docs/database/usage/limits#data_tree
    猜你喜欢
    • 2017-09-08
    • 1970-01-01
    • 2010-09-07
    • 2010-10-11
    • 2013-06-30
    • 1970-01-01
    • 2015-03-18
    • 2015-03-12
    • 1970-01-01
    相关资源
    最近更新 更多