Zend_Cache:加载缓存数据后,字符编码似乎搞乱了 [英] Zend_Cache: After loading cached data, character encoding seems messed up
问题描述
第一;在我的开发服务器(localhost; OSX上的默认XAMPP)一切正常,虽然当我部署完全相同的代码(和数据)到临时服务器(托管Apache2在Redhat上)。
我使用Zend_Cache缓存一些数据使用文件后端和自动序列化。
原始数据中使用的特殊字符显示正常,但是当从缓存加载时,它们都显示为乱码。
任何人都有一个线索? p>
PS。而不是一种解决方法,我正在寻找一种方法来了解在登台服务器上可能会错误。
b b b b b b b b b UPDATE
当查看原始缓存文件(一个序列化数组)时,我看到一个大的区别;
当在临时服务器上缓存的(相同)数据显示换行符时,在我的localhost上缓存的数据不显示换行符。
UPDATE
本地服务器运行 PHP 5.3
,暂存服务器运行 PHP 5.2.10
UPDATE
在Zend FW 1.10.8上运行
,
我几乎和你一样的状态,
开发机器是windows + php 5.3
开发机器是Linux + php 5.2.14
ZF版本是1.10
我唯一的区别是:我用来在引导类
中添加 mb_internal_encoding(UTF-8);
FYI,我用来缓存来自数据库的文本(阿拉伯语)所有编码的UTF8
当我打开文件,我看到阿拉伯文本如预期。
UPDATE :
1-这里是我完整的initCache函数,只是为了清楚
public function _initCache(){
mb_internal_encoding(UTF-8);
$ frontendOptions = array(
'automatic_serialization'=> TRUE,
'lifetime'=> 86400
);
$ backendOptions = array(
'cache_dir'=> APPLICATION_PATH。/ configs / cache /,
///'cache_dir'=> sys_get_temp_dir(),
);
$ cache = Zend_Cache :: factory('Core','File',$ frontendOptions,$ backendOptions);
Zend_Db_Table_Abstract :: setDefaultMetadataCache($ cache);
Zend_Registry :: set(cache,$ cache);
}
说明:
1 php版本早于PHP 6没有本机支持UTF-8,
http://stackoverflow.com/questions/716703/what-is-coming-in-php-6
2制作php 5.3或5.2使用 ICONV 或 MB_STRING
只需使用 var_dump mb_internal_encoding());
$ b
您可以在内部使用 ISO-8859-1 >
您可以覆盖 var_dump(mb_internal_encoding(UTF-8));
它会输出true(它成功覆盖内部编码)
说实话我不知道是否有更好的解决方案或< a href =http://stackoverflow.com/questions/4045060/how-bad-is-mb-internal-encodingutf-8>这有多糟糕,
如果你有更好的,我会很乐意采用它:)
UPDATE 2
case你不想使用该函数,
打开此文件Zend / Cache / Backend / File.php
并转到第976行$ b改变这个:
protected function _filePutContents($ file,$ string)
{
$ result = false;
$ f = @fopen($ file,'ab +');
if($ f){
if($ this-> _options ['file_locking'])@flock($ f,LOCK_EX);
fseek($ f,0);
ftruncate($ f,0);
$ tmp = @fwrite($ f,$ string);
if(!($ tmp === FALSE)){
$ result = true;
}
@fclose($ f);
}
@chmod($ file,$ this-> _options ['cache_file_umask']);
return $ result;
}
为:
protected function _filePutContents($ file,$ string)
{
$ string = mb_convert_encoding($ string,UTF-8,ISO-8859- 1); //我没有测试它,使用它在自己的风险,我宁愿坚持第一个解决方案
$ result = false;
$ f = @fopen($ file,'ab +');
if($ f){
if($ this-> _options ['file_locking'])@flock($ f,LOCK_EX);
fseek($ f,0);
ftruncate($ f,0);
$ tmp = @fwrite($ f,$ string);
if(!($ tmp === FALSE)){
$ result = true;
}
@fclose($ f);
}
@chmod($ file,$ this-> _options ['cache_file_umask']);
return $ result;
}
我没有手动测试,但它应该按预期工作
很高兴帮助!
First; On my development server (localhost; default XAMPP on OSX) everything works fine, though when I deploy the exact same code (and data) to the staging server (managed Apache2 on Redhat) it breaks.
I'm caching some data using Zend_Cache using the File backend and auto-serialization. Special characters used in the original data display fine, though when they are loaded from cache they're all garbled up.
Anyone got a clue?
PS. Instead of just a workaround, I'm looking for a way to understand what might go "wrong" on the staging server. What could possibly mess this up?
UPDATE The data I'm caching is UTF-8 encoded.
UPDATE When looking at the raw cache files (of a serialized array) there i See one big difference; The data cached on my localhost shows no newlines when the (identical) data cached on the staging server does show newlines.
UPDATE
Local server runs PHP 5.3
, staging server runs PHP 5.2.10
UPDATE Running on Zend FW 1.10.8
, I have almost identical state like you ,
development machine is windows + php 5.3
development machine is Linux + php 5.2.14
ZF version is 1.10
the only difference i had is : i used to add mb_internal_encoding("UTF-8");
in the bootstrap class
FYI , I used to cache text (arabic language ) from database all encoded UTF8 when i open the file i see the arabic text as expected .
UPDATE : 1- here is my complete initCache function just to make it clear
public function _initCache() {
mb_internal_encoding("UTF-8");
$frontendOptions = array(
'automatic_serialization' => TRUE,
'lifetime' => 86400
);
$backendOptions = array(
'cache_dir' => APPLICATION_PATH . "/configs/cache/",
///'cache_dir' => sys_get_temp_dir(),
);
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
Zend_Registry::set("cache", $cache);
}
Explanation : 1-Any php version earlier than PHP 6 doesn't have native support for UTF-8 , http://stackoverflow.com/questions/716703/what-is-coming-in-php-6
2-making php 5.3 or 5.2 deal with UTF8 by using ICONV or MB_STRING
simply by using var_dump(mb_internal_encoding());
you can tell that php using ISO-8859-1 internally ,
you can override it by var_dump(mb_internal_encoding("UTF-8"));
it would output true (it success to override the internal encoding )
to be honest i don't know if there is better solution or how bad it is ?? ,
if you had any better i would be happy to adopt it :)
UPDATE 2
in case you don't want to use that function ,
open this file "Zend/Cache/Backend/File.php"
and go to the line 976
change this :
protected function _filePutContents($file, $string)
{
$result = false;
$f = @fopen($file, 'ab+');
if ($f) {
if ($this->_options['file_locking']) @flock($f, LOCK_EX);
fseek($f, 0);
ftruncate($f, 0);
$tmp = @fwrite($f, $string);
if (!($tmp === FALSE)) {
$result = true;
}
@fclose($f);
}
@chmod($file, $this->_options['cache_file_umask']);
return $result;
}
to be this :
protected function _filePutContents($file, $string)
{
$string = mb_convert_encoding($string , "UTF-8" , "ISO-8859-1"); // i didn't test it , use it at your own risk and i'd rather stick with the first solution
$result = false;
$f = @fopen($file, 'ab+');
if ($f) {
if ($this->_options['file_locking']) @flock($f, LOCK_EX);
fseek($f, 0);
ftruncate($f, 0);
$tmp = @fwrite($f, $string);
if (!($tmp === FALSE)) {
$result = true;
}
@fclose($f);
}
@chmod($file, $this->_options['cache_file_umask']);
return $result;
}
i didn't test manually but it should work as expected
Glad it helped !
这篇关于Zend_Cache:加载缓存数据后,字符编码似乎搞乱了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!