您的位置:首页 > 编程语言 > PHP开发

PHP框架 Phalcon 1.0.0 beta发布,实测性能强劲

2013-03-08 09:33 267 查看
我们宣布今天(时差问题,应该是昨天了)发布Phalcon 1.0.0 beta版本,发布此版本,主要是从社区得到测试反馈加以改进。此版本引入了一些比较重要的特性:

多级缓存:

这个新功能是缓存组件的一部分,允许开发人员来实现一个多级缓存。这个新功能非常有用,因为你可以保存相同的数据在多个后端缓存组件中,并可配置不同的生命周期,读取缓存从最快的适配器到最慢的一个,直到数据都已经过期:

01
<?php
02
$ultraFastFrontend
=
new
Phalcon\Cache\Frontend\Data(
array
(
03
"lifetime"
=>3600
04
));
05
06
$fastFrontend
=
new
Phalcon\Cache\Frontend\Data(
array
(
07
"lifetime"
=>86400
08
));
09
10
$slowFrontend
=
new
Phalcon\Cache\Frontend\Data(
array
(
11
"lifetime"
=>604800
12
));
13
14
//Backends are registered from the fastest to the slower
15
$cache
=
new
\Phalcon\Cache\Multiple(
array
(
16
new
Phalcon\Cache\Backend\Apc(
$ultraFastFrontend
,
array
(
17
"prefix"
=>
'cache'
,
18
)),
19
new
Phalcon\Cache\Backend\Memcache(
$fastFrontend
,
array
(
20
"prefix"
=>
'cache'
,
21
"host"
=>
"localhost"
,
22
"port"
=>
"11211"
23
)),
24
new
Phalcon\Cache\Backend\File(
$slowFrontend
,
array
(
25
"prefix"
=>
'cache'
,
26
"cacheDir"
=>
"../app/cache/"
27
))
28
));
29
30
//Save,saves in every backend
31
$cache
->save(
'my-key'
,
$data
);
Volt模板引擎改进
此版本引入的一些Volt改进特性:

01
{# Ternary operator #}
02
{{ total > 0 ? total|format('%0.2f') : '0.0' }}
03
04
{# For-Else clause #}
05
{% for robot in robots %}
06
{{ robot.name }}
07
{% else %}
08
There are no robots
09
{% endfor %}
10
11
{# Loop-Context #}
12
<
table
>
13
{% for robot in robots %}
14
{% if loop.first %}
15
<
thead
>
16
<
tr
>
17
<
th
>Position</
th
>
18
<
th
>Id</
th
>
19
<
th
>Name</
th
>
20
</
tr
>
21
</
thead
>ae
22
<
tbody
>
23
{% endif %}
24
<
tr
>
25
<
th
>{{ loop.index }}</
th
>
26
<
th
>{{ robot.id }}</
th
>
27
<
th
>{{ robot.name }}</
th
>
28
</
tr
>
29
{% if loop.last %}
30
<
tbody
>
31
{% endif %}
32
{% endfor %}
33
</
table
>
34
35
{# Space control delimiters #}
36
<
ul
>
37
{%- for robot in robots -%}
38
<
li
>{{- robot.name -}}</
li
>
39
{%- endfor %}
40
</
ul
>
垂直/水平分片的改进
现在,你可以定义不同的数据库连接,使之一个只用于读操作,另一个只用于写操作。对于RDBMS中使用主从方式的应用场景非常有用:

1
class
Robots
extends
Phalcon\Mvc\Model
2
{
3
public
function
initialize()
4
{
5
$this
->setReadConnectionService(
'dbSlave'
);
6
$this
->setWriteConnectionService(
'dbMaster'
);
7
}
8
}
在大中型项目中,在数据库设计的时候,考虑到数据库最大承受数据量,通常会把数据库或者数据表水平切分,以降低单个库,单个表的压力。
因此,水平切片意味着读取数据将根据条件进行数据查询:

01
class
Robots
extends
Phalcon\Mvc\Model
02
{
03
public
function
selectReadConnection(
$intermediate
,
$bindParams
,
$bindTypes
)
04
{
05
//Check if there is a 'where' clause in the select
06
if
(isset(
$intermediate
[
'where'
])) {
07
08
$conditions
=
$intermediate
[
'where'
];
09
10
//Choose the possible shard according to the conditions
11
if
(
$conditions
[
'left'
][
'name'
] ==
'id'
) {
12
$id
=
$conditions
[
'right'
][
'value'
];
13
if
(
$id
> 0 &&
$id
< 10000) {
14
return
$this
->getDI()->get(
'dbShard1'
);
15
}
16
if
(
$id
> 10000) {
17
return
$this
->getDI()->get(
'dbShard2'
);
18
}
19
}
20
}
21
22
//Use a default shard
23
return
$this
->getDI()->get(
'dbShard0'
);
24
}
25
26
}
记录快照
有了这项新功能,指定的Models可以设定为查询时保持记录的快照。你可以使用此功能来实现审计或只是为了知道哪些字段被更改过:

1
class
Robots
extends
Phalcon\Mvc\Model
2
{
3
public
function
initalize()
4
{
5
$this
->keepSnapshots(true);
6
}
7
}
你可以通过以下方式检测哪些字段被更改过:

1
$robot
=
new
Robots();
2
$robot
->name =
'Other name'
;
3
var_dump(
$robot
->getChangedFields());
// ['name']
4
var_dump(
$robot
->hasChanged(
'name'
));
// true
5
var_dump(
$robot
->hasChanged(
'type'
));
// false
动态更新
此功能允许ORM在创建SQL UPDATE语句时,只改变有改变的字段,而不是整个表的所有字段。在某些情况下,这可以提高性能,减少应用程序与数据库服务器之间的传输数据量:

1
class
Robots
extends
Phalcon\Mvc\Model
2
{
3
public
function
initalize()
4
{
5
$this
->useDynamicUpdate(true);
6
}
7
}
验证
Phalcon\Validation 是基于ORM,ODM验证系统实现的一个独立的验证组件,该组件可以在model及collection之外实现验证规则:

01
$validation
=
new
Phalcon\Validation();
02
03
$validation
04
->add(
'name'
,
new
PresenceOf(
array
(
05
'message'
=>
'The name is required'
06
)))
07
->add(
'name'
,
new
StringLength(
array
(
08
'min'
=>5,
09
'minimumMessage'
=>
'The name is too short'
10
)))
11
->add(
'email'
,
new
PresenceOf(
array
(
12
'message'
=>
'The email is required'
13
)))
14
->add(
'email'
,
new
Email(
array
(
15
'message'
=>
'The email is not valid'
16
)))
17
->add(
'login'
,
new
PresenceOf(
array
(
18
'message'
=>
'The login is required'
19
)));
20
21
$messages
=
$validation
->validate(
$_POST
);
22
if
(
count
(
$messages
)) {
23
foreach
(
$messages
as
$message
) {
24
echo
$message
;
25
}
26
}
版本 1.0.0还包括其他一些小改动,bug修复及稳定性方面的改进。你可以在此看到完整的更新日志

帮助测试
可以从1.0.0分支中安装此版本:


1
git clone http://github.com/phalcon/cphalcon[/code]
2
cd
build
3
git checkout 1.0.0
4
sudo
./
install
Windows用户可以直接从下载页面下载DLL文件。

我们欢迎您提供宝贵的意见与建议,可以通过 Phosphorum,Stack Overflow or Google Group 进行交流。如果你发现了任何BUG,请在Github上创建issue.

中文文档: http://phalcon.5iunix.net
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: