易栈网-膘叔

发文热力图 2025-12-07 ~ 2026-12-06
少 多 近 365 天共 27 篇
浏览模式: 标准 | 列表 | 图文
xml2array 和 array2xml

xml2array 和 array2xml

这是一个简单的方法,其实是两种方法的其中一种:xml2array 和 array2xml 中的一种啦。
但因为array2xml的时候,没有办法做到更好的把attributes做到更好,因此,就折腾了一些简单的处理方法:
array2xml是hightman的方法的简版。我自己改了一些:

function array2xml($var, $type = 'root', $tag = '') {
$ret = '';
if (!is_int($type)) {
if ($tag)
return array2xml(array($tag => $var), 0, $type); else {
$tag .= $type;
$type = 0;
}
}
$level = $type;
$indent = str_repeat("\t", $level);
if (!is_array($var)) {
$ret .= $indent . '<' . $tag;
$var = strval($var);
if ($var == '') {
$ret .= ' />';
} else if (!preg_match('/[^0-9a-zA-Z@\._:\/-]/', $var)) {
$ret .= '>' . $var . '</' . $tag . '>';
} else {
$ret .= "><![CDATA[{$var}]]></{$tag}>";
}
// if (strpos($var, "\n") === false){
// $ret .= '><![CDATA[' . $var . ']]></' . $tag . '>';
// } else
$ret .= "\n";
} else if (!(is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) && !emptyempty($var)) {
foreach ($var as $tmp)
$ret .= array2xml($tmp, $level, $tag);
} else {
$ret .= $indent . '<' . $tag;
if ($level == 0)
$ret .= '';
if (isset($var['@attributes'])) {
foreach ($var['@attributes'] as $k => $v) {
if (!is_array($v)) {
$ret .= sprintf(' %s="%s"', $k, $v);
}
}
unset($var['@attributes']);
}
$ret .= ">\n";
foreach ($var as $key => $val) {
$ret .= array2xml($val, $level + 1, $key);
}
$ret .= "{$indent}</{$tag}>\n";
}
return $ret;

在其中强加了attributes。比较恶心的方法啦。。
然后xml2array,其实以前写过,但写的不太好,所以我这次抄的是ibm的网站上的xml2json中的代码:

define ("DEBUG", false);
// Maximum Recursion Depth that we can allow.
define ("MAX_RECURSION_DEPTH_ALLOWED", 25);
// An empty string
define ("EMPTY_STR", "");
// SimpleXMLElement object property name for attributes
define ("SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES", "@attributes");
// SimpleXMLElement object name.
define ("SIMPLE_XML_ELEMENT_PHP_CLASS", "SimpleXMLElement");
/**
* @static
* @param $simpleXmlElementObject
* @param int $recursionDepth
* @return array|null|string
*/
public static function xml2array($simpleXmlElementObject, $getAttributes = false , &$recursionDepth = 0 ) {
// Keep an eye on how deeply we are involved in recursion.
if ($recursionDepth > MAX_RECURSION_DEPTH_ALLOWED) {
// Fatal error. Exit now.
return (null);
}
if ($recursionDepth == 0) {
if (get_class($simpleXmlElementObject) != SIMPLE_XML_ELEMENT_PHP_CLASS) {
// If the external caller doesn't call this function initially
// with a SimpleXMLElement object, return now.
return (null);
} else {
// Store the original SimpleXmlElementObject sent by the caller.
// We will need it at the very end when we return from here for good.
$callerProvidedSimpleXmlElementObject = $simpleXmlElementObject;
}
} // End of if ($recursionDepth == 0) {
if (get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) {
// Get a copy of the simpleXmlElementObject
$copyOfsimpleXmlElementObject = $simpleXmlElementObject;
// Get the object variables in the SimpleXmlElement object for us to iterate.
$simpleXmlElementObject = get_object_vars($simpleXmlElementObject);
}
// It needs to be an array of object variables.
if (is_array($simpleXmlElementObject)) {
// Initialize the result array.
$resultArray = array();
// Is the input array size 0? Then, we reached the rare CDATA text if any.
if (count($simpleXmlElementObject) <= 0) {
// Let us return the lonely CDATA. It could even be
// an empty element or just filled with whitespaces.
return (trim(strval($copyOfsimpleXmlElementObject)));
}
// Let us walk through the child elements now.
foreach ($simpleXmlElementObject as $key => $value) {
// When this block of code is commented, XML attributes will be
// added to the result array.
// Uncomment the following block of code if XML attributes are
// NOT required to be returned as part of the result array.
/*
if((is_string($key)) && ($key == SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES)) {
continue;
}
*/
// Let us recursively process the current element we just visited.
// Increase the recursion depth by one.
$recursionDepth++;
if($key == '@attributes' && $getAttributes == true){
foreach(self::xml2array($value,$getAttributes, $recursionDepth) as $k=>$v){
$resultArray[$k]=$v;
}
}else{
$resultArray[$key] = self::xml2array($value,$getAttributes, $recursionDepth);
}
// Decrease the recursion depth by one.
$recursionDepth--;
} // End of foreach($simpleXmlElementObject as $key=>$value) {
if ($recursionDepth == 0) {
// That is it. We are heading to the exit now.
// Set the XML root element name as the root [top-level] key of
// the associative array that we are going to return to the caller of this
// recursive function.
$tempArray = $resultArray;
$resultArray = array();
$resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray;
}
return ($resultArray);
} else {
// We are now looking at either the XML attribute text or
// the text between the XML tags.
return (trim(strval($simpleXmlElementObject)));
} // End of else
}

改过其中的几行代码,否则会报错,改了哪几行我忘了。。官网地址是:http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/

这里还有一个:http://www.zenme.org/?action=show&id=270,可以参考一下

cimg0164.flv

ec2 micro型 上国外网站

amazon的VPS是大家都知道的。其实micro是最近11爷告诉我的。说是有信用卡的话,可以免费用micro一年(关于这一块我不多介绍了,网上多的是教程)
申请了一个micro之后,听人介绍说,用它来做网站会很耗资源,于是想到的就是,可以用来做ssh tunnel,用来翻翻墙啦。
果然,我用xshell在菜单的tunneling中选择了dynmac后,自动把商品设为1080,然后就能翻墙了。
开始上上facebook了。但其实我很纠结的是,我几乎没有好友在上面,推特我的兴趣也不大,可能只是用来看看一些平时看不到的技术性文章吧。

如果你有信用卡,你也可以尝试使用一年免费的ssh上网哦。

52ef2f324dd2fe69be5745a6d1100535

74361358ea9b27190a74c77ea7352347

笔记1

笔记1

这段时间在和IOS开发的同事们互相配合,本来是有笔记都会记录下来的。然而那段时间网站备案出点问题。然后就没有记录下来。于是现在开始就要把当时遇到的一些问题开始记录了。
比如这个。原本程序写的时候没有考虑推送,但是在进行了一阵后,发现还是需要推送才行,但当时创建的版本是是类似xxx.xxx.*,如果用推送的话就不能xxx.xxx.*,而是要用具体的版本(因为不是我写的啦,所以说不太清楚),后来找到了官方的介绍:
http://developer.apple.com/library/ios/#qa/qa1680/_index.html

可以从这里看:

Updating from a wildcard App ID to an explicit App ID

Q: My application is currently signed with a Provisioning Profile that uses a wildcard App ID. How do I enable my App ID to support In App Purchase or Apple Push Notification service?

A: My application is currently signed with a Provisioning Profile that uses a wildcard App ID. How do I enable my App ID to support In App Purchase or Apple Push Notification service?

In App Purchase and Apple Push Notification service require that your application is signed with a Provisioning Profile, which uses an explicit App ID such as com.mycompany.myappname. So, if you have shipped an application signed with a Provisioning Profile that uses a wildcard App ID such as com.mycompany.*, you may wonder how to enable that App ID to handle these features. The answer is to keep your project's Bundle ID the same and create a new App ID in the iPhone Portal that matches the Bundle ID you are currently using in your project. Follow the steps below to enable an App ID that supports In App Purchase or Apple Push Notification service:

  1. Identify your application's current Bundle ID

    You can find your Bundle ID by looking in your project's Info.plist file, however it is best to check what iTunes Connect says it is. Log in to iTunes Connect, navigate to the Manage Your Applications module present in the home page, then select the App Details link for the application you are updating, and copy the bundle identifier displayed in the ensuing page.

  2. Create a new App ID

    Your Team Agent or Team Admin should log in to the iPhone Portal and navigate to its App ID section to create a new App ID. In the App ID section, click on the New App ID button to navigate to the Create App ID form, which contains a Description field, a Bundle Identifier field, a Bundle Seed ID pop-up menu, and a Submit button as shown in Figure 1. Fill out the Description field with a meaningful name such as Explicit App ID for MyAppName for your App ID, select Generate New from the Bundle Seed ID pop-up menu, and paste the previously copied bundle identifier into the Bundle Identifier field. Click Submit to save the new App ID.

Figure 1 Create App ID form

952725d5437d2a8fc0da7b2811d22b29

  1. Enable the newly created App ID for In App Purchase or Apple Push Notification service

    Find your App ID in the App ID section of the Program Portal, click the Configure link next to it, and follow the instructions to enable your App ID for either In App Purchase or Apple Push Notification service.

  2. Update or create, download, and install a Provisioning Profile that uses your App ID enabled for In App Purchase or Apple Push Notifications

    Edit an existing Provisioning Profile or create a new one, and then associate it with your newly created App ID. Download and install this Provisioning Profile on your machine and select it in the Code Signing Identity section of your Target's Build pane in Xcode. Update the version number of your binary, build it, test it, and upload it to iTunes Connect for review.

看最后的两点就知道怎么操作了啦。。这些资料还是需要找英文的,MD,中文里就找不到类似的资料 。。。真TMD狗屎

终于通了。NND

终于通了。NND

历经多时,终于又通了。
6年的备案啊。。哭泣中

顺便,首页的模版被我误删除了。靠。

转:申请苹果IDP流程

转:申请苹果IDP流程

在苹果电脑没有到手先,先将这些信息备份下来,嗯,还有android的申请流程,过两天也会复制和转贴过来,免得以后真想找的时候找不到(听说android用国内的信用卡还不能申请?纠结中)
原文来自:http://blog.sina.com.cn/s/blog_53e2ac4d0100o06i.html
内容如下:

第一:IDP的申请

1.先在iPhone DevCenter上注册成为iphone developer
http://developer.apple.com/devcenter/ios/index.action

2.加入 iPhone开发程序项目iPhone Developer Program Apply Now
http://developer.apple.com/programs/start/standard/

3.你会得到一个PDF,打印出来,然后填写
填写IDP billing 表单的时候,注意,除开签名要手写(可以中文),其他的一定要全英文,不行就拼音。必须非常注意的是,billing address是你信用卡的账单地址,一定要和当时申请信用卡时的地址一致。

4,填好后发传真到指定号码,发完后给chinadev@asia.apple.com回一封信,告诉他们你已经传真过了。
您好:我已将表格填好并传真至+1 (408) 862-7602,还请劳烦贵公司工作人员帮忙与Billing 团队确认是否收到了我的传真。不胜感激。
最后查看信箱会有一自动回复的信件。

题外话:我公司的传真机不能发到美国,所以我用外国人的免费网站发了。

http://www.gotfreefax.com/

他说:Send Free Fax Online to the U.S. and Canada!

把你的传真件扫描一下,反正最后变成PDF格式。

网站上,填写各种信息,你的邮箱要写对,苹果号码4088627602,不要加1。

然后就到邮箱去,按照步骤操作,5分钟就会提示成功。

接下来就是等,苹果回复了,希望我的资料填写正确,一切顺利。

---------------------------
Over,现在developer.apple.com的用户登录好象也要翻墙了,真TMD纠结,其实以前developer.android.com也是要翻墙的。

文件同步工具Unison使用介绍

文件同步工具Unison使用介绍

说起unison的时候是在一个月前,当时在讨论怎么在与服务器进行数据同步,开始的时候都说的rsync,也有说dropbox的,但是想着dropbox经过国外的服务器,不安全啊。。数据从别人那里走一圈,怎么安全呢?
而说rsync的,是说在同步的时候会遍历全目录,如果目录不大,还OK,太大了,可能效率不高啥的。
这时候,乔大姐提出了unison,但是没用过,所以网上找了点资料先做备份。。。
原文URL:http://www.ixdba.net/a/web/2010/0422/44.html
因此,这是纯转载喽。。

一:Unison简介

Unison是windows和unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或网络上的)保持内容的一致。unison拥有其它一些同步工具或文件系统的相同特性,但也有自己的特点:

跨平台使用;

对内核和用户权限没有特别要求;

unison是双向的,它能自动处理两分拷贝中更新没有冲突的部分,有冲突的部分将会显示出来让用户选择更新策略;

IXDBA.NET技术社区

只要是能连通的两台主机,就可以运行unison,可以直接使用socket连接或安全的ssh连接方式,对带宽的要求不高,使用类似rsync的压缩传输协议。

Unison有文字界面和图形界面,这里只介绍如何在文字界面下使用.


二:安装unison

unison各种版本下载地址:

http://www.seas.upenn.edu/~bcpierce/unison//download.html

unison编译器下载地址:
http://caml.inria.fr/pub/distrib/ocaml-3.10

其它有用链接:
http://worldhello.net/doc/bcp/unison.html

http://www.seas.upenn.edu/~bcpierce/unison//contrib.html

http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#rshmeth

http://linux.chinaunix.net/bbs/viewthread.php?tid=888250&extra=page%3D1%26amp%3Bfilter%3Dreward

从以上地址可以下载各种平台,各种版本的unison,有基于源码安装的,有二进制的,我下载的是二进制的,可以直接使用.这里介绍源码安装:

源码安装unison

Linux下从源码包编译安装,需要一个叫做Objective Caml compiler的工具,版本至少3.0.7,可以从这里下载:http://caml.inria.fr/

Ocaml安装脚本如下:

# tar -zxf ocaml-3.09.3.tar.gz

# cd ocaml-3.09.3

# ./configure

# make world opt

# make install

Unison对版本要求很高,进行同步的两台主机需要相同版本的unison,所以这里使用和windows一致的版本2.13.16,unison-2.13.16.tar.gz

安装脚本如下:

# tar -zxf unison-2.13.16.tar.gz

# cd unison-2.13.16

# make UISTYLE=text

# make install

之后将生成可执行文件unison,将其cp到系统PATH即可。

# cp ./unison /usr/local/bin


三:配置双机ssh信任


由于unison在远程同步文件夹要登陆远程服务器,因此要配置两机互相信任

本例假设本地机为:10.178.1.132(linux)

远程机:10.178.1.110(solaris)

1. 在两台机器上创建 RSA密钥

以下操作要在本地机和远程机上都执行一遍

(1)以 root 用户登录

(2)在 root 用户的 主目录内创建.ssh 目录并设置正确的权限

[root@gsgatzhapp1 ~]# mkdir ~/.ssh

[root@gsgatzhapp1 ~]# chmod 700 ~/.ssh

(3)使用 ssh-keygen 命令生成第 2 版本的 SSH 协议的 RSA 密钥

[root@gsgatzhapp1 ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

17:e4:7c:79:8d:a0:00:3b:d9:f7:7a:56:f3:ac:54:4d oracle@gsgatzhapp1

在提示保存私钥(key)和公钥(public key)的位置时,使用默认值。 如果需要私钥密码(passphrase),则输入一个私钥密码(如果使用私钥密码,使用 ssh 执行远程命令时需要输入私钥密码,因此,本案例中未使用私钥密码),因此,直接回车即可。

2. 添加密钥到授权密钥文件(authorized key file)中

(1)以 root 用户登录

(2)在本地机上执行

[root@gsgatzhapp1 ~] # cd ~/.ssh

[root@gsgatzhapp1.ssh]#ssh 10.178.1.132 cat /root/.ssh/id_rsa.pub >> authorized_keys

[oracle@gsgatzhapp1.ssh]#ssh 10.178.1.110 cat /root/.ssh/id_rsa.pub >> authorized_keys

[oracle@gsgatzhapp1.ssh]#scp authorized_keys 10.178.1.110:/root/.ssh/

[oracle@gsgatzhapp1 .ssh]# chmod 600 /root/.ssh/authorized_keys

(3)在远程机10.178.1.110 上:

bash-2.05# chmod 600 /root/.ssh/authorized_keys

(4)测试

完成后,在 gsgatzhapp1 上执行:

[root@gsgatzhapp1 ~]# ssh 10.178.1.132 date

[root@gsgatzhapp1 ~]#ssh 10.178.1.110 date

如果不需要输入密码就出现系统日期,说明 SSH 配置成功。


四:unison的使用


Unison可以在一台主机上使用,同步两个文件夹,也可以在网络上是用。

1:本地使用

使用方法:

#unison 111 222 #同步本地的111和222文件夹

Contacting server...

Looking for changes

Warning: No archive files were found for these roots. This can happen either

because this is the first time you have synchronized these roots,

or because you have upgraded Unison to a new version with a different

archive format.

Update detection may take a while on this run if the replicas are

large.

Unison will assume that the 'last synchronized state' of both replicas

was completely empty. This means that any files that are different

will be reported as conflicts, and any files that exist only on one

replica will be judged as new and propagated to the other replica.

If the two replicas are identical, then no changes will be reported.

Press return to continue.[<spc>] Reconciling changes

111 222

<---- file aaaaaaaaaaaaa [f] ?

Commands:

<ret> or f or <spc> follow unison's recommendation (if any)

I ignore this path permanently

E permanently ignore files with this extension

N permanently ignore paths ending with this name

m merge the versions

d show differences

x show details

l list all suggested changes

p or b go back to previous item

g proceed immediately to propagating changes

q exit unison without propagating any changes

/ skip

> or . propagate from left to right

< or , propagate from right to left

<---- file aaaaaaaaaaaaa [f] f

Proceed with propagating updates? [] y

Propagating updates

UNISON started propagating changes at 15:06:08 on 27 Aug 2007

[BGN] Copying aaaaaaaaaaaaa

from /222

to /111

[END] Copying aaaaaaaaaaaaa

UNISON finished propagating changes at 15:06:08 on 27 Aug 2007

Saving synchronizer state

Synchronization complete (1 item transferred, 0 skipped, 0 failures)

如果检测到两个文件夹有所不同,unison会提示,让你选择相应的操作。如上例所示.

表示右边222的文件夹有新的文件,是否同步到左边的111文件夹,f表示force,然后将确认,进行更新,如果输入?会有更详细的介绍。

2: unison远程使用

使用方法:

# unison <本地目录> ssh://remotehostname(IP)/<远程目录的绝对路径>

例如:

# unison /home/AAA ssh://username@remotehostname(ip)//DB/path/BBB

表示将本机的目录/home/AAA和远端主机的/DB/path/BBB进行同步。一般的,需要两台机能ssh连接。

注意 在主机和目录间又多加了一个 "/"

3:unison参数说明

Unison有很多参数,这里只介绍经常使用的几个,详细的请参看unison手册:

" -testserver

测试连通性,连接到服务器即退出。示例:

$ unison / ssh://opensou1@bluehost/ -servercmd=~/bin/unison -testserver

如果服务器端 unison 可执行文件不在默认目录下,甚至没有 unison 命令(需要你编译一个上传到服务器),则需要使用 -servercmd 参数告诉要执行的服务器 unison 命令位置。

使用 -testserver 参数,则成功链接即退出,也不会去执行目录的比较等后续操作。

" -servercmd xxx

告诉 unison, 服务器端的 unison 命令是什么。参见上面的示例。

" -auto

接受缺省的动作,然后等待用户确认是否执行。

" -batch

batch mode, 全自动模式,接受缺省动作,并执行。

" -ignore xxx

增加 xxx 到忽略列表中

" -ignorecase [true|false|default]

是否忽略文件名大小写

" -follow xxx

是否支持对符号连接指向内容的同步

" owner = true (保持同步过来的文件属主)

" group = true (保持同步过来的文件组信息)

" perms = -1 (保持同步过来的文件读写权限)

" repeat = 1 (间隔1秒后,开始新的一次同步检查)

" retry = 3 (失败重试)

" sshargs = -C (使用ssh的压缩传输方式)

" xferbycopying = true

" -immutable xxx

不变目录,扫描时可以忽略

" -silent

安静模式

" -times

同步修改时间

" -path xxx 参数

只同步 -path 参数指定的子目录以及文件,而非整个目录。-path 可以多次出现,例如

unison /home/username ssh://remotehost//home/username \

-path shared \

-path pub \

-path .netscape/bookmarks.html

4:通过配置文件来使用unison

尽管可以完全通过命令行的方式来指定unison运行所需要的参数,但我们还是推荐使用配置文件来进行配置使用unison,原因很简单,看配置文件比看命令行容易理解,而且可管理性更强。

默认的配置文件夹位于~currentuser/.unison,即当前用户的home目录下,windows则位于C:\Documents and Settings\currentuser\.unison,默认的配置文件名是default.prf.

运行这样的命令:

#unison exitgogo

Unison将默认读取~currentuser/.unison/exitgogo.prf文件里的配置信息.我的配置信息在/root/.unison/exitgogo.prf,因此我们可以根据上面参数的介绍,把所有的参数配置信息写入到一个.prf的文件中.

下面是我的一个web应用中两个文件夹同步的配置信息:

root = /sina/webdata

root = ssh://root@192.168.60.121//sina/webdata

#force =/sina/webdata

ignore = Path as/*

#prefer = ssh://root@192.168.60.121//sina/webdata

batch = true

#repeat = 1

#retry = 3

owner = true

group = true

perms = -1

fastcheck=false

rsync =false

#debug=verbose

sshargs = -C

xferbycopying = true

log = true

logfile = /root/.unison/sina_122.1547.log

说明如下:

两个root表示需要同步的文件夹

force表示以本地的/var/www/bbsnew文件夹为标准,将该目录同步到远端。注意,如果指定了force参数,那么unison就变成了单项同步了,也就是说会以force指定的文件夹为准进行同步.

Unison本身是可以双向同步的,但是要做到双向同步,就不要设置force参数,如果设置了force参数,就成了单项同步了,此时unison类似与sync.

Unison双向同步基本原理是:假如有A B两个文件夹,A文件夹把自己的改动同步到B,B文件夹也把自己的改动同步到A,最后A B两文件夹的内容相同,是 A B文件夹的合集.

Unison双向同步的一个缺点是,对于一个文件在两个同步文件夹中都被修改时,unison是不会去同步的,因为unison无法判断以那个为准.

ignore = Path表示忽略/sina/webdata下面的WEB-INF/tmp目录,即同步时不同步它。

batch = true,表示全自动模式,接受缺省动作,并执行

-fastcheck true表示同步时使用文件的创建时间来比较两地文件,如果这个选项为false,unison则将比较两地文件的内容.建议设置为true

log = true表示在终端输出运行信息。

logfile则指定了同时将输出写入log文件。


五:unison FAQ


如何在和远程服务器同步大量数据,上传一部分数据后,超时:

9% 559:15 ETARead from remote host bluehost: Connection reset by peer

Fatal error: Lost connection with the server

实际操作中,最好的方法是,第一次先把要上传的文件打成包,用 ftp 上传,然后展开到服务器中,之后执行一次 unison 同步即可。

(责任编辑:南非蚂蚁)

转:geohash-用字符串实现附近地点搜索

转:geohash-用字符串实现附近地点搜索

依稀记得MYSQL5开始支持经纬度了,但在项目中没用过,所以现在也记不清是否有此功能了,不过今天看到这篇文章的时候,感觉这样的处理方法也不错。
原文来自:http://tech.idv2.com/2011/07/05/geohash-intro/

上回说到了用经纬度范围实现附近地点搜索。 一些小型应用中这样做没问题,但在大型应用中它有个显著的缺点:速度慢。慢的原因有两个, 第一是范围比较的索引利用率并不高,第二是SQL语句极其不稳定(不同的当前位置会产生完全不同的SQL查询),很难缓存。

可以考虑使用geohash算法。

geohash是一种地址编码,它能把二维的经纬度编码成一维的字符串。比如,北海公园的编码是wx4g0ec1。

52ef2f324dd2fe69be5745a6d1100535

geohash有以下几个特点:

首先,geohash用一个字符串表示经度和纬度两个坐标。某些情况下无法在两列上同时应用索引 (例如MySQL 4之前的版本,Google App Engine的数据层等),利用geohash,只需在一列上应用索引即可。

其次,geohash表示的并不是一个点,而是一个矩形区域。比如编码wx4g0ec19,它表示的是一个矩形区域。 使用者可以发布地址编码,既能表明自己位于北海公园附近,又不至于暴露自己的精确坐标,有助于隐私保护。

第三,编码的前缀可以表示更大的区域。例如wx4g0ec1,它的前缀wx4g0e表示包含编码wx4g0ec1在内的更大范围。 这个特性可以用于附近地点搜索。首先根据用户当前坐标计算geohash(例如wx4g0ec1)然后取其前缀进行查询 (SELECT * FROM place WHERE geohash LIKE 'wx4g0e%'),即可查询附近的所有地点。

74361358ea9b27190a74c77ea7352347

geohash的算法

下面以(39.92324, 116.3906)为例,介绍一下geohash的编码算法。首先将纬度范围(-90, 90)平分成两个区间(-90, 0)、(0, 90), 如果目标纬度位于前一个区间,则编码为0,否则编码为1。由于39.92324属于(0, 90),所以取编码为1。然后再将(0, 90)分成 (0, 45), (45, 90)两个区间,而39.92324位于(0, 45),所以编码为0。以此类推,直到精度符合要求为止,得到纬度编码为1011 1000 1100 0111 1001。

纬度范围 划分区间0 划分区间1 39.92324所属区间
(-90, 90) (-90, 0.0) (0.0, 90) 1
(0.0, 90) (0.0, 45.0) (45.0, 90) 0
(0.0, 45.0) (0.0, 22.5) (22.5, 45.0) 1
(22.5, 45.0) (22.5, 33.75) (33.75, 45.0) 1
(33.75, 45.0) (33.75, 39.375) (39.375, 45.0) 1
(39.375, 45.0) (39.375, 42.1875) (42.1875, 45.0) 0
(39.375, 42.1875) (39.375, 40.7812) (40.7812, 42.1875) 0
(39.375, 40.7812) (39.375, 40.0781) (40.0781, 40.7812) 0
(39.375, 40.0781) (39.375, 39.7265) (39.7265, 40.0781) 1
(39.7265, 40.0781) (39.7265, 39.9023) (39.9023, 40.0781) 1
(39.9023, 40.0781) (39.9023, 39.9902) (39.9902, 40.0781) 0
(39.9023, 39.9902) (39.9023, 39.9462) (39.9462, 39.9902) 0
(39.9023, 39.9462) (39.9023, 39.9243) (39.9243, 39.9462) 0
(39.9023, 39.9243) (39.9023, 39.9133) (39.9133, 39.9243) 1
(39.9133, 39.9243) (39.9133, 39.9188) (39.9188, 39.9243) 1
(39.9188, 39.9243) (39.9188, 39.9215) (39.9215, 39.9243) 1

经度也用同样的算法,对(-180, 180)依次细分,得到116.3906的编码为1101 0010 1100 0100 0100。

经度范围 划分区间0 划分区间1 116.3906所属区间
(-180, 180) (-180, 0.0) (0.0, 180) 1
(0.0, 180) (0.0, 90.0) (90.0, 180) 1
(90.0, 180) (90.0, 135.0) (135.0, 180) 0
(90.0, 135.0) (90.0, 112.5) (112.5, 135.0) 1
(112.5, 135.0) (112.5, 123.75) (123.75, 135.0) 0
(112.5, 123.75) (112.5, 118.125) (118.125, 123.75) 0
(112.5, 118.125) (112.5, 115.312) (115.312, 118.125) 1
(115.312, 118.125) (115.312, 116.718) (116.718, 118.125) 0
(115.312, 116.718) (115.312, 116.015) (116.015, 116.718) 1
(116.015, 116.718) (116.015, 116.367) (116.367, 116.718) 1
(116.367, 116.718) (116.367, 116.542) (116.542, 116.718) 0
(116.367, 116.542) (116.367, 116.455) (116.455, 116.542) 0
(116.367, 116.455) (116.367, 116.411) (116.411, 116.455) 0
(116.367, 116.411) (116.367, 116.389) (116.389, 116.411) 1
(116.389, 116.411) (116.389, 116.400) (116.400, 116.411) 0
(116.389, 116.400) (116.389, 116.394) (116.394, 116.400) 0

接下来将经度和纬度的编码合并,奇数位是纬度,偶数位是经度,得到编码 11100 11101 00100 01111 00000 01101 01011 00001。

最后,用0-9、b-z(去掉a, i, l, o)这32个字母进行base32编码,得到(39.92324, 116.3906)的编码为wx4g0ec1。

十进制 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
base32 0 1 2 3 4 5 6 7 8 9 b c d e f g
十进制 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
base32 h j k m n p q r s t u v w x y z

解码算法与编码算法相反,先进行base32解码,然后分离出经纬度,最后根据二进制编码对经纬度范围进行细分即可,这里不再赘述。 不过由于geohash表示的是区间,编码越长越精确,但不可能解码出完全一致的地址。

geohash的应用:附近地址搜索

geohash的最大用途就是附近地址搜索了。不过,从geohash的编码算法中可以看出它的一个缺点:位于格子边界两侧的两点, 虽然十分接近,但编码会完全不同。实际应用中,可以同时搜索当前格子周围的8个格子,即可解决这个问题。

以geohash的python库为例,相关的geohash操作如下:

>>> import geohash >>> geohash.encode(39.92324, 116.3906, 5)  # 编码,5表示编码长度 'wx4g0' >>> geohash.expand('wx4g0')                # 求wx4g0格子及周围8个格子的编码 ['wx4ep', 'wx4g1', 'wx4er', 'wx4g2', 'wx4g3', 'wx4dz', 'wx4fb', 'wx4fc', 'wx4g0']

最后,我们来看看本文开头提出的两个问题:速度慢,缓存命中率低。使用geohash查询附近地点,用的是字符串前缀匹配:

SELECT * FROM place WHERE geohash LIKE 'wx4g0%';

而前缀匹配可以利用geohash列上的索引,因此查询速度不会太慢。另外,即使用户坐标发生微小的变化, 也能编码成相同的geohash,这就保证了每次执行相同的SQL语句,使得缓存命中率大大提高。

相关资源

续:关于新浪微博

昨天刚写了关于新浪微博的二三事,早上板子就说了,每次取50条只返回45~50条这种 是因为纪录被删除了,但索引没有删除,这样可以提升速度。
好吧,我接受了这个说明。
但是今天在拉取评论的时候,发现了一个很恶心的问题,纪录居然从7月8日一下子跳到了7月4日,连接调用了几次接口,返回都是这样。
这让人太纠结了,然后去微博查了一下,纪录都存在,哭泣中。。。

0b3cd1ec88d1951d9baaae9d99a8a036

9c50f151f2112fcc0ba4a43b7332c005

12b4886cdd34da303c1c49c7fcf70df1

77e62fbe2430db0874365483387d02c8

关于新浪微博

在看到这篇文章后,我也想乱说几句。。。。
我看到的文章地址是:http://yangbalu.blog.163.com/blog/static/1902902742011696721650/
嗯,原文很长我就不列出来了。。。文中还提到了一些其他网站的链接:“以上都是老夫亲自为新浪微博开放平台把脉得出的结论,一些道听途说老夫一点都没敢列出来,比如新华网报道的“开发者被迫出逃新浪微博 表面为开放实为封闭平台 ”。”
我要说的是什么呢?
我其实是想说,我在用它的接口的时候,明明我用的参数是取50条,结果我连续取了50页,出来的结果好纠结,有的时候45条,有时候48条,50页中就两次是50条,其他的都在45~49之间徘徊,还好我没有用来排版,否则就真的笑话了。

b0bead10c9812854e09a8ce2f9cc71f9