WordPress中选项字符串的限制

时间:2012-09-14 00:13:13

标签: php wordpress-plugin wordpress

用于选项的字符串是否有限制?我想使用Transient API,当我在瞬态名称中包含一串url时,它不会保存数据。我不确定这是由于字符长度还是非法字符造成的。

$url = 'http://stackoverflow.com/questions/tagged/php+wordpress+wordpress-plugin';
$strTransient = 'sample_transient_' . $url;
$key = 'sample_transient';
$html = get_transient($strTransient);   
if( false === $html ) {
    echo 'cache is not used: ' . $strTransient . '<br />';      
    $html = wp_remote_get($url);    
    $html = $html['body'];  
    $savehtml = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $html, MCRYPT_MODE_CBC, md5(md5($key))));
    set_transient($strTransient, $savehtml, 60 );
} else {
    echo 'cache is used. <br />';
    $html = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($html), MCRYPT_MODE_CBC, md5(md5($key)));  
}
print_r($html);

提前致谢。

[编辑]

这似乎是由于字符长度。

$transientkey = 'verylongstring_verylongstring_verylongstring_verylongstring_verylongstring_verylongstring_verylongstring'; // <-- fails
// $transientkey = 'shortstring';   // <-- okay

$data = get_transient($transientkey);   
if( false === $data ) {
    echo 'transient is not saved: ' . $transientkey . '<br />';     
    $data = 'hello world!';
    echo 'now saving the data.<br />';
    set_transient($transientkey, $data, 60 );
} else {
    echo 'transient is used. <br />';
}
print_r($data);

那么在哪里可以找到有关选项键限制的确切信息?我无法在核心中找到它。这是对set_transient()

的评论

WP-包括/ option.php

/**
 * Set/update the value of a transient.
 *
 * You do not need to serialize values. If the value needs to be serialized, then
 * it will be serialized before it is set.
 *
 * @since 2.8.0
 * @package WordPress
 * @subpackage Transient
 *
 * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
 *  transient value to be stored.
 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
 *
 * @param string $transient Transient name. Expected to not be SQL-escaped.
 * @param mixed $value Transient value. Expected to not be SQL-escaped.
 * @param int $expiration Time until expiration in seconds, default 0
 * @return bool False if value was not set and true if value was set.
 */

还有任何建议将网址标识为临时密钥吗?

1 个答案:

答案 0 :(得分:2)

临时名称documentation says为&#34; 预计不会被SQL转义。字长不应超过45个字符。&#34;

查看Wordpress 3.4.1中的表结构:

mysql> desc wp_options;
+--------------+---------------------+------+-----+---------+----------------+
| Field        | Type                | Null | Key | Default | Extra          |
+--------------+---------------------+------+-----+---------+----------------+
| option_id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| option_name  | varchar(64)         | NO   | UNI |         |                |
| option_value | longtext            | NO   |     | NULL    |                |
| autoload     | varchar(20)         | NO   |     | yes     |                |
+--------------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

因此option_name不能超过64个字符,但data可以大到2 32 字节(~4GB)。 set_transient最终调用add/update_option,其中选项名称最多可包含64个字符,但由于瞬态以特殊方式工作,45是您应使用的最长值。

编辑:

从查看WP代码,瞬态名称限制为每个参考指南45个字符的原因是因为代码执行此操作:

$transient_timeout = '_transient_timeout_' . $transient;
//...
add_option( $transient_timeout, time() + $expiration, '', 'no' );

因此,如果您将hello作为瞬态传递,则会添加一个名为_transient_timeout_hello的选项。鉴于:45 + strlen('_transient_timeout_') === 64和64是WP选项表中选项名称字段的最大varchar长度。